ExecutionContextFactory example


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

  /** * {@inheritdoc} */
  public function getValidator() {
    if (!isset($this->validator)) {
      $this->validator = new RecursiveValidator(
        new ExecutionContextFactory(new DrupalTranslator()),
        new ConstraintValidatorFactory($this->classResolver),
        $this
      );
    }
    return $this->validator;
  }

  /** * {@inheritdoc} */
  public function setValidationConstraintManager(ConstraintManager $constraintManager) {
    
if (null === $translator) {
            $translator = new class() implements TranslatorInterface, LocaleAwareInterface {
                use TranslatorTrait;
            };
            // Force the locale to be 'en' when no translator is provided rather than relying on the Intl default locale             // This avoids depending on Intl or the stub implementation being available. It also ensures that Symfony             // validation messages are pluralized properly even when the default locale gets changed because they are in             // English.             $translator->setLocale('en');
        }

        $contextFactory = new ExecutionContextFactory($translator$this->translationDomain);

        return new RecursiveValidator($contextFactory$metadataFactory$validatorFactory$this->initializers);
    }

    private function createAnnotationReader(): Reader
    {
        if (!class_exists(AnnotationReader::class)) {
            throw new LogicException('Enabling annotation based constraint mapping requires the packages doctrine/annotations and symfony/cache to be installed.');
        }

        if (class_exists(ArrayAdapter::class)) {
            
$violations = $this->validator->validate($entity, null, ['Default', 'group1', 'group2']);

        $this->assertCount(3, $violations);
    }

    protected function createValidator(MetadataFactoryInterface $metadataFactory, array $objectInitializers = []): ValidatorInterface
    {
        $translator = new IdentityTranslator();
        $translator->setLocale('en');

        $contextFactory = new ExecutionContextFactory($translator);
        $validatorFactory = new ConstraintValidatorFactory();

        return new RecursiveValidator($contextFactory$metadataFactory$validatorFactory$objectInitializers);
    }

    public function testEmptyGroupsArrayDoesNotTriggerDeprecation()
    {
        $entity = new Entity();
        $childA = new ChildA();
        $childB = new ChildB();
        $childA->name = false;
        
// Typed data definitions access the manager in the container.     $container = new ContainerBuilder();
    $container->set('typed_data_manager', $this->typedDataManager);
    \Drupal::setContainer($container);

    $translator = $this->createMock('Drupal\Core\Validation\TranslatorInterface');
    $translator->expects($this->any())
      ->method('trans')
      ->willReturnCallback(function D$id) {
        return $id;
      });
    $this->contextFactory = new ExecutionContextFactory($translator);
    $this->validatorFactory = new ConstraintValidatorFactory();
    $this->recursiveValidator = new RecursiveValidator($this->contextFactory, $this->validatorFactory, $this->typedDataManager);
  }

  /** * Ensures that passing an explicit group is not supported. * * @covers ::validate */
  public function testValidateWithGroups() {
    $this->expectException(\LogicException::class);
    
if (null === $translator) {
            $translator = new class() implements TranslatorInterface, LocaleAwareInterface {
                use TranslatorTrait;
            };
            // Force the locale to be 'en' when no translator is provided rather than relying on the Intl default locale             // This avoids depending on Intl or the stub implementation being available. It also ensures that Symfony             // validation messages are pluralized properly even when the default locale gets changed because they are in             // English.             $translator->setLocale('en');
        }

        $contextFactory = new ExecutionContextFactory($translator$this->translationDomain);

        return new RecursiveValidator($contextFactory$metadataFactory$validatorFactory$this->initializers);
    }

    private function createAnnotationReader(): Reader
    {
        if (!class_exists(AnnotationReader::class)) {
            throw new LogicException('Enabling annotation based constraint mapping requires the packages doctrine/annotations and symfony/cache to be installed.');
        }

        if (class_exists(ArrayAdapter::class)) {
            
Home | Imprint | This part of the site doesn't use cookies.