vendor/shopware/core/Framework/Validation/DataValidator.php line 25

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Validation;
  3. use Shopware\Core\Framework\Log\Package;
  4. use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
  5. use Symfony\Component\Validator\ConstraintViolation;
  6. use Symfony\Component\Validator\ConstraintViolationList;
  7. use Symfony\Component\Validator\Validator\ValidatorInterface;
  8. #[Package('core')]
  9. class DataValidator
  10. {
  11.     /**
  12.      * @internal
  13.      */
  14.     public function __construct(private readonly ValidatorInterface $validator)
  15.     {
  16.     }
  17.     public function getViolations(array $dataDataValidationDefinition $definitionstring $path ''): ConstraintViolationList
  18.     {
  19.         $violations = new ConstraintViolationList();
  20.         $violations->addAll($this->validateProperties($data$definition$path));
  21.         $violations->addAll($this->validateSubDefinitions($data$definition$path));
  22.         $violations->addAll($this->validateListDefinitions($data$definition$path));
  23.         return $violations;
  24.     }
  25.     public function validate(array $dataDataValidationDefinition $definitionstring $path ''): void
  26.     {
  27.         $violations $this->getViolations($data$definition$path);
  28.         if ($violations->count() === 0) {
  29.             return;
  30.         }
  31.         throw new ConstraintViolationException($violations$data);
  32.     }
  33.     private function validateProperties(array $dataDataValidationDefinition $definitionstring $path): ConstraintViolationList
  34.     {
  35.         $constraintViolations = new ConstraintViolationList();
  36.         foreach ($definition->getProperties() as $propertyName => $constraints) {
  37.             $value $data[$propertyName] ?? null;
  38.             $violations $this->validator->validate($value$constraints);
  39.             /** @var ConstraintViolation $violation */
  40.             foreach ($violations as $violation) {
  41.                 $constraintViolations->add(
  42.                     new ConstraintViolation(
  43.                         $violation->getMessage(),
  44.                         $violation->getMessageTemplate(),
  45.                         $violation->getParameters(),
  46.                         $violation->getRoot(),
  47.                         $path '/' $propertyName,
  48.                         $violation->getInvalidValue(),
  49.                         $violation->getPlural(),
  50.                         $violation->getCode(),
  51.                         $violation->getConstraint(),
  52.                         $violation->getCause()
  53.                     )
  54.                 );
  55.             }
  56.         }
  57.         return $constraintViolations;
  58.     }
  59.     private function validateSubDefinitions(array $dataDataValidationDefinition $definitionstring $path): ConstraintViolationList
  60.     {
  61.         $constraintViolations = new ConstraintViolationList();
  62.         foreach ($definition->getSubDefinitions() as $propertyName => $subDefinition) {
  63.             $value $data[$propertyName] ?? [];
  64.             $constraintViolations->addAll(
  65.                 $this->getViolations($value$subDefinition$path '/' $propertyName)
  66.             );
  67.         }
  68.         return $constraintViolations;
  69.     }
  70.     private function validateListDefinitions(array $dataDataValidationDefinition $definitionstring $path): ConstraintViolationList
  71.     {
  72.         $constraintViolations = new ConstraintViolationList();
  73.         foreach ($definition->getListDefinitions() as $propertyName => $subDefinition) {
  74.             $values $data[$propertyName] ?? [];
  75.             $i 0;
  76.             foreach ($values as $item) {
  77.                 $constraintViolations->addAll(
  78.                     $this->getViolations($item$subDefinition$path '/' $propertyName '/' $i)
  79.                 );
  80.                 ++$i;
  81.             }
  82.         }
  83.         return $constraintViolations;
  84.     }
  85. }