Skip to content

Commit e75ec76

Browse files
committed
up: update some logic support deep path after wildcard
1 parent e900a44 commit e75ec76

13 files changed

+323
-100
lines changed

src/AbstractValidation.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ abstract class AbstractValidation implements ValidationInterface
3333
* @param array $rules
3434
* @param array $translates
3535
* @param string $scene
36-
* @param bool $startValidate 立即开始验证
36+
* @param bool $startValidate Start verification now
3737
*
3838
* @throws InvalidArgumentException
3939
*/
@@ -57,9 +57,9 @@ public function __construct(
5757
* @param string $scene
5858
* @param bool $startValidate
5959
*
60-
* @return AbstractValidation
60+
* @return self
6161
*/
62-
public static function quick(array $data, string $scene = '', bool $startValidate = false): AbstractValidation
62+
public static function quick(array $data, string $scene = '', bool $startValidate = false): self
6363
{
6464
return new static($data, [], [], $scene, $startValidate);
6565
}
@@ -80,7 +80,7 @@ public static function make(
8080
array $translates = [],
8181
string $scene = '',
8282
bool $startValidate = false
83-
) {
83+
): self {
8484
return new static($data, $rules, $translates, $scene, $startValidate);
8585
}
8686

@@ -95,13 +95,13 @@ public static function make(
9595
* @return static
9696
* @throws InvalidArgumentException
9797
*/
98-
public static function makeAndValidate(array $data, array $rules = [], array $translates = [], string $scene = '')
98+
public static function makeAndValidate(array $data, array $rules = [], array $translates = [], string $scene = ''): self
9999
{
100100
return new static($data, $rules, $translates, $scene, true);
101101
}
102102

103103
/**
104-
* Create and start verification immediately
104+
* Create and start verification immediately. alias of makeAndValidate()
105105
*
106106
* @param array $data
107107
* @param array $rules
@@ -111,7 +111,7 @@ public static function makeAndValidate(array $data, array $rules = [], array $tr
111111
* @return static
112112
* @throws InvalidArgumentException
113113
*/
114-
public static function check(array $data, array $rules = [], array $translates = [], string $scene = '')
114+
public static function check(array $data, array $rules = [], array $translates = [], string $scene = ''): self
115115
{
116116
return new static($data, $rules, $translates, $scene, true);
117117
}

src/FieldValidation.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ class FieldValidation extends AbstractValidation
2525
{
2626
use MultipleRulesTrait;
2727

28-
/*
28+
/* examples:
2929
public function rules()
3030
{
3131
return [
3232
['field', 'required|string:5,10|...', ...],
3333
['field0', ['required', 'string:5,10'], ...],
3434
['field1', 'rule1|rule2|...', ...],
3535
['field2', 'rule1|rule3|...', ...],
36+
['field3', function($val) {}, ...],
3637
];
3738
}
3839
*/

src/Helper.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use function function_exists;
2121
use function gettype;
2222
use function html_entity_decode;
23+
use function in_array;
2324
use function is_array;
2425
use function is_int;
2526
use function is_object;
@@ -207,6 +208,31 @@ public static function prettifyFieldName(string $field): string
207208
return strpos($str, '_') ? str_replace('_', ' ', $str) : $str;
208209
}
209210

211+
/**
212+
* @param string $scene Current scene value
213+
* @param string|array $ruleOn
214+
*
215+
* @return bool
216+
*/
217+
public static function ruleIsAvailable(string $scene, $ruleOn): bool
218+
{
219+
// - rule is not limit scene
220+
if (!$ruleOn) {
221+
return true;
222+
}
223+
224+
// - $ruleOn is not empty
225+
226+
// current scene is empty
227+
if (!$scene) {
228+
return false;
229+
}
230+
231+
$scenes = is_string($ruleOn) ? Filters::explode($ruleOn) : (array)$ruleOn;
232+
233+
return in_array($scene, $scenes, true);
234+
}
235+
210236
/**
211237
* getValueOfArray 支持以 '.' 分割进行子级值获取 eg: 'goods.apple'
212238
*
@@ -256,7 +282,7 @@ public static function call($cb, ...$args)
256282
// className::method
257283
if (strpos($cb, '::') > 0) {
258284
$cb = explode('::', $cb, 2);
259-
// function
285+
// function
260286
} elseif (function_exists($cb)) {
261287
return $cb(...$args);
262288
}

src/Traits/MultipleRulesTrait.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@
1010

1111
use Generator;
1212
use Inhere\Validate\Filter\Filters;
13+
use Inhere\Validate\Helper;
1314
use InvalidArgumentException;
1415
use function array_map;
1516
use function array_merge;
1617
use function array_shift;
1718
use function explode;
18-
use function in_array;
1919
use function is_array;
2020
use function is_object;
21-
use function is_string;
2221
use function strpos;
2322
use function trim;
2423

2524
/**
26-
* Trait MultipleRulesTrait - allow add multiple rules like Laravel.
25+
* Trait MultipleRulesTrait
26+
* - allow add multiple rules like Laravel.
2727
*
2828
* @package Inhere\Validate\Traits
2929
*/
@@ -62,17 +62,12 @@ protected function collectRules(): ?Generator
6262

6363
// check validators
6464
if (!isset($rule[1]) || !$rule[1]) {
65-
throw new InvalidArgumentException('The field validators must be is a validator name(s) string! position: rule[1]');
65+
throw new InvalidArgumentException('Please setting the validator(s) for validate field! position: rule[1]');
6666
}
6767

68-
// an rule for special scene.
69-
if (!empty($rule['on'])) {
70-
if (!$scene) {
71-
continue;
72-
}
73-
74-
$sceneList = is_string($rule['on']) ? Filters::explode($rule['on']) : (array)$rule['on'];
75-
if (!in_array($scene, $sceneList, true)) {
68+
// rule only allow use to special scene.
69+
if (isset($rule['on'])) {
70+
if (!Helper::ruleIsAvailable($scene, $rule['on'])) {
7671
continue;
7772
}
7873

@@ -106,7 +101,6 @@ protected function collectRules(): ?Generator
106101
protected function parseRule(string $rule, array $row): array
107102
{
108103
$rule = trim($rule, ': ');
109-
110104
if (false === strpos($rule, ':')) {
111105
$row[0] = $rule;
112106
return $row;

src/Valid.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Inhere\Validate;
44

55
use function array_merge;
6+
use function is_int;
7+
use function is_numeric;
68

79
/**
810
* Class Valid - Simple Data Validator
@@ -54,21 +56,46 @@ public function getData(): array
5456
*/
5557
public static function getInt(string $field, int $min = null, int $max = null, int $default = null): int
5658
{
57-
return 0;
59+
if (!isset(self::$data[$field])) {
60+
if ($default === null) {
61+
throw new ValidateException($field, 'is required and must be int');
62+
}
63+
64+
return $default;
65+
}
66+
67+
$val = self::$data[$field];
68+
if (is_numeric($val)) {
69+
$val = (int)$val;
70+
}
71+
72+
if (!is_int($val)) {
73+
throw new ValidateException($field, 'must be int value');
74+
}
75+
76+
// check min and max value
77+
if ($min !== null && $val < $min) {
78+
throw new ValidateException($field, "must be greater or equal to $min");
79+
}
80+
if ($max !== null && $val > $max) {
81+
throw new ValidateException($field, "must be less than or equal to $max");
82+
}
83+
84+
return $val;
5885
}
5986

60-
public static function getInts(string $field, int $min = null, int $max = null, int $default = 0): int
87+
public static function getInts(string $field, int $min = null, int $max = null, array $default = null): array
6188
{
62-
return 0;
89+
return [];
6390
}
6491

6592
public static function getString(string $field, int $minLen = null, int $maxLen = null, string $default = null): int
6693
{
6794
return 0;
6895
}
6996

70-
public static function getStrings(string $field, int $min = null, int $max = null, array $default = null): int
97+
public static function getStrings(string $field, int $min = null, int $max = null, array $default = null): array
7198
{
72-
return 0;
99+
return [];
73100
}
74101
}

src/ValidateException.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,33 @@
1111
*/
1212
class ValidateException extends RuntimeException
1313
{
14+
/**
15+
* @var string
16+
*/
17+
public $field;
18+
19+
/**
20+
* @param string $field
21+
* @param string $message
22+
*
23+
* @return static
24+
*/
25+
public static function create(string $field, string $message): self
26+
{
27+
return new self($field, $message);
28+
}
29+
30+
/**
31+
* Class constructor.
32+
*
33+
* @param string $field
34+
* @param string $message
35+
*/
36+
public function __construct(string $field, string $message)
37+
{
38+
parent::__construct($field . ' ' . $message, 500);
39+
40+
// save field
41+
$this->field = $field;
42+
}
1443
}

src/Validation.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,14 @@
2020
* ['userId', 'number'],
2121
* ['name', 'regexp' ,'/^[a-z]\w{2,12}$/'],
2222
* ])->validate();
23-
* $vd->fail();// bool
23+
*
24+
* $vd->isFail();// bool
2425
* $vd->firstError(); // get first error message.
25-
* $vd->passed();// bool
26+
* $vd->isOk();// bool
2627
*/
2728
class Validation extends AbstractValidation
2829
{
29-
/**
30-
* @return array
31-
*/
32-
/*
30+
/* examples:
3331
public function rules()
3432
{
3533
return [

0 commit comments

Comments
 (0)