AbstractComparisonValidator.php in Plug 7
File
lib/Symfony/validator/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php
View source
<?php
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
abstract class AbstractComparisonValidator extends ConstraintValidator {
public function validate($value, Constraint $constraint) {
if (!$constraint instanceof AbstractComparison) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\AbstractComparison');
}
if (null === $value) {
return;
}
$comparedValue = $constraint->value;
if (is_string($comparedValue)) {
if ($value instanceof \DatetimeImmutable) {
$comparedValue = new \DatetimeImmutable($comparedValue);
}
elseif ($value instanceof \DateTime || $value instanceof \DateTimeInterface) {
$comparedValue = new \DateTime($comparedValue);
}
}
if (!$this
->compareValues($value, $comparedValue)) {
$this
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this
->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE))
->setParameter('{{ compared_value }}', $this
->formatValue($comparedValue, self::OBJECT_TO_STRING | self::PRETTY_DATE))
->setParameter('{{ compared_value_type }}', $this
->formatTypeOf($comparedValue))
->addViolation();
}
}
protected abstract function compareValues($value1, $value2);
}