Skip to content

Commit a08b48f

Browse files
committed
update some logic. split global and current instance user validators
1 parent ca27731 commit a08b48f

12 files changed

+482
-173
lines changed

src/FieldValidation.php

Lines changed: 3 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Inhere\Validate;
1010

1111
use Inhere\Validate\Filter\Filters;
12+
use Inhere\Validate\Traits\StringRulesTrait;
1213

1314
/**
1415
* Class FieldValidation
@@ -21,6 +22,8 @@
2122
*/
2223
class FieldValidation extends AbstractValidation
2324
{
25+
use StringRulesTrait;
26+
2427
/*
2528
public function rules()
2629
{
@@ -32,101 +35,4 @@ public function rules()
3235
];
3336
}
3437
*/
35-
36-
/**
37-
* @return \Generator
38-
* @throws \InvalidArgumentException
39-
*/
40-
protected function collectRules()
41-
{
42-
$scene = $this->scene;
43-
44-
// 循环规则, 搜集当前场景可用的规则
45-
foreach ($this->getRules() as $rule) {
46-
// check field
47-
if (!isset($rule[0]) || !$rule[0]) {
48-
throw new \InvalidArgumentException('Please setting the field(string) to wait validate! position: rule[0].');
49-
}
50-
51-
// check validators
52-
if (!isset($rule[1]) || !$rule[1]) {
53-
throw new \InvalidArgumentException('The field validators must be is a validator name(s) string! position: rule[1].');
54-
}
55-
56-
// an rule for special scene.
57-
if (!empty($rule['on'])) {
58-
if (!$scene) {
59-
continue;
60-
}
61-
62-
$sceneList = \is_string($rule['on']) ? Filters::explode($rule['on']) : (array)$rule['on'];
63-
64-
if (!\in_array($scene, $sceneList, true)) {
65-
continue;
66-
}
67-
68-
unset($rule['on']);
69-
}
70-
71-
$this->_usedRules[] = $rule;
72-
// field
73-
$field = \array_shift($rule);
74-
75-
// if is a Closure
76-
if (\is_object($rule[0])) {
77-
yield $field => $rule;
78-
} else {
79-
// 'required|string:5,10;' OR 'required|in:5,10'
80-
$rules = \is_array($rule[0]) ? $rule[0] : \array_map('trim', \explode('|', $rule[0]));
81-
82-
foreach ($rules as $aRule) {
83-
$rule = $this->parseRule($aRule, $rule);
84-
yield $field => $rule;
85-
}
86-
}
87-
}
88-
}
89-
90-
/**
91-
* @param string $rule
92-
* @param array $row
93-
* @return array
94-
*/
95-
protected function parseRule(string $rule, array $row): array
96-
{
97-
$rule = \trim($rule, ': ');
98-
99-
if (false === \strpos($rule, ':')) {
100-
$row[0] = $rule;
101-
return $row;
102-
}
103-
104-
list($name, $args) = Filters::explode($rule, ':', 2);
105-
$args = \trim($args, ', ');
106-
$row[0] = $name;
107-
108-
switch ($name) {
109-
case 'in':
110-
case 'enum':
111-
case 'ontIn':
112-
$row[] = Filters::explode($args);
113-
break;
114-
115-
case 'size':
116-
case 'range':
117-
case 'string':
118-
case 'between':
119-
if (\strpos($args, ',')) {
120-
list($row['min'], $row['max']) = Filters::explode($args, ',', 2);
121-
} else {
122-
$row['min'] = $args;
123-
}
124-
break;
125-
default:
126-
$row = \array_merge($row, Filters::explode($args));
127-
break;
128-
}
129-
130-
return $row;
131-
}
13238
}

src/Filter/UserFilters.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2019-01-20
6+
* Time: 00:10
7+
*/
8+
9+
namespace Inhere\Validate\Filter;
10+
11+
/**
12+
* Class UserFilters - user custom add global filters
13+
* @package Inhere\Validate\Filter
14+
*/
15+
final class UserFilters
16+
{
17+
/** @var array user custom filters */
18+
private static $filters = [];
19+
20+
/**
21+
* @param string $name
22+
* @param callable $filter
23+
*/
24+
public static function add(string $name, callable $filter)
25+
{
26+
if (!isset(self::$filters[$name])) {
27+
self::$filters[$name] = $filter;
28+
}
29+
}
30+
31+
/**
32+
* @param string $name
33+
* @param callable $filter
34+
*/
35+
public static function set(string $name, callable $filter)
36+
{
37+
self::$filters[$name] = $filter;
38+
}
39+
40+
/**
41+
* @param string $name
42+
* @return bool
43+
*/
44+
public static function has(string $name): bool
45+
{
46+
return isset(self::$filters[$name]);
47+
}
48+
49+
/**
50+
* @return array
51+
*/
52+
public static function getFilters(): array
53+
{
54+
return self::$filters;
55+
}
56+
57+
/**
58+
* @param array $filters
59+
*/
60+
public static function addFilters(array $filters)
61+
{
62+
self::$filters = \array_merge(self::$filters, $filters);
63+
}
64+
65+
/**
66+
* @param array $filters
67+
*/
68+
public static function setFilters(array $filters)
69+
{
70+
self::$filters = $filters;
71+
}
72+
73+
/**
74+
* @param string $name
75+
*/
76+
public static function remove(string $name)
77+
{
78+
if (isset(self::$filters[$name])) {
79+
unset(self::$filters[$name]);
80+
}
81+
}
82+
83+
/**
84+
* clear all filters
85+
*/
86+
public static function removeAll()
87+
{
88+
self::$filters = [];
89+
}
90+
}

