addAll example

continue;
            }

            if ($command->getDefinition()->getClass() !== CategoryDefinition::class) {
                continue;
            }

            if (!isset($command->getPayload()['type'])) {
                continue;
            }

            $violationList->addAll($this->checkTypeChange($command$event));
        }

        if ($violationList->count() > 0) {
            $event->getExceptions()->add(new WriteConstraintViolationException($violationList));

            return;
        }
    }

    private function checkTypeChange(WriteCommand $command, PostWriteValidationEvent $event): ConstraintViolationListInterface
    {
        
/** * @internal */
    public function __construct(private readonly ValidatorInterface $validator)
    {
    }

    public function getViolations(array $data, DataValidationDefinition $definition, string $path = ''): ConstraintViolationList
    {
        $violations = new ConstraintViolationList();

        $violations->addAll($this->validateProperties($data$definition$path));
        $violations->addAll($this->validateSubDefinitions($data$definition$path));
        $violations->addAll($this->validateListDefinitions($data$definition$path));

        return $violations;
    }

    public function validate(array $data, DataValidationDefinition $definition, string $path = ''): void
    {
        $violations = $this->getViolations($data$definition$path);
        if ($violations->count() === 0) {
            return;
        }

    else {
      $values = [];
    }

    $validator = $this->getTypedDataManager()->getValidator();
    foreach ($values as $value) {
      $constraints = array_values($this->getConstraintObjects());
      if ($definition->isMultiple()) {
        $violations = new ConstraintViolationList();
        foreach ($value as $item) {
          $violations->addAll($validator->validate($item$constraints));
        }
      }
      else {
        $violations = $validator->validate($value$constraints);
      }
      foreach ($violations as $delta => $violation) {
        // Remove any violation that does not correspond to the constraints.         if (!in_array($violation->getConstraint()$constraints)) {
          $violations->remove($delta);
        }
      }
      
PreWriteValidationEvent::class => 'preValidate',
        ];
    }

    public function preValidate(PreWriteValidationEvent $event): void
    {
        if ($event->getContext()->getVersionId() !== Defaults::LIVE_VERSION) {
            return;
        }

        $violations = new ConstraintViolationList();
        $violations->addAll($this->getDeletedSystemTranslationViolations($event->getCommands()));

        if ($violations->count()) {
            $event->getExceptions()->add(new WriteConstraintViolationException($violations));
        }
    }

    /** * @param list<WriteCommand> $writeCommands */
    private function getDeletedSystemTranslationViolations(array $writeCommands): ConstraintViolationList
    {
        

        $writeException = $event->getExceptions();
        $commands = $event->getCommands();
        $violationList = new ConstraintViolationList();

        foreach ($commands as $command) {
            if (!($command instanceof InsertCommand) || $command->getDefinition()->getClass() !== LandingPageDefinition::class) {
                continue;
            }

            if (!$this->hasAnotherValidCommand($commands$command)) {
                $violationList->addAll(
                    $this->validator->startContext()
                        ->atPath($command->getPath() . '/salesChannels')
                        ->validate(null, [new NotBlank()])
                        ->getViolations()
                );
                $writeException->add(new WriteConstraintViolationException($violationList));
            }
        }
    }

    /** * @param WriteCommand[] $commands */
