Command
1 year ago
Constraints
1 year ago
Context
1 year ago
Exception
3 years ago
Mapping
3 months ago
Util
3 years ago
Validator
6 months ago
Violation
3 years ago
Constraint.php
1 year ago
ConstraintValidator.php
1 year ago
ConstraintValidatorFactory.php
4 years ago
ConstraintValidatorFactoryInterface.php
4 years ago
ConstraintValidatorInterface.php
4 years ago
ConstraintViolation.php
1 year ago
ConstraintViolationInterface.php
4 years ago
ConstraintViolationList.php
4 years ago
ConstraintViolationListInterface.php
4 years ago
ContainerConstraintValidatorFactory.php
4 years ago
GroupSequenceProviderInterface.php
4 years ago
ObjectInitializerInterface.php
4 years ago
Validation.php
4 years ago
ValidatorBuilder.php
4 years ago
index.php
3 years ago
Constraint.php
133 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\Validator; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Symfony\Component\Validator\Exception\ConstraintDefinitionException; |
| 5 | use MailPoetVendor\Symfony\Component\Validator\Exception\InvalidArgumentException; |
| 6 | use MailPoetVendor\Symfony\Component\Validator\Exception\InvalidOptionsException; |
| 7 | use MailPoetVendor\Symfony\Component\Validator\Exception\MissingOptionsException; |
| 8 | abstract class Constraint |
| 9 | { |
| 10 | public const DEFAULT_GROUP = 'Default'; |
| 11 | public const CLASS_CONSTRAINT = 'class'; |
| 12 | public const PROPERTY_CONSTRAINT = 'property'; |
| 13 | protected static $errorNames = []; |
| 14 | public $payload; |
| 15 | public $groups; |
| 16 | public static function getErrorName(string $errorCode) |
| 17 | { |
| 18 | if (!isset(static::$errorNames[$errorCode])) { |
| 19 | throw new InvalidArgumentException(\sprintf('The error code "%s" does not exist for constraint of type "%s".', $errorCode, static::class)); |
| 20 | } |
| 21 | return static::$errorNames[$errorCode]; |
| 22 | } |
| 23 | public function __construct($options = null, ?array $groups = null, $payload = null) |
| 24 | { |
| 25 | unset($this->groups); |
| 26 | // enable lazy initialization |
| 27 | $options = $this->normalizeOptions($options); |
| 28 | if (null !== $groups) { |
| 29 | $options['groups'] = $groups; |
| 30 | } |
| 31 | $options['payload'] = $payload ?? $options['payload'] ?? null; |
| 32 | foreach ($options as $name => $value) { |
| 33 | $this->{$name} = $value; |
| 34 | } |
| 35 | } |
| 36 | protected function normalizeOptions($options) : array |
| 37 | { |
| 38 | $normalizedOptions = []; |
| 39 | $defaultOption = $this->getDefaultOption(); |
| 40 | $invalidOptions = []; |
| 41 | $missingOptions = \array_flip((array) $this->getRequiredOptions()); |
| 42 | $knownOptions = \get_class_vars(static::class); |
| 43 | if (\is_array($options) && isset($options['value']) && !\property_exists($this, 'value')) { |
| 44 | if (null === $defaultOption) { |
| 45 | throw new ConstraintDefinitionException(\sprintf('No default option is configured for constraint "%s".', static::class)); |
| 46 | } |
| 47 | $options[$defaultOption] = $options['value']; |
| 48 | unset($options['value']); |
| 49 | } |
| 50 | if (\is_array($options)) { |
| 51 | \reset($options); |
| 52 | } |
| 53 | if ($options && \is_array($options) && \is_string(\key($options))) { |
| 54 | foreach ($options as $option => $value) { |
| 55 | if (\array_key_exists($option, $knownOptions)) { |
| 56 | $normalizedOptions[$option] = $value; |
| 57 | unset($missingOptions[$option]); |
| 58 | } else { |
| 59 | $invalidOptions[] = $option; |
| 60 | } |
| 61 | } |
| 62 | } elseif (null !== $options && !(\is_array($options) && 0 === \count($options))) { |
| 63 | if (null === $defaultOption) { |
| 64 | throw new ConstraintDefinitionException(\sprintf('No default option is configured for constraint "%s".', static::class)); |
| 65 | } |
| 66 | if (\array_key_exists($defaultOption, $knownOptions)) { |
| 67 | $normalizedOptions[$defaultOption] = $options; |
| 68 | unset($missingOptions[$defaultOption]); |
| 69 | } else { |
| 70 | $invalidOptions[] = $defaultOption; |
| 71 | } |
| 72 | } |
| 73 | if (\count($invalidOptions) > 0) { |
| 74 | throw new InvalidOptionsException(\sprintf('The options "%s" do not exist in constraint "%s".', \implode('", "', $invalidOptions), static::class), $invalidOptions); |
| 75 | } |
| 76 | if (\count($missingOptions) > 0) { |
| 77 | throw new MissingOptionsException(\sprintf('The options "%s" must be set for constraint "%s".', \implode('", "', \array_keys($missingOptions)), static::class), \array_keys($missingOptions)); |
| 78 | } |
| 79 | return $normalizedOptions; |
| 80 | } |
| 81 | public function __set(string $option, $value) |
| 82 | { |
| 83 | if ('groups' === $option) { |
| 84 | $this->groups = (array) $value; |
| 85 | return; |
| 86 | } |
| 87 | throw new InvalidOptionsException(\sprintf('The option "%s" does not exist in constraint "%s".', $option, static::class), [$option]); |
| 88 | } |
| 89 | public function __get(string $option) |
| 90 | { |
| 91 | if ('groups' === $option) { |
| 92 | $this->groups = [self::DEFAULT_GROUP]; |
| 93 | return $this->groups; |
| 94 | } |
| 95 | throw new InvalidOptionsException(\sprintf('The option "%s" does not exist in constraint "%s".', $option, static::class), [$option]); |
| 96 | } |
| 97 | public function __isset(string $option) |
| 98 | { |
| 99 | return 'groups' === $option; |
| 100 | } |
| 101 | public function addImplicitGroupName(string $group) |
| 102 | { |
| 103 | if (null === $this->groups && \array_key_exists('groups', (array) $this)) { |
| 104 | throw new \LogicException(\sprintf('"%s::$groups" is set to null. Did you forget to call "%s::__construct()"?', static::class, self::class)); |
| 105 | } |
| 106 | if (\in_array(self::DEFAULT_GROUP, $this->groups) && !\in_array($group, $this->groups)) { |
| 107 | $this->groups[] = $group; |
| 108 | } |
| 109 | } |
| 110 | public function getDefaultOption() |
| 111 | { |
| 112 | return null; |
| 113 | } |
| 114 | public function getRequiredOptions() |
| 115 | { |
| 116 | return []; |
| 117 | } |
| 118 | public function validatedBy() |
| 119 | { |
| 120 | return static::class . 'Validator'; |
| 121 | } |
| 122 | public function getTargets() |
| 123 | { |
| 124 | return self::PROPERTY_CONSTRAINT; |
| 125 | } |
| 126 | public function __sleep() : array |
| 127 | { |
| 128 | // Initialize "groups" option if it is not set |
| 129 | $this->groups; |
| 130 | return \array_keys(\get_object_vars($this)); |
| 131 | } |
| 132 | } |
| 133 |