src/Traits/StringRulesTrait.php

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2019-01-20
6+
* Time: 00:34
7+
*/
8+
9+
namespace Inhere\Validate\Traits;
10+
11+
use Inhere\Validate\Filter\Filters;
12+
13+
/**
14+
* Trait StringRulesTrait - Add rules like Laravel
15+
* @package Inhere\Validate\Traits
16+
*/
17+
trait StringRulesTrait
18+
{
19+
/**
20+
* Add rules like Laravel
21+
* @return array
22+
*/
23+
/*public function rules()
24+
{
25+
return [
26+
['field', 'required|string:5,10|...', ...],
27+
['field0', ['required', 'string:5,10'], ...],
28+
['field1', 'rule1|rule2|...', ...],
29+
['field2', 'rule1|rule3|...', ...],
30+
];
31+
}
32+
*/
33+
34+
/**
35+
* @return \Generator
36+
* @throws \InvalidArgumentException
37+
*/
38+
protected function collectRules()
39+
{
40+
$scene = $this->scene;
41+
42+
// 循环规则, 搜集当前场景可用的规则
43+
foreach ($this->getRules() as $rule) {
44+
// check field
45+
if (!isset($rule[0]) || !$rule[0]) {
46+
throw new \InvalidArgumentException('Please setting the field(string) to wait validate! position: rule[0].');
47+
}
48+
49+
// check validators
50+
if (!isset($rule[1]) || !$rule[1]) {
51+
throw new \InvalidArgumentException('The field validators must be is a validator name(s) string! position: rule[1].');
52+
}
53+
54+
// an rule for special scene.
55+
if (!empty($rule['on'])) {
56+
if (!$scene) {
57+
continue;
58+
}
59+
60+
$sceneList = \is_string($rule['on']) ? Filters::explode($rule['on']) : (array)$rule['on'];
61+
62+
if (!\in_array($scene, $sceneList, true)) {
63+
continue;
64+
}
65+
66+
unset($rule['on']);
67+
}
68+
69+
$this->_usedRules[] = $rule;
70+
// field
71+
$field = \array_shift($rule);
72+
73+
// if is a Closure
74+
if (\is_object($rule[0])) {
75+
yield $field => $rule;
76+
} else {
77+
// 'required|string:5,10;' OR 'required|in:5,10'
78+
$rules = \is_array($rule[0]) ? $rule[0] : \array_map('trim', \explode('|', $rule[0]));
79+
80+
foreach ($rules as $aRule) {
81+
$rule = $this->parseRule($aRule, $rule);
82+
yield $field => $rule;
83+
}
84+
}
85+
}
86+
}
87+
88+
/**
89+
* @param string $rule
90+
* @param array $row
91+
* @return array
92+
*/
93+
protected function parseRule(string $rule, array $row): array
94+
{
95+
$rule = \trim($rule, ': ');
96+
97+
if (false === \strpos($rule, ':')) {
98+
$row[0] = $rule;
99+
return $row;
100+
}
101+
102+
list($name, $args) = Filters::explode($rule, ':', 2);
103+
$args = \trim($args, ', ');
104+
$row[0] = $name;
105+
106+
switch ($name) {
107+
case 'in':
108+
case 'enum':
109+
case 'ontIn':
110+
$row[] = Filters::explode($args);
111+
break;
112+
113+
case 'size':
114+
case 'range':
115+
case 'string':
116+
case 'between':
117+
if (\strpos($args, ',')) {
118+
list($row['min'], $row['max']) = Filters::explode($args, ',', 2);
119+
} else {
120+
$row['min'] = $args;
121+
}
122+
break;
123+
default:
124+
$row = \array_merge($row, Filters::explode($args));
125+
break;
126+
}
127+
128+
return $row;
129+
}
130+
}

src/Utils/ErrorMessageTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,8 @@ public function getMessage($validator, string $field, array $args = [], $message
402402
];
403403

404404
foreach ($args as $key => $value) {
405-
$key = \is_int($key) ? "value{$key}" : $key;
405+
$key = \is_int($key) ? "value{$key}" : $key;
406+
// build params
406407
$params['{' . $key . '}'] = \is_array($value) ? \implode(',', $value) : $value;
407408
}
408409

src/Utils/Helper.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,48 @@ public static function call($cb, ...$args)
214214

215215
throw new \InvalidArgumentException('The parameter is not a callable');
216216
}
217+
218+
/**
219+
* compare of size
220+
* - int Compare size
221+
* - string Compare length
222+
* - array Compare length
223+
* @param mixed $val
224+
* @param mixed $expected
225+
* @param string $operator
226+
* @return bool
227+
*/
228+
public static function compareSize($val, string $operator, $expected): bool
229+
{
230+
// type must be same
231+
if (\gettype($val) !== \gettype($expected)) {
232+
return false;
233+
}
234+
235+
// not in: int, string, array
236+
if (($len = self::length($val)) < 0) {
237+
return false;
238+
}
239+
240+
$wantLen = self::length($expected);
241+
242+
switch ($operator) {
243+
case '>':
244+
$ok = $len > $wantLen;
245+
break;
246+
case '>=':
247+
$ok = $len >= $wantLen;
248+
break;
249+
case '<':
250+
$ok = $len < $wantLen;
251+
break;
252+
case '<=':
253+
$ok = $len <= $wantLen;
254+
break;
255+
default:
256+
$ok = false;
257+
}
258+
259+
return $ok;
260+
}
217261
}

0 commit comments

Comments
 (0)