df3d543349
* 2.4: made types consistent with those defined in Hack made {@inheritdoc} annotations consistent across the board made {@inheritdoc} annotations consistent across the board fixed types in phpdocs [Debug] Fixed ClassNotFoundFatalErrorHandler on windows. made phpdoc types consistent with those defined in Hack Add support Thai translations [Validator] Add missing czech translations made types consistent with those defined in Hack removed extra/unsupported arguments [HttpKernel] fixed an error message [TwigBundle] removed undefined argument [Translation] Make IcuDatFileLoader/IcuResFileLoader::load invalid resource compatible with HHVM. Conflicts: src/Symfony/Component/Console/Helper/TableHelper.php src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php src/Symfony/Component/Form/FormError.php src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php src/Symfony/Component/Process/ProcessPipes.php src/Symfony/Component/PropertyAccess/PropertyAccessor.php src/Symfony/Component/Security/Acl/Dbal/MutableAclProvider.php src/Symfony/Component/Security/Http/RememberMe/TokenBasedRememberMeServices.php src/Symfony/Component/Translation/Dumper/FileDumper.php src/Symfony/Component/Validator/ConstraintViolation.php src/Symfony/Component/Validator/Constraints/EmailValidator.php src/Symfony/Component/Validator/ExecutionContextInterface.php src/Symfony/Component/Validator/Mapping/BlackholeMetadataFactory.php
76 lines
2.3 KiB
PHP
76 lines
2.3 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the Symfony package.
|
|
*
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
*
|
|
* 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;
|
|
|
|
/**
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
|
*/
|
|
class LengthValidator extends ConstraintValidator
|
|
{
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function validate($value, Constraint $constraint)
|
|
{
|
|
if (!$constraint instanceof Length) {
|
|
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Length');
|
|
}
|
|
|
|
if (null === $value || '' === $value) {
|
|
return;
|
|
}
|
|
|
|
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
|
|
throw new UnexpectedTypeException($value, 'string');
|
|
}
|
|
|
|
$stringValue = (string) $value;
|
|
|
|
if (function_exists('grapheme_strlen') && 'UTF-8' === $constraint->charset) {
|
|
$length = grapheme_strlen($stringValue);
|
|
} elseif (function_exists('mb_strlen')) {
|
|
$length = mb_strlen($stringValue, $constraint->charset);
|
|
} else {
|
|
$length = strlen($stringValue);
|
|
}
|
|
|
|
if ($constraint->min == $constraint->max && $length != $constraint->min) {
|
|
$this->context->addViolation($constraint->exactMessage, array(
|
|
'{{ value }}' => $stringValue,
|
|
'{{ limit }}' => $constraint->min,
|
|
), $value, (int) $constraint->min);
|
|
|
|
return;
|
|
}
|
|
|
|
if (null !== $constraint->max && $length > $constraint->max) {
|
|
$this->context->addViolation($constraint->maxMessage, array(
|
|
'{{ value }}' => $stringValue,
|
|
'{{ limit }}' => $constraint->max,
|
|
), $value, (int) $constraint->max);
|
|
|
|
return;
|
|
}
|
|
|
|
if (null !== $constraint->min && $length < $constraint->min) {
|
|
$this->context->addViolation($constraint->minMessage, array(
|
|
'{{ value }}' => $stringValue,
|
|
'{{ limit }}' => $constraint->min,
|
|
), $value, (int) $constraint->min);
|
|
}
|
|
}
|
|
}
|