Skip to content

Commit

Permalink
feat(IsArray): 增加 minLength, maxLength 选项
Browse files Browse the repository at this point in the history
  • Loading branch information
twinh committed Nov 24, 2020
1 parent 1a49d63 commit c6dc44c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 6 deletions.
45 changes: 43 additions & 2 deletions lib/BaseValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* The base class of validator
*
* @author Twin Huang <twinhuang@qq.com>
* @method string t(string $message, array $parameters = array()) Translates a message
* @property \Wei\T $t The translator wei
* @mixin \ValidateMixin
* @mixin \TMixin
*/
abstract class BaseValidator extends Base
{
Expand Down Expand Up @@ -89,6 +89,12 @@ abstract class BaseValidator extends Base
*/
protected $store = [[]];

/**
* @var BaseValidator[]
* @experimental
*/
private $subValidators;

/**
* Validate the input value
*
Expand Down Expand Up @@ -125,6 +131,12 @@ public function getMessages($name = null)
$this->loadTranslationMessages();

$messages = [];
foreach ($this->subValidators as $validator) {
if ($validator->getErrors()) {
$messages = array_merge($messages, $validator->getMessages($name));
}
}

foreach ($this->errors as $optionName => $message) {
preg_match_all('/\%(.+?)\%/', $message, $matches);
$parameters = [];
Expand Down Expand Up @@ -312,4 +324,33 @@ protected function reset()
}
$this->store[] = [];
}

/**
* Validate by other rule
*
* @param mixed $input The input value to be validated
* @param string $rule The name of rule, like minLength, maxLength
* @param mixed $options The options pass to rule
* @param bool $skipExists Whether to skip current rule if there is same rule to validate later
* @return bool
* @experimental
*/
protected function validateRule($input, $rule, $options = null, bool $skipExists = true)
{
if ($skipExists && $this->validator) {
$rules = $this->validator->getFieldRules($this->validator->getCurrentField());
if (isset($rules[$rule])) {
$keys = array_keys($rules);
if ($keys[$this->validator->getCurrentRule()] < $keys[$rule]) {
return true;
}
}
}

$validator = null;
$this->subValidators[] = &$validator;
return $this->validate->validateOne($rule, $input, $options, $validator, [
'name' => $this->name,
]);
}
}
30 changes: 30 additions & 0 deletions lib/IsArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ class IsArray extends BaseValidator

protected $negativeMessage = '%name% must not be an array';

/**
* @var int|null
*/
protected $minLength;

/**
* @var int|null
*/
protected $maxLength;

public function __invoke($input, int $minLength = null, int $maxLength = null)
{
null !== $minLength && $this->storeOption('minLength', $minLength);
null !== $maxLength && $this->storeOption('maxLength', $maxLength);

return $this->isValid($input);
}

/**
* {@inheritdoc}
*/
Expand All @@ -28,6 +46,18 @@ protected function doValidate($input)
$this->addError('notArray');
return false;
}

if (null !== $this->minLength) {
$result = $this->validateRule($input, 'minLength', $this->minLength);
if (!$result) {
return $result;
}
}

if (null !== $this->maxLength) {
return $this->validateRule($input, 'maxLength', $this->maxLength);
}

return true;
}
}
15 changes: 11 additions & 4 deletions tests/unit/IsArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ final class IsArrayTest extends BaseValidatorTestCase
* @dataProvider providerForArrayVal
* @param mixed $input
*/
public function testArrayVal($input)
public function testArrayVal($input, int $minLength = null, int $maxLength = null)
{
$this->assertTrue($this->isArray($input));
$this->assertTrue($this->isArray($input, $minLength, $maxLength));
}

/**
* @dataProvider providerForNotArrayVal
* @param mixed $input
*/
public function testNotArrayVal($input)
public function testNotArrayVal($input, int $minLength = null, int $maxLength = null)
{
$this->assertFalse($this->isArray($input));
$this->assertFalse($this->isArray($input, $minLength, $maxLength));
}

public function providerForArrayVal()
Expand All @@ -32,6 +32,10 @@ public function providerForArrayVal()
[[1]],
[new \ArrayObject()],
[new \SimpleXMLElement('<xml></xml>')],
[[1, 2], 1],
[[1, 2], null, 3],
[[1, 2], 1, 3],
[[1, 2], 2, 3],
];
}

Expand All @@ -40,6 +44,9 @@ public function providerForNotArrayVal()
return [
[1],
['1,2,3'],
[[1, 2], 3],
[[1, 2], null, 1],
[[1, 2], 0, 1],
];
}
}

0 comments on commit c6dc44c

Please sign in to comment.