Final Class Yiisoft\Validator\Rule\CountHandler
| Inheritance | Yiisoft\Validator\Rule\CountHandler |
|---|---|
| Implements | Yiisoft\Validator\RuleHandlerInterface |
| Uses Traits | Yiisoft\Validator\Rule\Trait\CountableLimitHandlerTrait |
Validates that the value contains certain number of items.
Can be applied to arrays or classes implementing {@see \Countable} interface.
See also Yiisoft\Validator\Rule\Count.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| validate() | Yiisoft\Validator\Rule\CountHandler |
Method Details
| public Yiisoft\Validator\Result validate ( mixed $value, Yiisoft\Validator\RuleInterface $rule, Yiisoft\Validator\ValidationContext $context ) | ||
| $value | mixed | |
| $rule | Yiisoft\Validator\RuleInterface | |
| $context | Yiisoft\Validator\ValidationContext | |
public function validate(mixed $value, RuleInterface $rule, ValidationContext $context): Result
{
if (!$rule instanceof Count) {
throw new UnexpectedRuleException(Count::class, $rule);
}
$result = new Result();
/** @var mixed $value */
if (!is_countable($value)) {
$result->addError($rule->getIncorrectInputMessage(), [
'property' => $context->getTranslatedProperty(),
'Property' => $context->getCapitalizedTranslatedProperty(),
'type' => get_debug_type($value),
]);
return $result;
}
$count = count($value);
$this->validateCountableLimits($rule, $context, $count, $result);
return $result;
}
Signup or Login in order to comment.