AbstractComparisonValidator.php in Zircon Profile 8
File
vendor/symfony/validator/Constraints/AbstractComparisonValidator.php
View source
<?php
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
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)) {
if ($this->context instanceof ExecutionContextInterface) {
$this->context
->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();
}
else {
$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);
}