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
RangeValidator.php
144 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 | class RangeValidator 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 Range) { |
| 22 | throw new UnexpectedTypeException($constraint, Range::class); |
| 23 | } |
| 24 | if (null === $value) { |
| 25 | return; |
| 26 | } |
| 27 | $min = $this->getLimit($constraint->minPropertyPath, $constraint->min, $constraint); |
| 28 | $max = $this->getLimit($constraint->maxPropertyPath, $constraint->max, $constraint); |
| 29 | if (!\is_numeric($value) && !$value instanceof \DateTimeInterface) { |
| 30 | if ($this->isParsableDatetimeString($min) && $this->isParsableDatetimeString($max)) { |
| 31 | $this->context->buildViolation($constraint->invalidDateTimeMessage)->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))->setCode(Range::INVALID_CHARACTERS_ERROR)->addViolation(); |
| 32 | } else { |
| 33 | $this->context->buildViolation($constraint->invalidMessage)->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))->setCode(Range::INVALID_CHARACTERS_ERROR)->addViolation(); |
| 34 | } |
| 35 | return; |
| 36 | } |
| 37 | // Convert strings to DateTimes if comparing another DateTime |
| 38 | // This allows to compare with any date/time value supported by |
| 39 | // the DateTime constructor: |
| 40 | // https://php.net/datetime.formats |
| 41 | if ($value instanceof \DateTimeInterface) { |
| 42 | $dateTimeClass = null; |
| 43 | if (\is_string($min)) { |
| 44 | $dateTimeClass = $value instanceof \DateTimeImmutable ? \DateTimeImmutable::class : \DateTime::class; |
| 45 | try { |
| 46 | $min = new $dateTimeClass($min); |
| 47 | } catch (\Exception $e) { |
| 48 | throw new ConstraintDefinitionException(\sprintf('The min value "%s" could not be converted to a "%s" instance in the "%s" constraint.', $min, $dateTimeClass, \get_debug_type($constraint))); |
| 49 | } |
| 50 | } |
| 51 | if (\is_string($max)) { |
| 52 | $dateTimeClass = $dateTimeClass ?: ($value instanceof \DateTimeImmutable ? \DateTimeImmutable::class : \DateTime::class); |
| 53 | try { |
| 54 | $max = new $dateTimeClass($max); |
| 55 | } catch (\Exception $e) { |
| 56 | throw new ConstraintDefinitionException(\sprintf('The max value "%s" could not be converted to a "%s" instance in the "%s" constraint.', $max, $dateTimeClass, \get_debug_type($constraint))); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | $hasLowerLimit = null !== $min; |
| 61 | $hasUpperLimit = null !== $max; |
| 62 | if ($hasLowerLimit && $hasUpperLimit && ($value < $min || $value > $max)) { |
| 63 | $message = $constraint->notInRangeMessage; |
| 64 | $code = Range::NOT_IN_RANGE_ERROR; |
| 65 | if ($value < $min && $constraint->deprecatedMinMessageSet) { |
| 66 | $message = $constraint->minMessage; |
| 67 | $code = Range::TOO_LOW_ERROR; |
| 68 | } |
| 69 | if ($value > $max && $constraint->deprecatedMaxMessageSet) { |
| 70 | $message = $constraint->maxMessage; |
| 71 | $code = Range::TOO_HIGH_ERROR; |
| 72 | } |
| 73 | $violationBuilder = $this->context->buildViolation($message)->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))->setParameter('{{ min }}', $this->formatValue($min, self::PRETTY_DATE))->setParameter('{{ max }}', $this->formatValue($max, self::PRETTY_DATE))->setCode($code); |
| 74 | if (null !== $constraint->maxPropertyPath) { |
| 75 | $violationBuilder->setParameter('{{ max_limit_path }}', $constraint->maxPropertyPath); |
| 76 | } |
| 77 | if (null !== $constraint->minPropertyPath) { |
| 78 | $violationBuilder->setParameter('{{ min_limit_path }}', $constraint->minPropertyPath); |
| 79 | } |
| 80 | $violationBuilder->addViolation(); |
| 81 | return; |
| 82 | } |
| 83 | if ($hasUpperLimit && $value > $max) { |
| 84 | $violationBuilder = $this->context->buildViolation($constraint->maxMessage)->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))->setParameter('{{ limit }}', $this->formatValue($max, self::PRETTY_DATE))->setCode(Range::TOO_HIGH_ERROR); |
| 85 | if (null !== $constraint->maxPropertyPath) { |
| 86 | $violationBuilder->setParameter('{{ max_limit_path }}', $constraint->maxPropertyPath); |
| 87 | } |
| 88 | if (null !== $constraint->minPropertyPath) { |
| 89 | $violationBuilder->setParameter('{{ min_limit_path }}', $constraint->minPropertyPath); |
| 90 | } |
| 91 | $violationBuilder->addViolation(); |
| 92 | return; |
| 93 | } |
| 94 | if ($hasLowerLimit && $value < $min) { |
| 95 | $violationBuilder = $this->context->buildViolation($constraint->minMessage)->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))->setParameter('{{ limit }}', $this->formatValue($min, self::PRETTY_DATE))->setCode(Range::TOO_LOW_ERROR); |
| 96 | if (null !== $constraint->maxPropertyPath) { |
| 97 | $violationBuilder->setParameter('{{ max_limit_path }}', $constraint->maxPropertyPath); |
| 98 | } |
| 99 | if (null !== $constraint->minPropertyPath) { |
| 100 | $violationBuilder->setParameter('{{ min_limit_path }}', $constraint->minPropertyPath); |
| 101 | } |
| 102 | $violationBuilder->addViolation(); |
| 103 | } |
| 104 | } |
| 105 | private function getLimit(?string $propertyPath, $default, Constraint $constraint) |
| 106 | { |
| 107 | if (null === $propertyPath) { |
| 108 | return $default; |
| 109 | } |
| 110 | if (null === ($object = $this->context->getObject())) { |
| 111 | return $default; |
| 112 | } |
| 113 | try { |
| 114 | return $this->getPropertyAccessor()->getValue($object, $propertyPath); |
| 115 | } catch (NoSuchPropertyException $e) { |
| 116 | throw new ConstraintDefinitionException(\sprintf('Invalid property path "%s" provided to "%s" constraint: ', $propertyPath, \get_debug_type($constraint)) . $e->getMessage(), 0, $e); |
| 117 | } catch (UninitializedPropertyException $e) { |
| 118 | return null; |
| 119 | } |
| 120 | } |
| 121 | private function getPropertyAccessor() : PropertyAccessorInterface |
| 122 | { |
| 123 | if (null === $this->propertyAccessor) { |
| 124 | $this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
| 125 | } |
| 126 | return $this->propertyAccessor; |
| 127 | } |
| 128 | private function isParsableDatetimeString($boundary) : bool |
| 129 | { |
| 130 | if (null === $boundary) { |
| 131 | return \true; |
| 132 | } |
| 133 | if (!\is_string($boundary)) { |
| 134 | return \false; |
| 135 | } |
| 136 | try { |
| 137 | new \DateTime($boundary); |
| 138 | } catch (\Exception $e) { |
| 139 | return \false; |
| 140 | } |
| 141 | return \true; |
| 142 | } |
| 143 | } |
| 144 |