class LengthValidator
@author Bernhard Schussek <bschussek@gmail.com>
Hierarchy
- class \Symfony\Component\Validator\ConstraintValidator implements \Symfony\Component\Validator\ConstraintValidatorInterface
- class \Symfony\Component\Validator\Constraints\LengthValidator extends \Symfony\Component\Validator\ConstraintValidator
Expanded class hierarchy of LengthValidator
File
-
vendor/
symfony/ validator/ Constraints/ LengthValidator.php, line 22
Namespace
Symfony\Component\Validator\ConstraintsView source
class LengthValidator extends ConstraintValidator {
public function validate(mixed $value, Constraint $constraint) : void {
if (!$constraint instanceof Length) {
throw new UnexpectedTypeException($constraint, Length::class);
}
if (null === $value) {
return;
}
if (!\is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}
$stringValue = (string) $value;
if (null !== $constraint->normalizer) {
$stringValue = ($constraint->normalizer)($stringValue);
}
try {
$invalidCharset = !@mb_check_encoding($stringValue, $constraint->charset);
} catch (\ValueError $e) {
if (!str_starts_with($e->getMessage(), 'mb_check_encoding(): Argument #2 ($encoding) must be a valid encoding')) {
throw $e;
}
$invalidCharset = true;
}
$length = $invalidCharset ? 0 : match ($constraint->countUnit) { Length::COUNT_BYTES => \strlen($stringValue),
Length::COUNT_CODEPOINTS => mb_strlen($stringValue, $constraint->charset),
Length::COUNT_GRAPHEMES => grapheme_strlen($stringValue),
};
if ($invalidCharset || false === ($length ?? false)) {
$this->context
->buildViolation($constraint->charsetMessage)
->setParameter('{{ value }}', $this->formatValue($stringValue))
->setParameter('{{ charset }}', $constraint->charset)
->setInvalidValue($value)
->setCode(Length::INVALID_CHARACTERS_ERROR)
->addViolation();
return;
}
if (null !== $constraint->max && $length > $constraint->max) {
$exactlyOptionEnabled = $constraint->min == $constraint->max;
$this->context
->buildViolation($exactlyOptionEnabled ? $constraint->exactMessage : $constraint->maxMessage)
->setParameter('{{ value }}', $this->formatValue($stringValue))
->setParameter('{{ limit }}', $constraint->max)
->setParameter('{{ value_length }}', $length)
->setInvalidValue($value)
->setPlural($constraint->max)
->setCode($exactlyOptionEnabled ? Length::NOT_EQUAL_LENGTH_ERROR : Length::TOO_LONG_ERROR)
->addViolation();
return;
}
if (null !== $constraint->min && $length < $constraint->min) {
$exactlyOptionEnabled = $constraint->min == $constraint->max;
$this->context
->buildViolation($exactlyOptionEnabled ? $constraint->exactMessage : $constraint->minMessage)
->setParameter('{{ value }}', $this->formatValue($stringValue))
->setParameter('{{ limit }}', $constraint->min)
->setParameter('{{ value_length }}', $length)
->setInvalidValue($value)
->setPlural($constraint->min)
->setCode($exactlyOptionEnabled ? Length::NOT_EQUAL_LENGTH_ERROR : Length::TOO_SHORT_ERROR)
->addViolation();
}
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
ConstraintValidator::$context | protected | property | ||
ConstraintValidator::formatTypeOf | protected | function | Returns a string representation of the type of the value. | |
ConstraintValidator::formatValue | protected | function | Returns a string representation of the value. | |
ConstraintValidator::formatValues | protected | function | Returns a string representation of a list of values. | |
ConstraintValidator::initialize | public | function | Initializes the constraint validator. | Overrides ConstraintValidatorInterface::initialize |
ConstraintValidator::OBJECT_TO_STRING | public | constant | Whether to cast objects with a "__toString()" method to strings. | |
ConstraintValidator::PRETTY_DATE | public | constant | Whether to format {@link \DateTime} objects, either with the {@link \IntlDateFormatter} (if it is available) or as RFC-3339 dates ("Y-m-d H:i:s"). |
|
LengthValidator::validate | public | function | Checks if the passed value is valid. | Overrides ConstraintValidatorInterface::validate |