2013-04-29 13:43:20 +01:00
|
|
|
<?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;
|
2014-03-10 12:51:04 +00:00
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
2013-04-29 13:43:20 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides a base class for the validation of property comparisons.
|
|
|
|
*
|
|
|
|
* @author Daniel Holmes <daniel@danielholmes.org>
|
|
|
|
*/
|
|
|
|
abstract class AbstractComparisonValidator extends ConstraintValidator
|
|
|
|
{
|
|
|
|
/**
|
2014-04-15 06:57:34 +01:00
|
|
|
* {@inheritdoc}
|
2013-04-29 13:43:20 +01:00
|
|
|
*/
|
|
|
|
public function validate($value, Constraint $constraint)
|
|
|
|
{
|
2014-03-10 12:51:04 +00:00
|
|
|
if (!$constraint instanceof AbstractComparison) {
|
|
|
|
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\AbstractComparison');
|
|
|
|
}
|
|
|
|
|
2013-07-25 13:05:03 +01:00
|
|
|
if (null === $value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-11 07:51:18 +00:00
|
|
|
if (!$this->compareValues($value, $constraint->value)) {
|
2013-04-29 13:43:20 +01:00
|
|
|
$this->context->addViolation($constraint->message, array(
|
2014-08-04 11:36:09 +01:00
|
|
|
'{{ value }}' => $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE),
|
|
|
|
'{{ compared_value }}' => $this->formatValue($constraint->value, self::OBJECT_TO_STRING | self::PRETTY_DATE),
|
2014-07-24 13:14:19 +01:00
|
|
|
'{{ compared_value_type }}' => $this->formatTypeOf($constraint->value)
|
2013-04-29 13:43:20 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compares the two given values to find if their relationship is valid
|
|
|
|
*
|
|
|
|
* @param mixed $value1 The first value to compare
|
|
|
|
* @param mixed $value2 The second value to compare
|
|
|
|
*
|
2014-04-16 11:30:19 +01:00
|
|
|
* @return bool true if the relationship is valid, false otherwise
|
2013-04-29 13:43:20 +01:00
|
|
|
*/
|
|
|
|
abstract protected function compareValues($value1, $value2);
|
|
|
|
}
|