You are here

public function ValidatorBuilder::getValidator in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/validator/ValidatorBuilder.php \Symfony\Component\Validator\ValidatorBuilder::getValidator()

Builds and returns a new validator object.

Return value

ValidatorInterface The built validator.

Overrides ValidatorBuilderInterface::getValidator

File

vendor/symfony/validator/ValidatorBuilder.php, line 333

Class

ValidatorBuilder
The default implementation of {@link ValidatorBuilderInterface}.

Namespace

Symfony\Component\Validator

Code

public function getValidator() {
  $metadataFactory = $this->metadataFactory;
  if (!$metadataFactory) {
    $loaders = array();
    if (count($this->xmlMappings) > 1) {
      $loaders[] = new XmlFilesLoader($this->xmlMappings);
    }
    elseif (1 === count($this->xmlMappings)) {
      $loaders[] = new XmlFileLoader($this->xmlMappings[0]);
    }
    if (count($this->yamlMappings) > 1) {
      $loaders[] = new YamlFilesLoader($this->yamlMappings);
    }
    elseif (1 === count($this->yamlMappings)) {
      $loaders[] = new YamlFileLoader($this->yamlMappings[0]);
    }
    foreach ($this->methodMappings as $methodName) {
      $loaders[] = new StaticMethodLoader($methodName);
    }
    if ($this->annotationReader) {
      $loaders[] = new AnnotationLoader($this->annotationReader);
    }
    $loader = null;
    if (count($loaders) > 1) {
      $loader = new LoaderChain($loaders);
    }
    elseif (1 === count($loaders)) {
      $loader = $loaders[0];
    }
    $metadataFactory = new LazyLoadingMetadataFactory($loader, $this->metadataCache);
  }
  $validatorFactory = $this->validatorFactory ?: new ConstraintValidatorFactory($this->propertyAccessor);
  $translator = $this->translator;
  if (null === $translator) {
    $translator = new IdentityTranslator();

    // 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);
}