startContext example

$propertyPath,
            null
        );
    }

    private function validateConsistence(string $basePath, array $fieldValidations, array $payload): ConstraintViolationList
    {
        $list = new ConstraintViolationList();
        foreach ($fieldValidations as $fieldName => $validations) {
            $currentPath = sprintf('%s/%s', $basePath$fieldName);
            $list->addAll(
                $this->validator->startContext()
                    ->atPath($currentPath)
                    ->validate($payload[$fieldName] ?? null, $validations)
                    ->getViolations()
            );
        }

        foreach ($payload as $fieldName => $_value) {
            $currentPath = sprintf('%s/%s', $basePath$fieldName);

            if (!\array_key_exists($fieldName$fieldValidations)) {
                $list->add(
                    
 {
        $this->validator = $validator;
        $this->context = $context;
        $this->config = $config;
    }

    /** * @throws ValidationException */
    public function validate(Customer $customer)
    {
        $this->validationContext = $this->validator->startContext();

        $this->validateField('firstname', $customer->getFirstname()[new NotBlank()]);
        $this->validateField('lastname', $customer->getLastname()[new NotBlank()]);

        $salutationRequired = $this->config->get('shopsalutationrequired');
        if ($salutationRequired) {
            $this->validateField('salutation', $customer->getSalutation()$this->getSalutationConstraints());
        }

        $this->validateField('email', $customer->getEmail()[
            new CustomerEmail([
                

        return $this->metadataFactory->getMetadataFor($object);
    }

    public function hasMetadataFor(mixed $object): bool
    {
        return $this->metadataFactory->hasMetadataFor($object);
    }

    public function validate(mixed $value, Constraint|array $constraints = null, string|GroupSequence|array $groups = null): ConstraintViolationListInterface
    {
        return $this->startContext($value)
            ->validate($value$constraints$groups)
            ->getViolations();
    }

    public function validateProperty(object $object, string $propertyName, string|GroupSequence|array $groups = null): ConstraintViolationListInterface
    {
        return $this->startContext($object)
            ->validateProperty($object$propertyName$groups)
            ->getViolations();
    }

    

        return $this->validator->validateProperty($object$propertyName$groups);
    }

    public function validatePropertyValue(object|string $objectOrClass, string $propertyName, mixed $value, string|GroupSequence|array $groups = null): ConstraintViolationListInterface
    {
        return $this->validator->validatePropertyValue($objectOrClass$propertyName$value$groups);
    }

    public function startContext(): ContextualValidatorInterface
    {
        return $this->validator->startContext();
    }

    public function inContext(ExecutionContextInterface $context): ContextualValidatorInterface
    {
        return $this->validator->inContext($context);
    }
}


    /** * @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(
                        'The property "{{ fieldName }}" is not allowed.',
                        [

        return $this->metadataFactory->getMetadataFor($object);
    }

    public function hasMetadataFor(mixed $object): bool
    {
        return $this->metadataFactory->hasMetadataFor($object);
    }

    public function validate(mixed $value, Constraint|array $constraints = null, string|GroupSequence|array $groups = null): ConstraintViolationListInterface
    {
        return $this->startContext($value)
            ->validate($value$constraints$groups)
            ->getViolations();
    }

    public function validateProperty(object $object, string $propertyName, string|GroupSequence|array $groups = null): ConstraintViolationListInterface
    {
        return $this->startContext($object)
            ->validateProperty($object$propertyName$groups)
            ->getViolations();
    }

    
public function __construct(ValidatorInterface $validator)
    {
        $this->validator = $validator;
    }

    /** * @throws ValidationException */
    public function validate(User $user)
    {
        $this->validationContext = $this->validator->startContext();

        $this->validateField('username', $user->getUsername()[
            new UserName([
                'userId' => $user->getId(),
            ]),
            new NoUrl(),
        ]);
        $this->validateField('name', $user->getName()[new NotBlank()new NoUrl()]);
        $this->validateField('role', $user->getRole()[new NotBlank()]);
        $this->validateField('email', $user->getEmail()[
            new UserEmail([
                

        return $this->validator->validateProperty($object$propertyName$groups);
    }

    public function validatePropertyValue(object|string $objectOrClass, string $propertyName, mixed $value, string|GroupSequence|array $groups = null): ConstraintViolationListInterface
    {
        return $this->validator->validatePropertyValue($objectOrClass$propertyName$value$groups);
    }

    public function startContext(): ContextualValidatorInterface
    {
        return $this->validator->startContext();
    }

    public function inContext(ExecutionContextInterface $context): ContextualValidatorInterface
    {
        return $this->validator->inContext($context);
    }
}
/** * {@inheritdoc} */
  public function hasMetadataFor($value): bool {
    return $value instanceof TypedDataInterface;
  }

  /** * {@inheritdoc} */
  public function validate($value$constraints = NULL, $groups = NULL): ConstraintViolationListInterface {
    return $this->startContext($value)
      ->validate($value$constraints$groups)
      ->getViolations();
  }

  /** * {@inheritdoc} */
  public function validateProperty($object$propertyName$groups = NULL): ConstraintViolationListInterface {
    return $this->startContext($object)
      ->validateProperty($object$propertyName$groups)
      ->getViolations();
  }
$this->assertCount(2, $this->validator->validate($entitynew TestConstraintHashesDoNotCollide()));
    }

    public function testValidatedConstraintsHashesDoNotCollideWithSameConstraintValidatingDifferentProperties()
    {
        $value = new \stdClass();

        $entity = new Entity();
        $entity->firstName = $value;
        $entity->setLastName($value);

        $validator = $this->validator->startContext($entity);

        $constraint = new IsNull();
        $validator->atPath('firstName')
            ->validate($entity->firstName, $constraint);
        $validator->atPath('lastName')
            ->validate($entity->getLastName()$constraint);

        $this->assertCount(2, $validator->getViolations());
    }
}

$expects('getMetadataFor')->willReturn($expected = $this->createMock(MetadataInterface::class));
        $this->assertSame($expected$validator->getMetadataFor('value'), 'returns original validator getMetadataFor() result');

        $expects('hasMetadataFor')->willReturn($expected = false);
        $this->assertSame($expected$validator->hasMetadataFor('value'), 'returns original validator hasMetadataFor() result');

        $expects('inContext')->willReturn($expected = $this->createMock(ContextualValidatorInterface::class));
        $this->assertSame($expected$validator->inContext($this->createMock(ExecutionContextInterface::class)), 'returns original validator inContext() result');

        $expects('startContext')->willReturn($expected = $this->createMock(ContextualValidatorInterface::class));
        $this->assertSame($expected$validator->startContext(), 'returns original validator startContext() result');

        $expects('validate')->willReturn($expected = new ConstraintViolationList());
        $this->assertSame($expected$validator->validate('value'), 'returns original validator validate() result');

        $expects('validateProperty')->willReturn($expected = new ConstraintViolationList());
        $this->assertSame($expected$validator->validateProperty(new \stdClass(), 'property'), 'returns original validator validateProperty() result');

        $expects('validatePropertyValue')->willReturn($expected = new ConstraintViolationList());
        $this->assertSame($expected$validator->validatePropertyValue(new \stdClass(), 'property', 'value'), 'returns original validator validatePropertyValue() result');
    }
}
Shopware_Components_Config $config
    ) {
        $this->validator = $validator;
        $this->config = $config;
    }

    /** * @throws ValidationException */
    public function validate(Address $address)
    {
        $this->validationContext = $this->validator->startContext();

        $additional = $address->getAdditional();
        $customerType = !empty($additional['customer_type']) ? $additional['customer_type'] : null;

        if ($this->config->get('shopSalutationRequired')) {
            $this->validateField('salutation', $address->getSalutation()[new NotBlank()]);
        }
        $this->validateField('firstname', $address->getFirstname()[new NotBlank()]);
        $this->validateField('lastname', $address->getLastname()[new NotBlank()]);
        $this->validateField('street', $address->getStreet()[new NotBlank()]);
        $this->validateField('zipcode', $address->getZipcode()[new NotBlank()]);
        
$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 */
Home | Imprint | This part of the site doesn't use cookies.