Skip to content

Commit ca27731

Browse files
committed
add more compare validators, add more test
1 parent 506d2c3 commit ca27731

File tree

8 files changed

+235
-111
lines changed

8 files changed

+235
-111
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -562,10 +562,10 @@ $v = Validation::make($_POST,[
562562
`in/enum` | 枚举验证: 包含 | `['status', 'in', [1,2,3]]`
563563
`notIn` | 枚举验证: 不包含 | `['status', 'notIn', [4,5,6]]`
564564
`inField` | 枚举验证: 字段值 存在于 另一个字段(anotherField)的值中 | `['field', 'inField', 'anotherField']`
565-
`mustBe` | 必须是等于给定值 | `['status', 'mustBe', 1]`
566-
`notBe` | 不能等于给定值 | `['status', 'notBe', 0]`
567-
`compare/same/equal` | 字段值比较: 相同 | `['passwd', 'compare', 'repasswd']`
568-
`different/notEqual` | 字段值比较: 不能相同 | `['userId', 'notEqual', 'targetId']`
565+
`eq/mustBe` | 必须是等于给定值 | `['status', 'mustBe', 1]`
566+
`neq/notBe` | 不能等于给定值 | `['status', 'notBe', 0]`
567+
`eqField` | 字段值比较: 相同 | `['passwd', 'eqField', 'repasswd']`
568+
`neqField` | 字段值比较: 不能相同 | `['userId', 'neqField', 'targetId']`
569569
`requiredIf` | 指定的其它字段( anotherField )值等于任何一个 `value` 时,此字段为 **必填**(ref laravel) | `['city', 'requiredIf', 'myCity', ['chengdu'] ]`
570570
`requiredUnless` | 指定的其它字段( anotherField )值等于任何一个 `value` 时,此字段为 **不必填**(ref laravel) | `['city', 'requiredUnless', 'myCity', ['chengdu'] ]`
571571
`requiredWith` | 指定的字段中的 _任意一个_ 有值且不为空,则此字段为 **必填**(ref laravel) | `['city', 'requiredWith', ['myCity'] ]`

src/Utils/ErrorMessageTrait.php

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

99
namespace Inhere\Validate\Utils;
1010

11+
use Inhere\Validate\Validators;
12+
1113
/**
1214
* trait ErrorMessageTrait
1315
* @author inhere
1416
* @package Inhere\Validate\Utils
1517
*/
1618
trait ErrorMessageTrait
1719
{
18-
/**
19-
* @var array
20-
*/
21-
private static $validatorAliases = [
22-
// alias => real name.
23-
'int' => 'integer',
24-
'num' => 'number',
25-
'bool' => 'boolean',
26-
'in' => 'enum',
27-
'range' => 'size',
28-
'between' => 'size',
29-
'len' => 'length',
30-
'lenEq' => 'fixedSize',
31-
'lengthEq' => 'fixedSize',
32-
'sizeEq' => 'fixedSize',
33-
'diff' => 'notEqual',
34-
'different' => 'notEqual',
35-
'map' => 'isMap',
36-
'list' => 'isList',
37-
'array' => 'isArray',
38-
'absUrl' => 'absoluteUrl',
39-
// 'ints' => 'intList',
40-
];
41-
4220
/**
4321
* 默认的错误提示信息
4422
* @var array
@@ -394,7 +372,7 @@ public function setMessages(array $messages): self
394372
public function getMessage($validator, string $field, array $args = [], $message = null): string
395373
{
396374
$rawName = \is_string($validator) ? $validator : 'callback';
397-
$validator = self::getValidatorName($rawName);
375+
$validator = Validators::getRealName($rawName);
398376

399377
// get message from default dict.
400378
if (!$message) {
@@ -483,24 +461,6 @@ public function getTranslate(string $field): string
483461
return $field;
484462
}
485463

486-
/**
487-
* get real validator name by alias name
488-
* @param string $validator
489-
* @return string
490-
*/
491-
public static function getValidatorName(string $validator): string
492-
{
493-
return self::$validatorAliases[$validator] ?? $validator;
494-
}
495-
496-
/**
497-
* @return array
498-
*/
499-
public static function getValidatorAliases(): array
500-
{
501-
return self::$validatorAliases;
502-
}
503-
504464
/**
505465
* @return bool
506466
*/

src/Utils/Helper.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,23 @@ public static function getMimeType(string $file): string
7878
return (string)\finfo_file(\finfo_open(FILEINFO_MIME_TYPE), $file);
7979
}
8080

81+
/**
82+
* @param mixed $val
83+
* @return int
84+
*/
85+
public static function length($val): int
86+
{
87+
if (\is_int($val)) {
88+
return $val;
89+
}
90+
91+
if (\is_string($val)) {
92+
return self::strlen($val);
93+
}
94+
95+
return \is_array($val) ? \count($val) : -1;
96+
}
97+
8198
/**
8299
* @param string $str
83100
* @param string $encoding

src/Utils/UserAndContextValidatorsTrait.php

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919
*/
2020
trait UserAndContextValidatorsTrait
2121
{
22-
/**
23-
* custom add's validator by addValidator()
24-
* @var array
25-
*/
22+
/** @var array user custom add's validators */
2623
protected static $_validators = [];
2724

2825
/** @var string */
@@ -55,22 +52,21 @@ trait UserAndContextValidatorsTrait
5552
public function addValidator(string $name, callable $callback, string $msg = ''): self
5653
{
5754
self::setValidator($name, $callback, $msg);
58-
5955
return $this;
6056
}
6157

6258
/**
6359
* add a custom validator
6460
* @param string $name
6561
* @param callable $callback
66-
* @param string $msg
62+
* @param string $message
6763
*/
68-
public static function setValidator(string $name, callable $callback, string $msg = null)
64+
public static function setValidator(string $name, callable $callback, string $message = '')
6965
{
7066
self::$_validators[$name] = $callback;
7167

72-
if ($msg) {
73-
self::setDefaultMessage($name, $msg);
68+
if ($message) {
69+
self::setDefaultMessage($name, $message);
7470
}
7571
}
7672

@@ -404,7 +400,7 @@ public function mimesValidator(string $field, $types = null)
404400
}
405401

406402
/*******************************************************************************
407-
* Special validators(require context data)
403+
* Field compare validators(require context data)
408404
******************************************************************************/
409405

410406
/**
@@ -428,26 +424,48 @@ public function equalValidator($val, string $compareField): bool
428424
return $this->compareValidator($val, $compareField);
429425
}
430426

427+
public function eqFieldValidator($val, string $compareField): bool
428+
{
429+
return $this->compareValidator($val, $compareField);
430+
}
431+
431432
/**
432433
* 字段值比较:当前字段值是否与给定的字段值不相同
433434
* @param mixed $val
434435
* @param string $compareField
435436
* @return bool
436437
*/
437-
public function notEqualValidator($val, string $compareField): bool
438+
public function neqFieldValidator($val, string $compareField): bool
438439
{
439440
return $compareField && ($val !== $this->getByPath($compareField));
440441
}
441442

443+
// TODO: gtFiled, ltField, gteField, lteField
444+
442445
/**
443-
* alias of the 'notEqualValidator'
444-
* @param mixed $val
445-
* @param string $compareField
446+
* 字段值比较:当前字段值 要大于 给定字段的值
447+
* @param string|int $val
448+
* @param string $compareField
446449
* @return bool
447450
*/
448-
public function differentValidator($val, string $compareField): bool
451+
public function gtFieldValidator($val, string $compareField): bool
449452
{
450-
return $compareField && ($val !== $this->getByPath($compareField));
453+
$minVal = $this->getByPath($compareField);
454+
455+
return Validators::gt($val, $minVal);
456+
}
457+
458+
/**
459+
* 字段值比较:当前字段值 要大于等于 给定字段的值
460+
* @param string|int $val
461+
* @param string $compareField
462+
* @return bool
463+
*/
464+
public function gteFieldValidator($val, string $compareField): bool
465+
{
466+
$minVal = $this->getByPath($compareField);
467+
468+
return Validators::gte($val, $minVal);
451469
}
452470

453471
/**
@@ -465,6 +483,19 @@ public function inFieldValidator($val, string $anotherField): bool
465483
return false;
466484
}
467485

486+
/**
487+
* 比较两个日期字段的 间隔天数 是否符合要求
488+
* @todo
489+
* @param string $val
490+
* @param string $compareField
491+
* @param int $expected
492+
* @param string $op
493+
*/
494+
public function intervalDayValidator($val, string $compareField, int $expected, string $op = '>=')
495+
{
496+
497+
}
498+
468499
/**
469500
* 对数组中的每个值都应用给定的验证器,并且要全部通过
470501
* `['foo.*.id', 'each', 'number']`
@@ -518,19 +549,6 @@ public function eachValidator(array $values, ...$args): bool
518549
return true;
519550
}
520551

521-
/**
522-
* 比较两个日期字段的 间隔天数 是否符合要求
523-
* @todo
524-
* @param string $val
525-
* @param string $compareField
526-
* @param int $expected
527-
* @param string $op
528-
*/
529-
public function intervalDayValidator($val, string $compareField, int $expected, string $op = '>=')
530-
{
531-
532-
}
533-
534552
/*******************************************************************************
535553
* getter/setter/helper
536554
******************************************************************************/

src/ValidationTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ protected function valueValidate(string $field, $value, $validator, array $args,
337337
$args[] = $this->data;
338338
$passed = $validator($value, ...$args);
339339
} elseif (\is_string($validator)) {
340-
$realName = self::getValidatorName($validator);
340+
$realName = Validators::getRealName($validator);
341341
// if $validator is a custom add callback in the property {@see $_validators}.
342342
if (isset(self::$_validators[$validator])) {
343343
$args[] = $this->data;

0 commit comments

Comments
 (0)