for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace Yiisoft\Validator\Rule;
use Yiisoft\Validator\Exception\UnexpectedRuleException;
use Yiisoft\Validator\Result;
use Yiisoft\Validator\Rule\Trait\LimitHandlerTrait;
use Yiisoft\Validator\RuleHandlerInterface;
use Yiisoft\Validator\ValidationContext;
use function is_string;
/**
* Validates that the value is a string of a certain length.
*
* @see Length
*/
final class LengthHandler implements RuleHandlerInterface
{
use LimitHandlerTrait;
public function validate($value, object $rule, ValidationContext $context): Result
if (!$rule instanceof Length) {
throw new UnexpectedRuleException(Length::class, $rule);
}
$result = new Result();
if (!is_string($value)) {
$result->addError($rule->getIncorrectInputMessage(), [
'attribute' => $context->getTranslatedAttribute(),
'type' => get_debug_type($value),
]);
return $result;
$length = mb_strlen($value, $rule->getEncoding());
$this->validateLimits($rule, $context, $length, $result);