- md5
- a8c9fbc9f966a881b42e41e341eb26c6
- sha1
- eef4b18dab417c9e134a0e7a2325b31c0773f659
- sha256
- 9a8a1a9e8ca7f162eef9792ee77d328ff227144ea1b0620fbb65da5f21ef81d2
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
/**
* @author Bernhard Schussek <[email protected]>
*/
class LengthValidator extends ConstraintValidator
{
/**
* @return void
*/
public function validate(mixed $value, Constraint $constraint)
{
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((int) $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((int) $constraint->min)
->setCode($exactlyOptionEnabled ? Length::NOT_EQUAL_LENGTH_ERROR : Length::TOO_SHORT_ERROR)
->addViolation();
}
}
}
The same content is in 136 releases, Joomla 5.1.0-beta1 to 6.3.0-alpha1. See them all, or every version of this file.