@@ -53,25 +53,25 @@ trait ValidationTrait
53
53
* [ field2 => errorMessage3 ]
54
54
* ]
55
55
*/
56
- private $ _errors = [];
56
+ private $ _errors = [];
57
57
58
58
/**
59
59
* the rules is by setRules()
60
60
* @var array
61
61
*/
62
- private $ _rules = [];
62
+ private $ _rules = [];
63
63
64
64
/**
65
65
* available rules at current scene
66
66
* @var array
67
67
*/
68
- private $ _availableRules = [];
68
+ private $ _availableRules = [];
69
69
70
70
/**
71
71
* custom append's validator by addValidator()
72
72
* @var array
73
73
*/
74
- private $ _validators = [];
74
+ private $ _validators = [];
75
75
76
76
/**
77
77
* attribute field translate list
@@ -126,8 +126,13 @@ public function messages()
126
126
127
127
//////////////////////////////////// Validate ////////////////////////////////////
128
128
129
- public function beforeValidate (){}
130
- public function afterValidate (){}
129
+ public function beforeValidate ()
130
+ {
131
+ }
132
+
133
+ public function afterValidate ()
134
+ {
135
+ }
131
136
132
137
/**
133
138
* [ValidatorList::required] 验证是必定被调用的
@@ -166,8 +171,8 @@ public function validate(array $onlyChecked = [], $stopOnError = null)
166
171
$ skipOnEmpty = $ rule ['skipOnEmpty ' ] ?? true ;
167
172
168
173
// 如何判断属性为空 默认使用 empty($data[$attr]). 也可自定义
169
- $ isEmpty = [ ValidatorList::class, 'isEmpty ' ];
170
- if ( isset ($ rule ['isEmpty ' ]) && $ rule ['isEmpty ' ] instanceof \Closure ) {
174
+ $ isEmpty = [ValidatorList::class, 'isEmpty ' ];
175
+ if (isset ($ rule ['isEmpty ' ]) && $ rule ['isEmpty ' ] instanceof \Closure) {
171
176
$ isEmpty = $ rule ['isEmpty ' ];
172
177
}
173
178
@@ -176,7 +181,7 @@ public function validate(array $onlyChecked = [], $stopOnError = null)
176
181
177
182
// 验证的前置条件 -- 不满足条件,跳过此条规则
178
183
$ when = $ rule ['when ' ] ?? null ;
179
- if ( $ when && $ when instanceof \Closure && $ when ($ data , $ this ) !== true ) {
184
+ if ($ when && $ when instanceof \Closure && $ when ($ data , $ this ) !== true ) {
180
185
continue ;
181
186
}
182
187
@@ -192,18 +197,18 @@ public function validate(array $onlyChecked = [], $stopOnError = null)
192
197
// $skipOnEmpty is true && ValidatorList::isEmpty($data,$attr)
193
198
if (
194
199
($ onlyChecked && !in_array ($ attr , $ onlyChecked , true )) ||
195
- ( $ validator !== 'required ' && $ skipOnEmpty && call_user_func ($ isEmpty , $ data , $ attr ))
200
+ ($ validator !== 'required ' && $ skipOnEmpty && call_user_func ($ isEmpty , $ data , $ attr ))
196
201
) {
197
- continue ;
202
+ continue ;
198
203
}
199
204
200
205
// mark attribute is safe. not need validate. like. 'created_at'
201
- if ( $ validator === 'safe ' ) {
206
+ if ($ validator === 'safe ' ) {
202
207
$ this ->_safeData [$ attr ] = $ data [$ attr ];
203
208
continue ;
204
209
}
205
210
206
- [$ result ,$ validator ] = $ this ->doValidate ($ data , $ attr , $ validator , $ copy );
211
+ [$ result , $ validator ] = $ this ->doValidate ($ data , $ attr , $ validator , $ copy );
207
212
208
213
if ($ result === false ) {
209
214
$ this ->_errors [] = [
@@ -217,13 +222,13 @@ public function validate(array $onlyChecked = [], $stopOnError = null)
217
222
$ message = null ;
218
223
219
224
// There is an error an immediate end to verify
220
- if ( $ this ->hasError () && $ this ->_stopOnError ) {
225
+ if ($ this ->hasError () && $ this ->_stopOnError ) {
221
226
break ;
222
227
}
223
228
}
224
229
225
230
// fix: has error, clear safe data.
226
- if ( $ this ->hasError () ) {
231
+ if ($ this ->hasError ()) {
227
232
$ this ->_safeData = [];
228
233
}
229
234
@@ -239,18 +244,18 @@ public function validate(array $onlyChecked = [], $stopOnError = null)
239
244
* do Validate
240
245
* @param array $data 待验证的数据列表
241
246
* @param string $attr 属性名称
242
- * @param mixed $validator 验证器
247
+ * @param \Closure|string $validator 验证器
243
248
* @param array $args 验证需要的参数
244
249
* @return array
245
250
*/
246
251
protected function doValidate ($ data , $ attr , $ validator , $ args )
247
252
{
248
253
// if attr don't exists.
249
- if ( !$ this ->has ($ attr ) ) {
254
+ if (!$ this ->has ($ attr )) {
250
255
return [false , $ validator instanceof \Closure ? 'callback ' : $ validator ];
251
256
}
252
257
253
- if ( $ validator === 'required ' ) {
258
+ if ($ validator === 'required ' ) {
254
259
$ result = ValidatorList::required ($ data , $ attr );
255
260
256
261
return [$ result , $ validator ];
@@ -260,26 +265,26 @@ protected function doValidate($data, $attr, $validator, $args)
260
265
array_unshift ($ args , $ data [$ attr ]);
261
266
262
267
// if $validator is a closure
263
- if ( is_callable ($ validator ) && $ validator instanceof \Closure) {
264
- $ callback = $ validator ;
268
+ if (is_callable ($ validator ) && $ validator instanceof \Closure) {
269
+ $ callback = $ validator ;
265
270
$ validator = 'callback ' ;
266
271
$ args [] = $ data ;
267
272
268
- } elseif ( is_string ($ validator ) ) {
273
+ } elseif (is_string ($ validator )) {
269
274
270
275
// if $validator is a custom add callback in the property {@see $_validators}.
271
- if ( isset ($ this ->_validators [$ validator ]) ) {
276
+ if (isset ($ this ->_validators [$ validator ])) {
272
277
$ callback = $ this ->_validators [$ validator ];
273
278
274
- // if $validator is a custom method of the subclass.
275
- } elseif ( is_string ($ validator ) && method_exists ($ this , $ validator ) ) {
279
+ // if $validator is a custom method of the subclass.
280
+ } elseif (is_string ($ validator ) && method_exists ($ this , $ validator )) {
276
281
277
282
$ callback = [$ this , $ validator ];
278
283
279
- // $validator is a method of the class 'ValidatorList'
280
- } elseif ( is_string ($ validator ) && is_callable ([ValidatorList::class, $ validator ]) ) {
284
+ // $validator is a method of the class 'ValidatorList'
285
+ } elseif (is_string ($ validator ) && is_callable ([ValidatorList::class, $ validator ])) {
281
286
282
- $ callback = [ ValidatorList::class, $ validator ];
287
+ $ callback = [ValidatorList::class, $ validator ];
283
288
} else {
284
289
throw new \InvalidArgumentException ("The validator [ $ validator] don't exists! " );
285
290
}
@@ -300,7 +305,7 @@ protected function resetRuntimeData($clearErrors = false)
300
305
{
301
306
$ this ->_safeData = $ this ->_availableRules = [];
302
307
303
- if ( $ clearErrors ) {
308
+ if ($ clearErrors ) {
304
309
$ this ->clearErrors ();
305
310
}
306
311
@@ -350,18 +355,18 @@ protected function collectRules()
350
355
}
351
356
352
357
// check validator
353
- if ( !is_string ($ rule [1 ]) && !($ rule [1 ] instanceof \Closure) ) {
358
+ if (!is_string ($ rule [1 ]) && !($ rule [1 ] instanceof \Closure)) {
354
359
throw new \InvalidArgumentException ('The rule validator rule must be is a validator name or a Closure! position: rule[1]. ' );
355
360
}
356
361
357
362
// global rule.
358
363
if (empty ($ rule ['on ' ])) {
359
364
$ this ->_availableRules [] = $ rule ;
360
365
361
- // only use to special scene.
366
+ // only use to special scene.
362
367
} else {
363
368
$ ruleScene = $ rule ['on ' ];
364
- $ ruleScene = is_string ($ ruleScene ) ? array_map ('trim ' ,explode (', ' , $ ruleScene )) : (array )$ ruleScene ;
369
+ $ ruleScene = is_string ($ ruleScene ) ? array_map ('trim ' , explode (', ' , $ ruleScene )) : (array )$ ruleScene ;
365
370
366
371
if (in_array ($ scene , $ ruleScene , true )) {
367
372
unset($ rule ['on ' ]);
@@ -389,17 +394,24 @@ public function clearErrors()
389
394
390
395
/**
391
396
* 是否有错误
392
- * @date 2015-09-27
393
397
* @return boolean
394
398
*/
395
399
public function hasError (): bool
396
400
{
397
401
return count ($ this ->_errors ) > 0 ;
398
402
}
403
+
404
+ /**
405
+ * @return bool
406
+ */
399
407
public function isFail (): bool
400
408
{
401
409
return $ this ->hasError ();
402
410
}
411
+
412
+ /**
413
+ * @return bool
414
+ */
403
415
public function fail (): bool
404
416
{
405
417
return $ this ->hasError ();
@@ -425,13 +437,12 @@ public function getErrors(): array
425
437
/**
426
438
* 得到第一个错误信息
427
439
* @author inhere
428
- * @date 2015-09-27
429
440
* @param bool $onlyMsg
430
441
* @return array|string
431
442
*/
432
443
public function firstError ($ onlyMsg = true )
433
444
{
434
- $ e = $ this ->_errors ;
445
+ $ e = $ this ->_errors ;
435
446
$ first = array_shift ($ e );
436
447
437
448
return $ onlyMsg ? array_values ($ first )[0 ] : $ first ;
@@ -440,13 +451,12 @@ public function firstError($onlyMsg = true)
440
451
/**
441
452
* 得到最后一个错误信息
442
453
* @author inhere
443
- * @date 2015-09-27
444
454
* @param bool $onlyMsg
445
455
* @return array|string
446
456
*/
447
457
public function lastError ($ onlyMsg = true )
448
458
{
449
- $ e = $ this ->_errors ;
459
+ $ e = $ this ->_errors ;
450
460
$ last = array_pop ($ e );
451
461
452
462
return $ onlyMsg ? array_values ($ last )[0 ] : $ last ;
@@ -457,25 +467,25 @@ public function lastError($onlyMsg = true)
457
467
* @var array
458
468
*/
459
469
private static $ _defaultMessages = [
460
- 'int ' => '{attr} must be an integer! ' ,
470
+ 'int ' => '{attr} must be an integer! ' ,
461
471
'number ' => '{attr} must be an integer greater than 0! ' ,
462
- 'bool ' => '{attr} must be is boolean! ' ,
463
- 'float ' => '{attr} must be is float! ' ,
472
+ 'bool ' => '{attr} must be is boolean! ' ,
473
+ 'float ' => '{attr} must be is float! ' ,
464
474
'regexp ' => '{attr} does not meet the conditions ' ,
465
- 'url ' => '{attr} not is url address! ' ,
466
- 'email ' => '{attr} not is email address! ' ,
467
- 'ip ' => '{attr} not is ip address! ' ,
475
+ 'url ' => '{attr} not is url address! ' ,
476
+ 'email ' => '{attr} not is email address! ' ,
477
+ 'ip ' => '{attr} not is ip address! ' ,
468
478
'required ' => 'parameter {attr} is required! ' ,
469
479
'length ' => '{attr} length must at rang {min} ~ {max} ' ,
470
- 'size ' => '{attr} must be an integer and at rang {min} ~ {max} ' ,
471
- 'min ' => '{attr} minimum boundary is {value} ' ,
472
- 'max ' => '{attr} maximum boundary is {value} ' ,
473
- 'in ' => '{attr} must in ({value}) ' ,
480
+ 'size ' => '{attr} must be an integer and at rang {min} ~ {max} ' ,
481
+ 'min ' => '{attr} minimum boundary is {value} ' ,
482
+ 'max ' => '{attr} maximum boundary is {value} ' ,
483
+ 'in ' => '{attr} must in ({value}) ' ,
474
484
'string ' => '{attr} must be a string ' ,
475
485
'compare ' => '{attr} must be equals to {attr0} ' ,
476
486
'isArray ' => '{attr} must be an array ' ,
477
487
'callback ' => '{attr} don \'t pass the test and verify! ' ,
478
- '_ ' => '{attr} validation is not through! ' ,
488
+ '_ ' => '{attr} validation is not through! ' ,
479
489
];
480
490
481
491
/**
@@ -504,7 +514,7 @@ public function getMessages(): array
504
514
* @param string $msg 自定义提示消息
505
515
* @return string
506
516
*/
507
- public function getMessage ($ name , array $ params , array $ rule = [], $ msg= null )
517
+ public function getMessage ($ name , array $ params , array $ rule = [], $ msg = null )
508
518
{
509
519
if (!$ msg ) {
510
520
$ msgList = $ this ->getMessages ();
@@ -675,8 +685,8 @@ public function has(string $key): bool
675
685
676
686
/**
677
687
* Get data item for key
678
- * @param string $key The data key
679
- * @param mixed $default The default value to return if data key does not exist
688
+ * @param string $key The data key
689
+ * @param mixed $default The default value to return if data key does not exist
680
690
* @return mixed The key's value, or the default value
681
691
*/
682
692
public function get ($ key , $ default = null )
@@ -694,6 +704,7 @@ public function getSafe(string $key, $default = null)
694
704
{
695
705
return $ this ->getValid ($ key , $ default );
696
706
}
707
+
697
708
public function getValid (string $ key , $ default = null )
698
709
{
699
710
return array_key_exists ($ key , $ this ->_safeData ) ? $ this ->_safeData [$ key ] : $ default ;
0 commit comments