Skip to content

Commit d30a96c

Browse files
committed
rename class name, update readme
1 parent 27bfd78 commit d30a96c

12 files changed

+1830
-1705
lines changed

README.md

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ $v = Validation::make($_POST,[
489489
<a name="built-in-filters"></a>
490490
## 内置的过滤器
491491

492-
> 一些 php 内置的函数可直接使用。 e.g `trim|ucfirst` `json_decode`
492+
> 一些 php 内置的函数可直接使用。 e.g `trim|ucfirst` `json_decode` `md5`
493493
494494
过滤器 | 说明 | 示例
495495
-------|-------------|------------
@@ -507,6 +507,7 @@ $v = Validation::make($_POST,[
507507
`timestamp/strToTime` | 字符串日期转换时间戳 | `['pulishedAt', 'number', 'filter' => 'strToTime'],`
508508
`url` | URL 过滤,移除所有不符合 URL 的字符 | `['field', 'url', 'filter' => 'url'],`
509509
`str2list/str2array` | 字符串转数组 `'tag0,tag1' -> ['tag0', 'tag1']` | `['tags', 'strList', 'filter' => 'str2array'],`
510+
`unique` | 去除数组中的重复值(by `array_unique()`) | `['tagIds', 'intList', 'filter' => 'unique'],`
510511
`email` | email 过滤,移除所有不符合 email 的字符 | `['field', 'email', 'filter' => 'email'],`
511512
`encoded` | 去除 URL 编码不需要的字符,与 `urlencode()` 函数很类似 | `['imgUrl', 'url', 'filter' => 'encoded'],`
512513
`clearSpace` | 清理空格 | `['title', 'string', 'filter' => 'clearSpace'],`
@@ -528,7 +529,7 @@ $v = Validation::make($_POST,[
528529
`bool/boolean` | 验证是否是 bool. [关于bool值](#about-bool-value) | `['open', 'bool']`
529530
`float` | 验证是否是 float | `['price', 'float']`
530531
`string` | 验证是否是 string. **支持长度检查** | `['name', 'string']`, `['name', 'string', 'min'=>4, 'max'=>16]`
531-
`accepted` | 验证的字段必须为 `yes/on/1/true` 这在确认「服务条款」是否同意时有用(from laravel) | `['agree', 'accepted']`
532+
`accepted` | 验证的字段必须为 `yes/on/1/true` 这在确认「服务条款」是否同意时有用(ref laravel) | `['agree', 'accepted']`
532533
`url` | 验证是否是 url | `['myUrl', 'url']`
533534
`email` | 验证是否是 email | `['userEmail', 'email']`
534535
`alpha` | 验证值是否仅包含字母字符 | `['name', 'alpha']`
@@ -558,18 +559,18 @@ $v = Validation::make($_POST,[
558559
`notBe` | 不能等于给定值 | `['status', 'notBe', 0]`
559560
`compare/same/equal` | 字段值比较: 相同 | `['passwd', 'compare', 'repasswd']`
560561
`different/notEqual` | 字段值比较: 不能相同 | `['userId', 'notEqual', 'targetId']`
561-
`requiredIf` | 指定的其它字段( anotherField )值等于任何一个 `value` 时,此字段为 **必填** | `['city', 'requiredIf', 'myCity', ['chengdu'] ]`
562-
`requiredUnless` | 指定的其它字段( anotherField )值等于任何一个 `value` 时,此字段为 **不必填** | `['city', 'requiredUnless', 'myCity', ['chengdu'] ]`
563-
`requiredWith` | 指定的字段中的 _任意一个_ 有值且不为空,则此字段为 **必填** | `['city', 'requiredWith', ['myCity'] ]`
564-
`requiredWithAll` | 如果指定的 _所有字段_ 都有值,则此字段为 **必填** | `['city', 'requiredWithAll', ['myCity', 'myCity1'] ]`
565-
`requiredWithout` | 如果缺少 _任意一个_ 指定的字段值,则此字段为 **必填** | `['city', 'requiredWithout', ['myCity', 'myCity1'] ]`
566-
`requiredWithoutAll` | 如果所有指定的字段 **都没有值**,则此字段为 **必填** | `['city', 'requiredWithoutAll', ['myCity', 'myCity1'] ]`
562+
`requiredIf` | 指定的其它字段( anotherField )值等于任何一个 `value` 时,此字段为 **必填**(ref laravel) | `['city', 'requiredIf', 'myCity', ['chengdu'] ]`
563+
`requiredUnless` | 指定的其它字段( anotherField )值等于任何一个 `value` 时,此字段为 **不必填**(ref laravel) | `['city', 'requiredUnless', 'myCity', ['chengdu'] ]`
564+
`requiredWith` | 指定的字段中的 _任意一个_ 有值且不为空,则此字段为 **必填**(ref laravel) | `['city', 'requiredWith', ['myCity'] ]`
565+
`requiredWithAll` | 如果指定的 _所有字段_ 都有值,则此字段为 **必填**(ref laravel) | `['city', 'requiredWithAll', ['myCity', 'myCity1'] ]`
566+
`requiredWithout` | 如果缺少 _任意一个_ 指定的字段值,则此字段为 **必填**(ref laravel) | `['city', 'requiredWithout', ['myCity', 'myCity1'] ]`
567+
`requiredWithoutAll` | 如果所有指定的字段 **都没有值**,则此字段为 **必填**(ref laravel) | `['city', 'requiredWithoutAll', ['myCity', 'myCity1'] ]`
567568
`date` | 验证是否是 date | `['publishedAt', 'date']`
568569
`dateFormat` | 验证是否是 date, 并且是指定的格式 | `['publishedAt', 'dateFormat', 'Y-m-d']`
569570
`dateEquals` | 验证是否是 date, 并且是否是等于给定日期 | `['publishedAt', 'dateEquals', '2017-05-12']`
570-
`beforeDate` | 验证字段值必须是给定日期之前的值 | `['publishedAt', 'beforeDate', '2017-05-12']`
571-
`beforeOrEqualDate` | 字段值必须是小于或等于给定日期的值 | `['publishedAt', 'beforeOrEqualDate', '2017-05-12']`
572-
`afterOrEqualDate` | 字段值必须是大于或等于给定日期的值 | `['publishedAt', 'afterOrEqualDate', '2017-05-12']`
571+
`beforeDate` | 验证字段值必须是给定日期之前的值(ref laravel) | `['publishedAt', 'beforeDate', '2017-05-12']`
572+
`beforeOrEqualDate` | 字段值必须是小于或等于给定日期的值(ref laravel) | `['publishedAt', 'beforeOrEqualDate', '2017-05-12']`
573+
`afterOrEqualDate` | 字段值必须是大于或等于给定日期的值(ref laravel) | `['publishedAt', 'afterOrEqualDate', '2017-05-12']`
573574
`afterDate` | 验证字段值必须是给定日期之前的值 | `['publishedAt', 'afterDate', '2017-05-12']`
574575
`json` | 验证是否是json字符串(默认严格验证,必须以`{` `[` 开始) | `['goods', 'json']` `['somedata', 'json', false]` - 非严格,普通字符串`eg 'test'`也会通过
575576
`file` | 验证是否是上传的文件 | `['upFile', 'file']`
@@ -594,7 +595,28 @@ $v = Validation::make($_POST,[
594595
['createdAt, updatedAt', 'safe']
595596
```
596597

597-
### 关于文件验证
598+
### 一些补充说明
599+
600+
<a name="about-empty-value"></a>
601+
#### 关于为空判断
602+
603+
字段符合下方任一条件时即为「空」
604+
605+
- 该值为 `null`.
606+
- 该值为空字符串 `''`
607+
- 该值为空数组 `[]`
608+
- 该值为空对象 -- 空的 `可数` 对象
609+
- 该值为没有路径的上传文件
610+
611+
<a name="about-bool-value"></a>
612+
#### 关于布尔值
613+
614+
值符合下列的任意一项即认为是为bool值(不区分大小写)
615+
616+
- 是 "1"、"true"、"on" 和 "yes" (`TRUE`)
617+
- 是 "0"、"false"、"off"、"no" 和 "" (`FALSE`)
618+
619+
#### 关于文件验证
598620

599621
文件验证时注意要设置文件信息源数据
600622

@@ -609,18 +631,10 @@ $v = Validation::make($_POST, [
609631
// ...
610632
```
611633

612-
### (注意)一些补充说明
634+
#### 提示和注意
613635

614636
- **请将 `required*` 系列规则写在规则列表的最前面**
615-
- 关于为空判断:字段符合下方任一条件时即为「空」<a name="about-empty-value"></a>
616-
- 该值为 `null`.
617-
- 该值为空字符串 `''`
618-
- 该值为空数组 `[]`
619-
- 该值为空对象 -- 空的 `可数` 对象
620-
- 该值为没有路径的上传文件
621-
- 关于布尔值: 值符合下列的任意一项即认为是为bool值<a name="about-bool-value"></a>
622-
- 是 "1"、"true"、"on" 和 "yes" (`TRUE`)
623-
- 是 "0"、"false"、"off"、"no" 和 "" (`FALSE`)
637+
- 验证大小范围 `int` 是比较大小。 `string``array` 是检查长度。大小范围 是包含边界值的
624638
- `size/range` `length` 可以只定义 `min` 或者 `max`
625639
- 支持对数组的子级值验证
626640

@@ -639,8 +653,31 @@ $v = Validation::make($_POST, [
639653
['goods.pear', 'max', 30], //goods 下的 pear 值最大不能超过 30
640654
```
641655

642-
- `required*` 系列规则参考自 laravel
643-
- 验证大小范围 `int` 是比较大小。 `string``array` 是检查长度。大小范围 是包含边界值的
656+
- 支持对数组的子级值进行遍历验证
657+
658+
```php
659+
[
660+
'goods' => [
661+
'apple' => 34,
662+
'pear' => 50,
663+
],
664+
'users' => [
665+
['id' => 34, 'name' => 'tom'],
666+
['id' => 89, 'name' => 'john'],
667+
]
668+
]
669+
```
670+
671+
```php
672+
['goods.*', 'each', 'number'], //goods 下的 每个值 都必须为大于0 的整数
673+
// 写法是等效的
674+
// ['goods', 'each', 'number'], //goods 下的 每个值 都必须为大于0 的整数
675+
676+
// 多维数组
677+
['users.*.id', 'each', 'required'],
678+
['users.*.id', 'each', 'number', 'min' => 34],
679+
['users.*.name', 'each', 'string', 'min' => 5],
680+
```
644681

645682
## 一些关键方法API
646683

src/Filter/FilterList.php renamed to src/Filter/Filters.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,18 @@
99
use Inhere\Validate\Utils\Helper;
1010

1111
/**
12-
* Class FilterList
12+
* Class Filters
1313
* @package Inhere\Validate\Filter
1414
*/
15-
final class FilterList
15+
final class Filters
1616
{
17+
/**
18+
* don't allow create instance.
19+
*/
20+
private function __construct()
21+
{
22+
}
23+
1724
/**
1825
* 布尔值验证,转换成字符串后是下列的一个,就认为他是个bool值
1926
* - "1"、"true"、"on" 和 "yes" (equal TRUE)
@@ -35,7 +42,7 @@ public static function boolean($val, $nullAsFalse = false)
3542
}
3643

3744
/**
38-
* @see ValidatorList::boolean()
45+
* @see Validators::boolean()
3946
* {@inheritdoc}
4047
*/
4148
public static function bool($val, $nullAsFalse = false)
@@ -59,7 +66,7 @@ public static function integer($val)
5966
}
6067

6168
/**
62-
* @see FilterList::integer()
69+
* @see Filters::integer()
6370
* {@inheritdoc}
6471
*/
6572
public static function int($val)
@@ -133,7 +140,7 @@ public static function string($val, $flags = 0)
133140
}
134141

135142
/**
136-
* @see FilterList::string()
143+
* @see Filters::string()
137144
* {@inheritdoc}
138145
*/
139146
public static function stripped($val, $flags = 0)
@@ -520,4 +527,18 @@ public static function callback($val, $callback)
520527
{
521528
return filter_var($val, FILTER_CALLBACK, ['options' => $callback]);
522529
}
530+
531+
/**
532+
* 去除数组中的重复值
533+
* @param mixed $val
534+
* @return array
535+
*/
536+
public static function unique($val)
537+
{
538+
if (!$val || \is_array($val)) {
539+
return $val;
540+
}
541+
542+
return array_unique($val);
543+
}
523544
}

src/Utils/DataFiltersTrait.php

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

99
namespace Inhere\Validate\Utils;
1010

11-
use Inhere\Validate\Filter\FilterList;
11+
use Inhere\Validate\Filter\Filters;
1212

1313
/**
1414
* Trait DataFiltersTrait
@@ -86,8 +86,8 @@ protected function callStringCallback($filter, ...$args)
8686
$value = $this->$filter(...$args);
8787

8888
// $filter is a method of the class 'FilterList'
89-
} elseif (method_exists(FilterList::class, $filter)) {
90-
$value = FilterList::$filter(...$args);
89+
} elseif (method_exists(Filters::class, $filter)) {
90+
$value = Filters::$filter(...$args);
9191

9292
// it is function name
9393
} elseif (\function_exists($filter)) {

src/Utils/Rule.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
*/
1515
final class Rule
1616
{
17+
/**
18+
* @var string
19+
*/
20+
public $field;
21+
1722
/**
1823
* validator name OR validator object
1924
* @var string|callable
@@ -30,11 +35,22 @@ final class Rule
3035
*/
3136
public $when;
3237

38+
/**
39+
* @var mixed
40+
*/
41+
public $value;
42+
3343
/**
3444
* @var mixed
3545
*/
3646
public $default;
3747

48+
/**
49+
* default error message
50+
* @var mixed
51+
*/
52+
public $message;
53+
3854
/**
3955
* check Empty
4056
* @var callable
@@ -50,4 +66,30 @@ final class Rule
5066
* @var array|null
5167
*/
5268
public $filters;
69+
70+
public function initByArray(array $config)
71+
{
72+
73+
}
74+
75+
/**
76+
* @param string $field 属性名称
77+
* @param mixed $value 属性值
78+
* @param \Closure|string $validator 验证器
79+
* @param array $params 验证需要的参数
80+
* @param string $message default error message
81+
* @param mixed $default default value
82+
* @return Rule
83+
*/
84+
public function init(string $field, $value, $validator, array $params, $message, $default): Rule
85+
{
86+
$this->field = $field;
87+
$this->value = $value;
88+
$this->validator = $validator;
89+
$this->params = $params;
90+
$this->message = $message;
91+
$this->default = $default;
92+
93+
return $this;
94+
}
5395
}

src/Utils/UserAndContextValidatorsTrait.php

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

99
namespace Inhere\Validate\Utils;
1010

11-
use Inhere\Validate\ValidatorList;
11+
use Inhere\Validate\Validators;
1212

1313
/**
1414
* trait UserAndContextValidatorsTrait
@@ -133,7 +133,7 @@ public static function getValidators(): array
133133

134134
/**
135135
* 验证字段必须存在,且输入数据不为空。
136-
* @see ValidatorList::isEmpty() 如何鉴定为空
136+
* @see Validators::isEmpty() 如何鉴定为空
137137
* @param string $field
138138
* @param null|mixed $value
139139
* @return bool
@@ -151,7 +151,7 @@ public function required(string $field, $value = null)
151151
$val = $this->uploadedFiles[$field];
152152
}
153153

154-
return !ValidatorList::isEmpty($val);
154+
return !Validators::isEmpty($val);
155155
}
156156

157157
/**
@@ -458,7 +458,7 @@ public function differentValidator($val, string $compareField)
458458
public function inFieldValidator($val, string $anotherField)
459459
{
460460
if ($anotherField && $dict = $this->getByPath($anotherField)) {
461-
return ValidatorList::in($val, $dict);
461+
return Validators::in($val, $dict);
462462
}
463463

464464
return false;
@@ -477,7 +477,7 @@ public function inFieldValidator($val, string $anotherField)
477477
public function eachValidator(array $values, ...$args)
478478
{
479479
if (!$validator = array_shift($args)) {
480-
throw new \InvalidArgumentException('must setting a validator for each validate.');
480+
throw new \InvalidArgumentException('must setting a validator for \'each\' validate.');
481481
}
482482

483483
foreach ($values as $value) {
@@ -488,7 +488,7 @@ public function eachValidator(array $values, ...$args)
488488
} elseif (\is_string($validator)) {
489489
// special for required
490490
if ('required' === $validator) {
491-
$passed = !ValidatorList::isEmpty($value);
491+
$passed = !Validators::isEmpty($value);
492492

493493
} elseif (isset(self::$_validators[$validator])) {
494494
$callback = self::$_validators[$validator];
@@ -497,8 +497,8 @@ public function eachValidator(array $values, ...$args)
497497
} elseif (method_exists($this, $method = $validator . 'Validator')) {
498498
$passed = $this->$method($value, ...$args);
499499

500-
} elseif (method_exists(ValidatorList::class, $validator)) {
501-
$passed = ValidatorList::$validator($value, ...$args);
500+
} elseif (method_exists(Validators::class, $validator)) {
501+
$passed = Validators::$validator($value, ...$args);
502502

503503
// it is function name
504504
} elseif (\function_exists($validator)) {

src/ValidationTrait.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public function validate(array $onlyChecked = null, $stopOnError = null)
175175
$validator = $rule[0];
176176

177177
// 如何判断属性为空 默认使用 ValidatorList::isEmpty(). 也可自定义
178-
$isEmpty = [ValidatorList::class, 'isEmpty'];
178+
$isEmpty = [Validators::class, 'isEmpty'];
179179
if (!empty($rule['isEmpty']) && (\is_string($rule['isEmpty']) || $rule['isEmpty'] instanceof \Closure)) {
180180
$isEmpty = $rule['isEmpty'];
181181
}
@@ -258,7 +258,6 @@ public function validate(array $onlyChecked = null, $stopOnError = null)
258258
// fix : deny repeat validate
259259
$this->_validated = true;
260260

261-
unset($data);
262261
return $this;
263262
}
264263

@@ -335,8 +334,8 @@ protected function valueValidate(string $field, $value, $validator, array $args,
335334
} elseif (method_exists($this, $method = $validator . 'Validator')) {
336335
$passed = $this->$method($value, ...$args);
337336

338-
} elseif (method_exists(ValidatorList::class, $validator)) {
339-
$passed = ValidatorList::$validator($value, ...$args);
337+
} elseif (method_exists(Validators::class, $validator)) {
338+
$passed = Validators::$validator($value, ...$args);
340339

341340
// it is function name
342341
} elseif (\function_exists($validator)) {

0 commit comments

Comments
 (0)