Skip to content

Commit 506d2c3

Browse files
committed
udpate some info, remove some no-use methods
1 parent b5c6c88 commit 506d2c3

File tree

8 files changed

+115
-187
lines changed

8 files changed

+115
-187
lines changed

README.md

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -48,31 +48,49 @@ e.g
4848
- **github** https://github.com/inhere/php-validate.git
4949
- **gitee** https://gitee.com/inhere/php-validate.git
5050

51-
> **注意:** master 分支是要求 `php7+` 的(推荐使用)。php5 分支是支持php5的代码分支,基本上不再维护
51+
> **注意:** master 分支是要求 `php7+` 的(推荐使用)。`1.x` 分支是支持php5的代码分支,但是基本上不再维护
5252
5353
## 安装
5454

55-
- 使用 composer 命令
56-
5755
```php
5856
composer require inhere/php-validate
5957
// composer require inhere/php-validate ^2.2
6058
```
6159

62-
- 使用 composer.json
60+
## 立即使用
6361

64-
编辑 `composer.json`,在 `require` 添加
62+
<a name="how-to-use1"></a>
63+
### 方式1: 直接使用类 `Validation`
6564

66-
```
67-
"inhere/php-validate": "~2.0",
68-
// "inhere/php-validate": "dev-php5", // for php5
69-
```
65+
需要快速简便的使用验证时,可直接使用 `Inhere\Validate\Validation`
7066

71-
然后执行: `composer update`
67+
```php
68+
use Inhere\Validate\Validation;
7269

73-
## 立即使用
70+
class SomeController
71+
{
72+
public function demoAction()
73+
{
74+
$v = Validation::check($_POST,[
75+
// add rule
76+
['title', 'min', 40],
77+
['freeTime', 'number'],
78+
]);
7479

75-
<a name="how-to-use1"></a>
80+
if ($v->fail()) {
81+
var_dump($v->getErrors());
82+
var_dump($v->firstError());
83+
}
84+
85+
// $postData = $v->all(); // 原始数据
86+
$safeData = $v->getSafeData(); // 验证通过的安全数据
87+
88+
$db->save($safeData);
89+
}
90+
}
91+
```
92+
93+
<a name="how-to-use2"></a>
7694
### 方式1: 继承类 `Validation`
7795

