Skip to content

Commit ffcebfd

Browse files
committed
update
1 parent d85b537 commit ffcebfd

File tree

10 files changed

+248
-33
lines changed

10 files changed

+248
-33
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,10 @@ public function get(string $key, $default = null)
514514
`intList` | 验证字段值是否是一个 int list | `['tagIds', 'intList']`
515515
`strList` | 验证字段值是否是一个 string list | `['tags', 'strList']`
516516
`size/range` | 验证大小范围, 可以支持验证 `int`, `string`, `array` 数据类型 | `['tagId', 'size', 'min'=>4, 'max'=>567]`
517-
`length` | 长度验证( 跟 `size`差不多, 但只能验证 `string`, `array` 的长度 | ....
517+
`length` | 长度验证( 跟 `size`差不多, 但只能验证 `string`, `array` 的长度 | `['username', 'length', 'min' => 5, 'max' => 20]`
518518
`min` | 最小边界值验证 | `['title', 'min', 40]`
519519
`max` | 最大边界值验证 | `['title', 'max', 40]`
520+
`mustBe` | 必须是等于给定值 | `['status', 'mustBe', 0]`
520521
`in` | 枚举验证 | `['status', 'in', [1,2,3]`
521522
`notIn` | 枚举验证 | `['status', 'notIn', [4,5,6]]`
522523
`required` | 要求此字段/属性是必须的 | `['tagId, userId', 'required' ]`

examples/field.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
require __DIR__ . '/simple-loader.php';
4+
5+
$data = [
6+
// 'userId' => 234,
7+
'userId' => 'is not an integer',
8+
'tagId' => '234535',
9+
// 'freeTime' => '1456767657', // filed not exists
10+
'note' => '',
11+
'name' => 'Ajohn',
12+
'existsField' => 'test',
13+
'passwd' => 'password',
14+
'repasswd' => 'repassword',
15+
'insertTime' => '1456767657',
16+
'goods' => [
17+
'apple' => 34,
18+
'pear' => 50,
19+
],
20+
];
21+
22+
$rules = [
23+
['userId', 'required|int'],
24+
['tagId', 'size:0,50'],
25+
];
26+
27+
echo "\n----------------------------\n use FieldValidation\n----------------------------\n\n";
28+
29+
$v = \Inhere\Validate\FieldValidation::make($data, $rules)
30+
->setTranslates([
31+
'goods.pear' => '梨子'
32+
])
33+
->setMessages([
34+
'freeTime.required' => 'freeTime is required!!!!'
35+
])
36+
->validate([], false);
37+
38+
print_r($v->getErrors());

examples/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
'name' => 'Ajohn',
1212
'existsField' => 'test',
1313
'passwd' => 'password',
14-
'repasswd' => 'repassword',
14+
'repasswd' => 'password',
1515
'insertTime' => '1456767657',
1616
'goods' => [
1717
'apple' => 34,

src/FieldValidation.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,26 @@
1212
* Class FieldValidation
1313
* - one field to many rules. like Laravel framework
1414
* ```php
15-
* [
16-
* ['field', 'required|string:5,10|...', ...],
17-
* ['field0', ['required', 'string:5,10'], ...],
18-
* ['field1', 'rule1|rule2|...', ...],
19-
* ['field2', 'rule1|rule3|...', ...],
20-
* ]
15+
* $vd = FieldValidation::make($data, $rules, ...);
16+
* $vd->validate();
2117
* ```
2218
* @package Inhere\Validate
2319
*/
2420
class FieldValidation extends AbstractValidation
2521
{
22+
/**
23+
* @return array
24+
*/
25+
public function rules()
26+
{
27+
return [
28+
// ['field', 'required|string:5,10|...', ...],
29+
// ['field0', ['required', 'string:5,10'], ...],
30+
// ['field1', 'rule1|rule2|...', ...],
31+
// ['field2', 'rule1|rule3|...', ...],
32+
];
33+
}
34+
2635
protected function collectRules()
2736
{
2837
$scene = $this->scene;
@@ -39,14 +48,11 @@ protected function collectRules()
3948
throw new \InvalidArgumentException('The field validators must be is a validator name(s) string! position: rule[1].');
4049
}
4150

42-
// global rule.
43-
if (empty($rule['on'])) {
44-
// $this->_availableRules[] = $rule;
45-
// only use to special scene.
46-
} else {
51+
// an rule for special sence.
52+
if (!empty($rule['on'])) {
4753
$sceneList = is_string($rule['on']) ? array_map('trim', explode(',', $rule['on'])) : (array)$rule['on'];
4854

49-
if (!in_array($scene, $sceneList, true)) {
55+
if ($scene && !in_array($scene, $sceneList, true)) {
5056
continue;
5157
}
5258

src/Utils/ErrorMessage.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ class ErrorMessage
4949
'{attr} must be an integer/string/array and minimum value/length is {min}',
5050
'{attr} must be an integer/string/array and value/length range {min} ~ {max}',
5151
],
52+
'between' => [
53+
'{attr} between validation is not through!',
54+
'{attr} must be an integer/string/array and minimum value/length is {min}',
55+
'{attr} must be an integer/string/array and value/length between {min} ~ {max}',
56+
],
5257
'min' => '{attr} minimum boundary is {value0}',
5358
'max' => '{attr} maximum boundary is {value0}',
5459
'in' => '{attr} must in ({value0})',
@@ -59,13 +64,17 @@ class ErrorMessage
5964
'{attr} must be a string and length range must be {min} ~ {max}',
6065
],
6166
'regexp' => '{attr} does not match the {value0} conditions',
67+
6268
'compare' => '{attr} must be equals to {value0}',
6369
'same' => '{attr} must be equals to {value0}',
70+
'equal' => '{attr} must be equals to {value0}',
71+
6472
'isArray' => '{attr} must be an array',
6573
'isMap' => '{attr} must be an array and is key-value format',
6674
'isList' => '{attr} must be an array of nature',
6775
'intList' => '{attr} must be an array and value is all integers',
6876
'strList' => '{attr} must be an array and value is all strings',
77+
6978
'json' => '{attr} must be an json string',
7079
'callback' => '{attr} don\'t pass the test and verify!',
7180
'_' => '{attr} validation is not through!',

src/Validation.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@
2424
*/
2525
class Validation extends AbstractValidation
2626
{
27+
/**
28+
* @return array
29+
*/
30+
public function rules()
31+
{
32+
return [
33+
// ['fields', 'validator', arg0, arg1, something ...]
34+
// ['tagId,userId,name,email,freeTime', 'required'],
35+
// ['userId', 'number'],
36+
];
37+
}
38+
2739
/**
2840
* @param string $key
2941
* @param null $value

src/ValidationTrait.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ trait ValidationTrait
2828
*/
2929
private static $_validators = [];
3030

31+
private $_filters = [];
32+
3133
/**
3234
* current scenario name
3335
* 当前验证的场景 -- 如果需要让规则列表在多个类似情形下使用
@@ -101,9 +103,7 @@ trait ValidationTrait
101103
*/
102104
public function rules()
103105
{
104-
return [
105-
// ['fields', 'validator', 'arg1', 'arg2' ...]
106-
];
106+
return [];
107107
}
108108

109109
/**
@@ -427,7 +427,7 @@ protected function collectRules()
427427
} else {
428428
$sceneList = is_string($rule['on']) ? array_map('trim', explode(',', $rule['on'])) : (array)$rule['on'];
429429

430-
if (!in_array($scene, $sceneList, true)) {
430+
if ($scene && !in_array($scene, $sceneList, true)) {
431431
continue;
432432
}
433433

@@ -460,7 +460,31 @@ protected function collectSafeValue($attr, $value)
460460
}
461461
}
462462

463-
//////////////////////////////////// error info ////////////////////////////////////
463+
/*******************************************************************************
464+
* Filters
465+
******************************************************************************/
466+
467+
/**
468+
* @param mixed $val
469+
* @param mixed $compareVal
470+
* @return bool
471+
*/
472+
public function compare($val, $compareField)
473+
{
474+
return $compareField && ($val === $this->get($compareField));
475+
}
476+
public function same($val, $compareField)
477+
{
478+
return $this->compare($val, $compareField);
479+
}
480+
public function equal($val, $compareField)
481+
{
482+
return $this->compare($val, $compareField);
483+
}
484+
485+
/*******************************************************************************
486+
* Errors
487+
******************************************************************************/
464488

465489
/**
466490
* @return $this

src/ValidatorList.php

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,43 @@
1010
namespace Inhere\Validate;
1111

1212
use Inhere\Validate\Utils\Helper;
13+
use Inhere\Validate\Filter\FilterList;
1314

1415
/**
1516
* Class ValidatorList
1617
* @package Inhere\Validate
1718
*/
1819
final class ValidatorList
1920
{
21+
22+
/*******************************************************************************
23+
* Filters
24+
******************************************************************************/
25+
26+
/**
27+
* trim filter
28+
* @param string $val
29+
* @return string
30+
*/
31+
public static function trim($val)
32+
{
33+
return is_string($val) ? trim($val) : $val;
34+
}
35+
36+
/**
37+
* trim filter
38+
* @param string $val
39+
* @return string
40+
*/
41+
public static function toInt($val)
42+
{
43+
return FilterList::int($val);
44+
}
45+
46+
/*******************************************************************************
47+
* Validators
48+
******************************************************************************/
49+
2050
/**
2151
* 值是否为空判断
2252
* @param mixed $val
@@ -181,16 +211,32 @@ public static function size($val, $min = null, $max = null)
181211
return self::integer($val, $options);
182212
}
183213

214+
public static function between($val, $min = null, $max = null)
215+
{
216+
return self::size($val, $min, $max);
217+
}
218+
184219
public static function range($val, $min = null, $max = null)
185220
{
186221
return self::size($val, $min, $max);
187222
}
188223

224+
/**
225+
* 必须是等于给定值
226+
* @param mixed $val
227+
* @param mixed $excepted
228+
* @return bool
229+
*/
230+
public static function mustBe($val, $excepted)
231+
{
232+
return $val === $excepted;
233+
}
234+
189235
/**
190236
* 最小值检查
191237
* @param int $val
192238
* @param integer $minRange
193-
* @return mixed
239+
* @return bool
194240
*/
195241
public static function min($val, $minRange)
196242
{
@@ -201,7 +247,7 @@ public static function min($val, $minRange)
201247
* 最大值检查
202248
* @param int $val
203249
* @param int $maxRange
204-
* @return mixed
250+
* @return bool
205251
*/
206252
public static function max($val, $maxRange)
207253
{
@@ -213,7 +259,7 @@ public static function max($val, $maxRange)
213259
* @param string|array $val 字符串/数组
214260
* @param integer $minLength 最小长度
215261
* @param int $maxLength 最大长度
216-
* @return mixed
262+
* @return bool
217263
*/
218264
public static function length($val, $minLength = 0, $maxLength = null)
219265
{
@@ -545,16 +591,6 @@ public static function compare($val, $compareVal)
545591
return $val === $compareVal;
546592
}
547593

548-
/**
549-
* @param mixed $val
550-
* @param mixed $compareVal
551-
* @return bool
552-
*/
553-
public static function same($val, $compareVal)
554-
{
555-
return $val === $compareVal;
556-
}
557-
558594
/**
559595
* 校验字段值是否是日期格式
560596
* @param string $date 日期

tests/FieldValidationTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
4+
use PHPUnit\Framework\TestCase;
5+
use Inhere\Validate\FieldValidation;
6+
7+
/**
8+
* @covers FieldValidation
9+
*/
10+
class FieldValidationTest extends TestCase
11+
{
12+
public $data = [
13+
// 'userId' => 234,
14+
'userId' => 'is not an integer',
15+
'tagId' => '234535',
16+
// 'freeTime' => '1456767657', // filed not exists
17+
'note' => '',
18+
'name' => 'Ajohn',
19+
'existsField' => 'test',
20+
'passwd' => 'password',
21+
'repasswd' => 'repassword',
22+
'insertTime' => '1456767657',
23+
'goods' => [
24+
'apple' => 34,
25+
'pear' => 50,
26+
],
27+
];
28+
29+
public function testValidate()
30+
{
31+
$rules = [
32+
''
33+
];
34+
}
35+
}

0 commit comments

Comments
 (0)