annotate vendor/symfony/validator/Constraints/AbstractComparisonValidator.php @ 8:50b0d041100e

Further files for download
author Chris Cannam
date Mon, 05 Feb 2018 10:56:40 +0000
parents 4c8ae668cc8c
children 1fec387a4317
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\Validator\Constraints;
Chris@0 13
Chris@0 14 use Symfony\Component\Validator\Constraint;
Chris@0 15 use Symfony\Component\Validator\ConstraintValidator;
Chris@0 16 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
Chris@0 17
Chris@0 18 /**
Chris@0 19 * Provides a base class for the validation of property comparisons.
Chris@0 20 *
Chris@0 21 * @author Daniel Holmes <daniel@danielholmes.org>
Chris@0 22 * @author Bernhard Schussek <bschussek@gmail.com>
Chris@0 23 */
Chris@0 24 abstract class AbstractComparisonValidator extends ConstraintValidator
Chris@0 25 {
Chris@0 26 /**
Chris@0 27 * {@inheritdoc}
Chris@0 28 */
Chris@0 29 public function validate($value, Constraint $constraint)
Chris@0 30 {
Chris@0 31 if (!$constraint instanceof AbstractComparison) {
Chris@0 32 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\AbstractComparison');
Chris@0 33 }
Chris@0 34
Chris@0 35 if (null === $value) {
Chris@0 36 return;
Chris@0 37 }
Chris@0 38
Chris@0 39 $comparedValue = $constraint->value;
Chris@0 40
Chris@0 41 // Convert strings to DateTimes if comparing another DateTime
Chris@0 42 // This allows to compare with any date/time value supported by
Chris@0 43 // the DateTime constructor:
Chris@0 44 // http://php.net/manual/en/datetime.formats.php
Chris@0 45 if (is_string($comparedValue)) {
Chris@0 46 if ($value instanceof \DateTimeImmutable) {
Chris@0 47 // If $value is immutable, convert the compared value to a
Chris@0 48 // DateTimeImmutable too
Chris@0 49 $comparedValue = new \DatetimeImmutable($comparedValue);
Chris@0 50 } elseif ($value instanceof \DateTimeInterface) {
Chris@0 51 // Otherwise use DateTime
Chris@0 52 $comparedValue = new \DateTime($comparedValue);
Chris@0 53 }
Chris@0 54 }
Chris@0 55
Chris@0 56 if (!$this->compareValues($value, $comparedValue)) {
Chris@0 57 $this->context->buildViolation($constraint->message)
Chris@0 58 ->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE))
Chris@0 59 ->setParameter('{{ compared_value }}', $this->formatValue($comparedValue, self::OBJECT_TO_STRING | self::PRETTY_DATE))
Chris@0 60 ->setParameter('{{ compared_value_type }}', $this->formatTypeOf($comparedValue))
Chris@0 61 ->setCode($this->getErrorCode())
Chris@0 62 ->addViolation();
Chris@0 63 }
Chris@0 64 }
Chris@0 65
Chris@0 66 /**
Chris@0 67 * Compares the two given values to find if their relationship is valid.
Chris@0 68 *
Chris@0 69 * @param mixed $value1 The first value to compare
Chris@0 70 * @param mixed $value2 The second value to compare
Chris@0 71 *
Chris@0 72 * @return bool true if the relationship is valid, false otherwise
Chris@0 73 */
Chris@0 74 abstract protected function compareValues($value1, $value2);
Chris@0 75
Chris@0 76 /**
Chris@0 77 * Returns the error code used if the comparison fails.
Chris@0 78 *
Chris@0 79 * @return string|null The error code or `null` if no code should be set
Chris@0 80 */
Chris@0 81 protected function getErrorCode()
Chris@0 82 {
Chris@0 83 }
Chris@0 84 }