vendor/symfony/validator/Constraints/Collection.php line 24

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\Constraints;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
  13. /**
  14.  * @Annotation
  15.  * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
  16.  *
  17.  * @author Bernhard Schussek <bschussek@gmail.com>
  18.  */
  19. #[\Attribute(\Attribute::TARGET_PROPERTY \Attribute::TARGET_METHOD \Attribute::IS_REPEATABLE)]
  20. class Collection extends Composite
  21. {
  22.     public const MISSING_FIELD_ERROR '2fa2158c-2a7f-484b-98aa-975522539ff8';
  23.     public const NO_SUCH_FIELD_ERROR '7703c766-b5d5-4cef-ace7-ae0dd82304e9';
  24.     protected static $errorNames = [
  25.         self::MISSING_FIELD_ERROR => 'MISSING_FIELD_ERROR',
  26.         self::NO_SUCH_FIELD_ERROR => 'NO_SUCH_FIELD_ERROR',
  27.     ];
  28.     public $fields = [];
  29.     public $allowExtraFields false;
  30.     public $allowMissingFields false;
  31.     public $extraFieldsMessage 'This field was not expected.';
  32.     public $missingFieldsMessage 'This field is missing.';
  33.     /**
  34.      * {@inheritdoc}
  35.      */
  36.     public function __construct($fields null, ?array $groups null$payload null, ?bool $allowExtraFields null, ?bool $allowMissingFields null, ?string $extraFieldsMessage null, ?string $missingFieldsMessage null)
  37.     {
  38.         if (self::isFieldsOption($fields)) {
  39.             $fields = ['fields' => $fields];
  40.         }
  41.         parent::__construct($fields$groups$payload);
  42.         $this->allowExtraFields $allowExtraFields ?? $this->allowExtraFields;
  43.         $this->allowMissingFields $allowMissingFields ?? $this->allowMissingFields;
  44.         $this->extraFieldsMessage $extraFieldsMessage ?? $this->extraFieldsMessage;
  45.         $this->missingFieldsMessage $missingFieldsMessage ?? $this->missingFieldsMessage;
  46.     }
  47.     /**
  48.      * {@inheritdoc}
  49.      */
  50.     protected function initializeNestedConstraints()
  51.     {
  52.         parent::initializeNestedConstraints();
  53.         if (!\is_array($this->fields)) {
  54.             throw new ConstraintDefinitionException(sprintf('The option "fields" is expected to be an array in constraint "%s".'__CLASS__));
  55.         }
  56.         foreach ($this->fields as $fieldName => $field) {
  57.             // the XmlFileLoader and YamlFileLoader pass the field Optional
  58.             // and Required constraint as an array with exactly one element
  59.             if (\is_array($field) && == \count($field)) {
  60.                 $this->fields[$fieldName] = $field $field[0];
  61.             }
  62.             if (!$field instanceof Optional && !$field instanceof Required) {
  63.                 $this->fields[$fieldName] = new Required($field);
  64.             }
  65.         }
  66.     }
  67.     public function getRequiredOptions()
  68.     {
  69.         return ['fields'];
  70.     }
  71.     protected function getCompositeOption()
  72.     {
  73.         return 'fields';
  74.     }
  75.     private static function isFieldsOption($options): bool
  76.     {
  77.         if (!\is_array($options)) {
  78.             return false;
  79.         }
  80.         foreach ($options as $optionOrField) {
  81.             if ($optionOrField instanceof Constraint) {
  82.                 return true;
  83.             }
  84.             if (null === $optionOrField) {
  85.                 continue;
  86.             }
  87.             if (!\is_array($optionOrField)) {
  88.                 return false;
  89.             }
  90.             if ($optionOrField && !($optionOrField[0] ?? null) instanceof Constraint) {
  91.                 return false;
  92.             }
  93.         }
  94.         return true;
  95.     }
  96. }