Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

vendor/symfony/validator/Constraint.php line 111

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Validator;
  11. use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
  12. use Symfony\Component\Validator\Exception\InvalidArgumentException;
  13. use Symfony\Component\Validator\Exception\InvalidOptionsException;
  14. use Symfony\Component\Validator\Exception\MissingOptionsException;
  15. /**
  16.  * Contains the properties of a constraint definition.
  17.  *
  18.  * A constraint can be defined on a class, a property or a getter method.
  19.  * The Constraint class encapsulates all the configuration required for
  20.  * validating this class, property or getter result successfully.
  21.  *
  22.  * Constraint instances are immutable and serializable.
  23.  *
  24.  * @author Bernhard Schussek <bschussek@gmail.com>
  25.  */
  26. abstract class Constraint
  27. {
  28.     /**
  29.      * The name of the group given to all constraints with no explicit group.
  30.      */
  31.     public const DEFAULT_GROUP 'Default';
  32.     /**
  33.      * Marks a constraint that can be put onto classes.
  34.      */
  35.     public const CLASS_CONSTRAINT 'class';
  36.     /**
  37.      * Marks a constraint that can be put onto properties.
  38.      */
  39.     public const PROPERTY_CONSTRAINT 'property';
  40.     /**
  41.      * Maps error codes to the names of their constants.
  42.      */
  43.     protected static $errorNames = [];
  44.     /**
  45.      * Domain-specific data attached to a constraint.
  46.      *
  47.      * @var mixed
  48.      */
  49.     public $payload;
  50.     /**
  51.      * The groups that the constraint belongs to.
  52.      *
  53.      * @var string[]
  54.      */
  55.     public $groups;
  56.     /**
  57.      * Returns the name of the given error code.
  58.      *
  59.      * @return string
  60.      *
  61.      * @throws InvalidArgumentException If the error code does not exist
  62.      */
  63.     public static function getErrorName(string $errorCode)
  64.     {
  65.         if (!isset(static::$errorNames[$errorCode])) {
  66.             throw new InvalidArgumentException(sprintf('The error code "%s" does not exist for constraint of type "%s".'$errorCode, static::class));
  67.         }
  68.         return static::$errorNames[$errorCode];
  69.     }
  70.     /**
  71.      * Initializes the constraint with options.
  72.      *
  73.      * You should pass an associative array. The keys should be the names of
  74.      * existing properties in this class. The values should be the value for these
  75.      * properties.
  76.      *
  77.      * Alternatively you can override the method getDefaultOption() to return the
  78.      * name of an existing property. If no associative array is passed, this
  79.      * property is set instead.
  80.      *
  81.      * You can force that certain options are set by overriding
  82.      * getRequiredOptions() to return the names of these options. If any
  83.      * option is not set here, an exception is thrown.
  84.      *
  85.      * @param mixed    $options The options (as associative array)
  86.      *                          or the value for the default
  87.      *                          option (any other type)
  88.      * @param string[] $groups  An array of validation groups
  89.      * @param mixed    $payload Domain-specific data attached to a constraint
  90.      *
  91.      * @throws InvalidOptionsException       When you pass the names of non-existing
  92.      *                                       options
  93.      * @throws MissingOptionsException       When you don't pass any of the options
  94.      *                                       returned by getRequiredOptions()
  95.      * @throws ConstraintDefinitionException When you don't pass an associative
  96.      *                                       array, but getDefaultOption() returns
  97.      *                                       null
  98.      */
  99.     public function __construct($options null, array $groups null$payload null)
  100.     {
  101.         unset($this->groups); // enable lazy initialization
  102.         $options $this->normalizeOptions($options);
  103.         if (null !== $groups) {
  104.             $options['groups'] = $groups;
  105.         }
  106.         $options['payload'] = $payload ?? $options['payload'] ?? null;
  107.         foreach ($options as $name => $value) {
  108.             $this->$name $value;
  109.         }
  110.     }
  111.     protected function normalizeOptions($options): array
  112.     {
  113.         $normalizedOptions = [];
  114.         $defaultOption $this->getDefaultOption();
  115.         $invalidOptions = [];
  116.         $missingOptions array_flip((array) $this->getRequiredOptions());
  117.         $knownOptions get_class_vars(static::class);
  118.         if (\is_array($options) && isset($options['value']) && !property_exists($this'value')) {
  119.             if (null === $defaultOption) {
  120.                 throw new ConstraintDefinitionException(sprintf('No default option is configured for constraint "%s".', static::class));
  121.             }
  122.             $options[$defaultOption] = $options['value'];
  123.             unset($options['value']);
  124.         }
  125.         if (\is_array($options)) {
  126.             reset($options);
  127.         }
  128.         if ($options && \is_array($options) && \is_string(key($options))) {
  129.             foreach ($options as $option => $value) {
  130.                 if (\array_key_exists($option$knownOptions)) {
  131.                     $normalizedOptions[$option] = $value;
  132.                     unset($missingOptions[$option]);
  133.                 } else {
  134.                     $invalidOptions[] = $option;
  135.                 }
  136.             }
  137.         } elseif (null !== $options && !(\is_array($options) && === \count($options))) {
  138.             if (null === $defaultOption) {
  139.                 throw new ConstraintDefinitionException(sprintf('No default option is configured for constraint "%s".', static::class));
  140.             }
  141.             if (\array_key_exists($defaultOption$knownOptions)) {
  142.                 $normalizedOptions[$defaultOption] = $options;
  143.                 unset($missingOptions[$defaultOption]);
  144.             } else {
  145.                 $invalidOptions[] = $defaultOption;
  146.             }
  147.         }
  148.         if (\count($invalidOptions) > 0) {
  149.             throw new InvalidOptionsException(sprintf('The options "%s" do not exist in constraint "%s".'implode('", "'$invalidOptions), static::class), $invalidOptions);
  150.         }
  151.         if (\count($missingOptions) > 0) {
  152.             throw new MissingOptionsException(sprintf('The options "%s" must be set for constraint "%s".'implode('", "'array_keys($missingOptions)), static::class), array_keys($missingOptions));
  153.         }
  154.         return $normalizedOptions;
  155.     }
  156.     /**
  157.      * Sets the value of a lazily initialized option.
  158.      *
  159.      * Corresponding properties are added to the object on first access. Hence
  160.      * this method will be called at most once per constraint instance and
  161.      * option name.
  162.      *
  163.      * @param mixed $value The value to set
  164.      *
  165.      * @throws InvalidOptionsException If an invalid option name is given
  166.      */
  167.     public function __set(string $option$value)
  168.     {
  169.         if ('groups' === $option) {
  170.             $this->groups = (array) $value;
  171.             return;
  172.         }
  173.         throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint "%s".'$option, static::class), [$option]);
  174.     }
  175.     /**
  176.      * Returns the value of a lazily initialized option.
  177.      *
  178.      * Corresponding properties are added to the object on first access. Hence
  179.      * this method will be called at most once per constraint instance and
  180.      * option name.
  181.      *
  182.      * @return mixed
  183.      *
  184.      * @throws InvalidOptionsException If an invalid option name is given
  185.      */
  186.     public function __get(string $option)
  187.     {
  188.         if ('groups' === $option) {
  189.             $this->groups = [self::DEFAULT_GROUP];
  190.             return $this->groups;
  191.         }
  192.         throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint "%s".'$option, static::class), [$option]);
  193.     }
  194.     /**
  195.      * @return bool
  196.      */
  197.     public function __isset(string $option)
  198.     {
  199.         return 'groups' === $option;
  200.     }
  201.     /**
  202.      * Adds the given group if this constraint is in the Default group.
  203.      */
  204.     public function addImplicitGroupName(string $group)
  205.     {
  206.         if (null === $this->groups && \array_key_exists('groups', (array) $this)) {
  207.             throw new \LogicException(sprintf('"%s::$groups" is set to null. Did you forget to call "%s::__construct()"?', static::class, self::class));
  208.         }
  209.         if (\in_array(self::DEFAULT_GROUP$this->groups) && !\in_array($group$this->groups)) {
  210.             $this->groups[] = $group;
  211.         }
  212.     }
  213.     /**
  214.      * Returns the name of the default option.
  215.      *
  216.      * Override this method to define a default option.
  217.      *
  218.      * @return string|null
  219.      *
  220.      * @see __construct()
  221.      */
  222.     public function getDefaultOption()
  223.     {
  224.         return null;
  225.     }
  226.     /**
  227.      * Returns the name of the required options.
  228.      *
  229.      * Override this method if you want to define required options.
  230.      *
  231.      * @return string[]
  232.      *
  233.      * @see __construct()
  234.      */
  235.     public function getRequiredOptions()
  236.     {
  237.         return [];
  238.     }
  239.     /**
  240.      * Returns the name of the class that validates this constraint.
  241.      *
  242.      * By default, this is the fully qualified name of the constraint class
  243.      * suffixed with "Validator". You can override this method to change that
  244.      * behavior.
  245.      *
  246.      * @return string
  247.      */
  248.     public function validatedBy()
  249.     {
  250.         return static::class.'Validator';
  251.     }
  252.     /**
  253.      * Returns whether the constraint can be put onto classes, properties or
  254.      * both.
  255.      *
  256.      * This method should return one or more of the constants
  257.      * Constraint::CLASS_CONSTRAINT and Constraint::PROPERTY_CONSTRAINT.
  258.      *
  259.      * @return string|string[] One or more constant values
  260.      */
  261.     public function getTargets()
  262.     {
  263.         return self::PROPERTY_CONSTRAINT;
  264.     }
  265.     /**
  266.      * Optimizes the serialized value to minimize storage space.
  267.      *
  268.      * @internal
  269.      */
  270.     public function __sleep(): array
  271.     {
  272.         // Initialize "groups" option if it is not set
  273.         $this->groups;
  274.         return array_keys(get_object_vars($this));
  275.     }
  276. }