AbstractComparison.php
1 year ago
AbstractComparisonValidator.php
1 year ago
All.php
1 year ago
AllValidator.php
4 years ago
AtLeastOneOf.php
1 year ago
AtLeastOneOfValidator.php
1 year ago
Bic.php
1 year ago
BicValidator.php
1 year ago
Blank.php
1 year ago
BlankValidator.php
4 years ago
Callback.php
1 year ago
CallbackValidator.php
4 years ago
CardScheme.php
1 year ago
CardSchemeValidator.php
1 year ago
Cascade.php
1 year ago
Choice.php
1 year ago
ChoiceValidator.php
1 year ago
Cidr.php
1 year ago
CidrValidator.php
4 years ago
Collection.php
1 year ago
CollectionValidator.php
4 years ago
Composite.php
1 year ago
Compound.php
4 years ago
CompoundValidator.php
4 years ago
Count.php
1 year ago
CountValidator.php
4 years ago
Country.php
1 year ago
CountryValidator.php
4 years ago
CssColor.php
1 year ago
CssColorValidator.php
1 year ago
Currency.php
1 year ago
CurrencyValidator.php
4 years ago
Date.php
1 year ago
DateTime.php
1 year ago
DateTimeValidator.php
2 years ago
DateValidator.php
1 year ago
DisableAutoMapping.php
1 year ago
DivisibleBy.php
4 years ago
DivisibleByValidator.php
4 years ago
Email.php
1 year ago
EmailValidator.php
1 year ago
EnableAutoMapping.php
1 year ago
EqualTo.php
4 years ago
EqualToValidator.php
4 years ago
Existence.php
4 years ago
Expression.php
1 year ago
ExpressionLanguageSyntax.php
1 year ago
ExpressionLanguageSyntaxValidator.php
1 year ago
ExpressionValidator.php
1 year ago
File.php
1 year ago
FileValidator.php
4 years ago
GreaterThan.php
4 years ago
GreaterThanOrEqual.php
4 years ago
GreaterThanOrEqualValidator.php
4 years ago
GreaterThanValidator.php
4 years ago
GroupSequence.php
4 years ago
GroupSequenceProvider.php
4 years ago
Hostname.php
1 year ago
HostnameValidator.php
4 years ago
Iban.php
1 year ago
IbanValidator.php
1 year ago
IdenticalTo.php
4 years ago
IdenticalToValidator.php
4 years ago
Image.php
1 year ago
ImageValidator.php
4 years ago
Ip.php
1 year ago
IpValidator.php
4 years ago
IsFalse.php
1 year ago
IsFalseValidator.php
4 years ago
IsNull.php
1 year ago
IsNullValidator.php
4 years ago
IsTrue.php
1 year ago
IsTrueValidator.php
4 years ago
Isbn.php
1 year ago
IsbnValidator.php
1 year ago
Isin.php
1 year ago
IsinValidator.php
4 years ago
Issn.php
1 year ago
IssnValidator.php
4 years ago
Json.php
1 year ago
JsonValidator.php
1 year ago
Language.php
1 year ago
LanguageValidator.php
4 years ago
Length.php
1 year ago
LengthValidator.php
4 years ago
LessThan.php
4 years ago
LessThanOrEqual.php
4 years ago
LessThanOrEqualValidator.php
4 years ago
LessThanValidator.php
4 years ago
Locale.php
1 year ago
LocaleValidator.php
4 years ago
Luhn.php
1 year ago
LuhnValidator.php
4 years ago
Negative.php
4 years ago
NegativeOrZero.php
4 years ago
NotBlank.php
1 year ago
NotBlankValidator.php
4 years ago
NotCompromisedPassword.php
1 year ago
NotCompromisedPasswordValidator.php
1 year ago
NotEqualTo.php
4 years ago
NotEqualToValidator.php
4 years ago
NotIdenticalTo.php
4 years ago
NotIdenticalToValidator.php
4 years ago
NotNull.php
1 year ago
NotNullValidator.php
4 years ago
NumberConstraintTrait.php
4 years ago
Optional.php
4 years ago
Positive.php
4 years ago
PositiveOrZero.php
4 years ago
Range.php
1 year ago
RangeValidator.php
1 year ago
Regex.php
1 year ago
RegexValidator.php
4 years ago
Required.php
4 years ago
Sequentially.php
1 year ago
SequentiallyValidator.php
4 years ago
Time.php
1 year ago
TimeValidator.php
1 year ago
Timezone.php
1 year ago
TimezoneValidator.php
1 year ago
Traverse.php
4 years ago
Type.php
1 year ago
TypeValidator.php
4 years ago
Ulid.php
1 year ago
UlidValidator.php
2 years ago
Unique.php
1 year ago
UniqueValidator.php
1 year ago
Url.php
1 year ago
UrlValidator.php
1 year ago
Uuid.php
1 year ago
UuidValidator.php
4 years ago
Valid.php
1 year ago
ValidValidator.php
4 years ago
ZeroComparisonConstraintTrait.php
1 year ago
index.php
3 years ago
AbstractComparisonValidator.php
75 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\Validator\Constraints; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; |
| 5 | use MailPoetVendor\Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException; |
| 6 | use MailPoetVendor\Symfony\Component\PropertyAccess\PropertyAccess; |
| 7 | use MailPoetVendor\Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
| 8 | use MailPoetVendor\Symfony\Component\Validator\Constraint; |
| 9 | use MailPoetVendor\Symfony\Component\Validator\ConstraintValidator; |
| 10 | use MailPoetVendor\Symfony\Component\Validator\Exception\ConstraintDefinitionException; |
| 11 | use MailPoetVendor\Symfony\Component\Validator\Exception\UnexpectedTypeException; |
| 12 | abstract class AbstractComparisonValidator extends ConstraintValidator |
| 13 | { |
| 14 | private $propertyAccessor; |
| 15 | public function __construct(?PropertyAccessorInterface $propertyAccessor = null) |
| 16 | { |
| 17 | $this->propertyAccessor = $propertyAccessor; |
| 18 | } |
| 19 | public function validate($value, Constraint $constraint) |
| 20 | { |
| 21 | if (!$constraint instanceof AbstractComparison) { |
| 22 | throw new UnexpectedTypeException($constraint, AbstractComparison::class); |
| 23 | } |
| 24 | if (null === $value) { |
| 25 | return; |
| 26 | } |
| 27 | if ($path = $constraint->propertyPath) { |
| 28 | if (null === ($object = $this->context->getObject())) { |
| 29 | return; |
| 30 | } |
| 31 | try { |
| 32 | $comparedValue = $this->getPropertyAccessor()->getValue($object, $path); |
| 33 | } catch (NoSuchPropertyException $e) { |
| 34 | throw new ConstraintDefinitionException(\sprintf('Invalid property path "%s" provided to "%s" constraint: ', $path, \get_debug_type($constraint)) . $e->getMessage(), 0, $e); |
| 35 | } catch (UninitializedPropertyException $e) { |
| 36 | $comparedValue = null; |
| 37 | } |
| 38 | } else { |
| 39 | $comparedValue = $constraint->value; |
| 40 | } |
| 41 | // Convert strings to DateTimes if comparing another DateTime |
| 42 | // This allows to compare with any date/time value supported by |
| 43 | // the DateTime constructor: |
| 44 | // https://php.net/datetime.formats |
| 45 | if (\is_string($comparedValue) && $value instanceof \DateTimeInterface) { |
| 46 | // If $value is immutable, convert the compared value to a DateTimeImmutable too, otherwise use DateTime |
| 47 | $dateTimeClass = $value instanceof \DateTimeImmutable ? \DateTimeImmutable::class : \DateTime::class; |
| 48 | try { |
| 49 | $comparedValue = new $dateTimeClass($comparedValue); |
| 50 | } catch (\Exception $e) { |
| 51 | throw new ConstraintDefinitionException(\sprintf('The compared value "%s" could not be converted to a "%s" instance in the "%s" constraint.', $comparedValue, $dateTimeClass, \get_debug_type($constraint))); |
| 52 | } |
| 53 | } |
| 54 | if (!$this->compareValues($value, $comparedValue)) { |
| 55 | $violationBuilder = $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))->setCode($this->getErrorCode()); |
| 56 | if (null !== $path) { |
| 57 | $violationBuilder->setParameter('{{ compared_value_path }}', $path); |
| 58 | } |
| 59 | $violationBuilder->addViolation(); |
| 60 | } |
| 61 | } |
| 62 | private function getPropertyAccessor() : PropertyAccessorInterface |
| 63 | { |
| 64 | if (null === $this->propertyAccessor) { |
| 65 | $this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
| 66 | } |
| 67 | return $this->propertyAccessor; |
| 68 | } |
| 69 | protected abstract function compareValues($value1, $value2); |
| 70 | protected function getErrorCode() |
| 71 | { |
| 72 | return null; |
| 73 | } |
| 74 | } |
| 75 |