$this->assertSame($violation$this->list[0]);
    }

    public function testAddAll()
    {
        $violations = [
            10 => $this->getViolation('Error 1'),
            20 => $this->getViolation('Error 2'),
            30 => $this->getViolation('Error 3'),
        ];
        $otherList = new ConstraintViolationList($violations);
        $this->list->addAll($otherList);

        $this->assertCount(3, $this->list);

        $this->assertSame($violations[10]$this->list[0]);
        $this->assertSame($violations[20]$this->list[1]);
        $this->assertSame($violations[30]$this->list[2]);
    }

    public function testIterator()
    {
        $violations = [
            


    public function postValidate(PostWriteValidationEvent $event): void
    {
        $commands = $event->getCommands();
        $affectedIds = $this->getAffectedIds($commands);
        if (\count($affectedIds) === 0) {
            return;
        }

        $violations = new ConstraintViolationList();
        $violations->addAll($this->getInheritanceViolations($affectedIds));
        $violations->addAll($this->getMissingTranslationCodeViolations($affectedIds));

        if ($violations->count() > 0) {
            $event->getExceptions()->add(new WriteConstraintViolationException($violations));
        }
    }

    public function preValidate(PreWriteValidationEvent $event): void
    {
        $commands = $event->getCommands();

        
return $value ?? [];
    }

    /** * @param array<string, array<Constraint>> $fieldValidations * @param array<mixed> $payload * @param array<string> $missingProperties */
    private function validateConsistence(array $fieldValidations, array $payload, ConstraintViolationList $violationList, array $missingProperties): void
    {
        foreach ($fieldValidations as $fieldName => $validations) {
            $violationList->addAll(
                $this->validator->startContext()
                    ->atPath('/value/' . $fieldName)
                    ->validate($payload[$fieldName] ?? null, $validations)
                    ->getViolations()
            );
        }

        foreach ($payload as $fieldName => $_value) {
            if (!\array_key_exists($fieldName$fieldValidations) && $fieldName !== '_name' && !isset($missingProperties[$fieldName])) {
                $violationList->add(
                    $this->buildViolation(
                        
if ($error->canUseMessageForUser()) {
                            $parameters['hint'] = $error->getMessage();
                        }
                        $template = 'This value should be of type {{ type }}.';
                        $message = $trans($template$parameters, 'validators');
                        $violations->add(new ConstraintViolation($message$template$parameters, null, $error->getPath(), null));
                    }
                    $payload = $e->getData();
                }

                if (null !== $payload) {
                    $violations->addAll($this->validator->validate($payload, null, $argument->validationGroups ?? null));
                }

                if (\count($violations)) {
                    throw new HttpException($validationFailedCodeimplode("\n", array_map(static fn ($e) => $e->getMessage()iterator_to_array($violations)))new ValidationFailedException($payload$violations));
                }
            } else {
                try {
                    $payload = $this->$payloadMapper($request$type$argument);
                } catch (PartialDenormalizationException $e) {
                    throw new HttpException($validationFailedCodeimplode("\n", array_map(static fn ($e) => $e->getMessage()$e->getErrors()))$e);
                }
            }

    private function assertValidAddresses(array $addresses): void
    {
        $constraints = (new ConstraintBuilder())
            ->isNotBlank()
            ->isEmail()
            ->getConstraints();

        $violations = new ConstraintViolationList();
        foreach ($addresses as $address) {
            $violations->addAll($this->validator->validate($address$constraints));
        }

        if ($violations->count() > 0) {
            throw new ConstraintViolationException($violations$addresses);
        }
    }

    /** * @param string[] $addresses * * @return string[] */
/** * {@inheritdoc} */
  public function validateContexts() {
    $violations = new ConstraintViolationList();

    // @todo Implement the Symfony Validator component to let the validator     // traverse and set property paths accordingly.     // See https://www.drupal.org/project/drupal/issues/3153847.     foreach ($this->getContexts() as $context) {
      $violations->addAll($context->validate());
    }
    return $violations;
  }

  /** * {@inheritdoc} */
  public function getCacheContexts() {
    $cache_contexts = [];
    // Applied contexts can affect the cache contexts when this plugin is     // involved in caching, collect and return them.
'This "_name" value (%value%) is invalid.',
                    ['%value%' => 'NULL'],
                    $basePath . '/_name'
                )
            );
        } else {
            $rule = $this->ruleConditionRegistry->getRuleInstance($type);
            // do not validate container             if (!$rule instanceof Container) {
                $rule->assign($data);
                $validations = $rule->getConstraints();
                $violationList->addAll($this->validateConsistence($basePath$validations$data));
            }
        }

        if (\array_key_exists('rules', $data)) {
            foreach ($data['rules'] as $rule) {
                $violationList->addAll($this->validateRules($rule$basePath . '/' . $type));
            }
        }

        return $violationList;
    }

    
if ($error->canUseMessageForUser()) {
                            $parameters['hint'] = $error->getMessage();
                        }
                        $template = 'This value should be of type {{ type }}.';
                        $message = $trans($template$parameters, 'validators');
                        $violations->add(new ConstraintViolation($message$template$parameters, null, $error->getPath(), null));
                    }
                    $payload = $e->getData();
                }

                if (null !== $payload) {
                    $violations->addAll($this->validator->validate($payload, null, $argument->validationGroups ?? null));
                }

                if (\count($violations)) {
                    throw new HttpException($validationFailedCodeimplode("\n", array_map(static fn ($e) => $e->getMessage()iterator_to_array($violations)))new ValidationFailedException($payload$violations));
                }
            } else {
                try {
                    $payload = $this->$payloadMapper($request$type$argument);
                } catch (PartialDenormalizationException $e) {
                    throw new HttpException($validationFailedCodeimplode("\n", array_map(static fn ($e) => $e->getMessage()$e->getErrors()))$e);
                }
            }
Home | Imprint | This part of the site doesn't use cookies.