0 follower

Final Class Yiisoft\Validator\Rule\CountHandler

InheritanceYiisoft\Validator\Rule\CountHandler
ImplementsYiisoft\Validator\RuleHandlerInterface
Uses TraitsYiisoft\Validator\Rule\Trait\CountableLimitHandlerTrait

Validates that the value contains certain number of items.

Can be applied to arrays or classes implementing Countable interface.

See also Yiisoft\Validator\Rule\Count.

Public Methods

Hide inherited methods

Method Description Defined By
validate() Yiisoft\Validator\Rule\CountHandler

Method Details

Hide inherited methods

validate() public method

public validate( mixed $value, Yiisoft\Validator\RuleInterface $rule, Yiisoft\Validator\ValidationContext $context ): Yiisoft\Validator\Result
$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;
}