7896
创建一个新的class,并继承 `Inhere\Validate\Validation`。用于一个(或一系列相关)请求的验证, 相当于 laravel 的 表单请求验证
@@ -185,37 +203,6 @@ $safeData = $v->getSafeData(); // 验证通过的安全数据
185203
$db->save($safeData);
186204
```
187205

188-
<a name="how-to-use2"></a>
189-
### 方式2: 直接使用类 `Validation`
190-
191-
需要快速简便的使用验证时,可直接使用 `Inhere\Validate\Validation`
192-
193-
```php
194-
use Inhere\Validate\Validation;
195-
196-
class SomeController
197-
{
198-
public function demoAction()
199-
{
200-
$v = Validation::check($_POST,[
201-
// add rule
202-
['title', 'min', 40],
203-
['freeTime', 'number'],
204-
]);
205-
206-
if ($v->fail()) {
207-
var_dump($v->getErrors());
208-
var_dump($v->firstError());
209-
}
210-
211-
// $postData = $v->all(); // 原始数据
212-
$safeData = $v->getSafeData(); // 验证通过的安全数据
213-
214-
$db->save($safeData);
215-
}
216-
}
217-
```
218-
219206
<a name="how-to-use3"></a>
220207
### 方式3: 使用trait `ValidationTrait`
221208

@@ -296,7 +283,7 @@ class UserController
296283

297284
## 添加自定义验证器
298285

299-
- **方式1**在继承了 `Inhere\Validate\Validation` 的子类添加验证方法. 请看上面的 [使用方式1](#how-to-use1)
286+
- **方式1**在继承了 `Inhere\Validate\Validation` 的子类添加验证方法. 请看上面的 [使用方式1](#how-to-use2)
300287

301288
> 注意: 写在当前类里的验证器方法必须带有后缀 `Validator`, 以防止对内部的其他的方法造成干扰
302289

src/Filter/Filters.php

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,11 @@ public static function lowercase($val): string
211211
return \is_numeric($val) ? $val : '';
212212
}
213213

214-
return Helper::strToLower($val);
214+
if (\function_exists('mb_strtolower')) {
215+
return \mb_strtolower($val, 'utf-8');
216+
}
217+
218+
return \strtolower($val);
215219
}
216220

217221
/**
@@ -226,16 +230,38 @@ public static function upper($val): string
226230

227231
/**
228232
* string to uppercase
229-
* @param string $val
233+
* @param string $str
230234
* @return string
231235
*/
232-
public static function uppercase($val): string
236+
public static function uppercase($str): string
233237
{
234-
if (!$val || !\is_string($val)) {
235-
return \is_numeric($val) ? $val : '';
238+
if (!$str || !\is_string($str)) {
239+
return \is_numeric($str) ? $str : '';
236240
}
237241

238-
return Helper::strToUpper($val);
242+
if (\function_exists('mb_strtoupper')) {
243+
return \mb_strtoupper($str, 'utf-8');
244+
}
245+
246+
return \strtoupper($str);
247+
}
248+
249+
/**
250+
* @param string $str
251+
* @return string
252+
*/
253+
public static function ucfirst($str): string
254+
{
255+
return self::uppercase(self::subStr($str, 0, 1)) . self::subStr($str, 1);
256+
}
257+
258+
public static function ucwords($str): string
259+
{
260+
if (\function_exists('mb_convert_case')) {
261+
return \mb_convert_case($str, \MB_CASE_TITLE);
262+
}
263+
264+
return \ucwords(Filters::lowercase($str));
239265
}
240266

241267
/**
@@ -250,7 +276,9 @@ public static function snake($val, $sep = '_'): string
250276
}
251277

252278
/**
253-
* string to snakeCase
279+
* Transform a CamelCase string to underscore_case string
280+
* 'CMSCategories' => 'cms_categories'
281+
* 'RangePrice' => 'range_price'
254282
* @param string $val
255283
* @param string $sep
256284
* @return string
@@ -261,7 +289,9 @@ public static function snakeCase($val, $sep = '_'): string
261289
return '';
262290
}
263291

264-
return Helper::toSnakeCase($val, $sep);
292+
$val = \preg_replace('/([A-Z][a-z])/', $sep . '$1', $val);
293+
294+
return self::lowercase(\trim($val, $sep));
265295
}
266296

267297
/**
@@ -276,7 +306,7 @@ public static function camel($val, $ucFirst = false): string
276306
}
277307

278308
/**
279-
* string to camelcase
309+
* Translates a string with underscores into camel case (e.g. first_name -> firstName)
280310
* @param string $val
281311
* @param bool $ucFirst
282312
* @return string
@@ -287,7 +317,15 @@ public static function camelCase($val, $ucFirst = false): string
287317
return '';
288318
}
289319

290-
return Helper::toCamelCase($val, $ucFirst);
320+
$str = self::lowercase($val);
321+
322+
if ($ucFirst) {
323+
$str = self::ucfirst($str);
324+
}
325+
326+
return \preg_replace_callback('/_+([a-z])/', function ($c) {
327+
return \strtoupper($c[1]);
328+
}, $str);
291329
}
292330

293331
/**
@@ -311,7 +349,25 @@ public static function strToTime($val): int
311349
return 0;
312350
}
313351

314-
return (int)strtotime($val);
352+
return (int)\strtotime($val);
353+
}
354+
355+
/**
356+
* @param string $str
357+
* @param int $start
358+
* @param int $length
359+
* @param string $encoding
360+
* @return bool|string
361+
*/
362+
public static function subStr(string $str, int $start, int $length = 0, string $encoding = 'utf-8')
363+
{
364+
$length = $length === 0 ? Helper::strlen($str) : $length;
365+
366+
if (\function_exists('mb_substr')) {
367+
return \mb_substr($str, $start, $length, $encoding);
368+
}
369+
370+
return \substr($str, $start, $length);
315371
}
316372

317373
/**
@@ -466,7 +522,7 @@ public static function stringCute($string, $start = 0, $end = null): string
466522
}
467523

468524
// $length = Helper::strlen($string);
469-
return Helper::subStr($string, $start, $end);
525+
return self::subStr($string, $start, $end);
470526
}
471527

472528
/**

src/Filter/Filtration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace Inhere\Validate\Filter;
1010

11-
use Inhere\Validate\Utils\DataFiltersTrait;
11+
use Inhere\Validate\Utils\DataFilteringTrait;
1212

1313
/**
1414
* Class Filtration
@@ -23,7 +23,7 @@
2323
*/
2424
class Filtration
2525
{
26-
use DataFiltersTrait;
26+
use DataFilteringTrait;
2727

2828
/** @var array raw data */
2929
private $_data;

src/Utils/DataFiltersTrait.php renamed to src/Utils/DataFilteringTrait.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@
1111
use Inhere\Validate\Filter\Filters;
1212

1313
/**
14-
* Trait DataFiltersTrait
14+
* Trait DataFilteringTrait
1515
* @package Inhere\Validate\Utils
1616
*/
17-
trait DataFiltersTrait
17+
trait DataFilteringTrait
1818
{
1919
/** @var array user custom filters */
2020
private static $customFilters = [];
2121

2222
/** @var array filter aliases map */
2323
private static $filterAliases = [
24+
'substr' => 'subStr',
25+
'substring' => 'subStr',
2426
'str2list' => 'explode',
2527
'str2array' => 'explode',
2628
'string2list' => 'explode',
@@ -32,6 +34,8 @@ trait DataFiltersTrait
3234
'str2lower' => 'lowercase',
3335
'strToLower' => 'lowercase',
3436
'clearNl' => 'clearNewline',
37+
'str2time' => 'strToTime',
38+
'strtotime' => 'strToTime',
3539
];
3640

3741
/**

src/Utils/ErrorMessageTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ public function getTranslates(): array
471471
public function getTranslate(string $field): string
472472
{
473473
$trans = $this->getTranslates();
474+
474475
if (isset($trans[$field])) {
475476
return $trans[$field];
476477
}

0 commit comments

Comments
 (0)