public static function create(IContainer $context) { $loaders = array(); $context = new ValidatorContext(); AnnotationRegistry::registerAutoloadNamespaces(array('\\Doctrine\\ORM\\Mapping\\', '\\Symfony\\Component\\Validator\\Constraints\\')); $annotationReader = new AnnotationReader(); $annotationReader->setIgnoreNotImportedAnnotations(true); $loader = new AnnotationLoader($annotationReader); $context->setClassMetadataFactory(new ClassMetadataFactory($loader)); $context->setConstraintValidatorFactory(new ConstraintValidatorFactory()); $factory = new SymfonyValidatorFactory($context); return $factory->getValidator(); }
/** * Builds a validator factory with the default mapping loaders * * @param array $mappingFiles A list of XML or YAML file names * where mapping information can be * found. Can be empty. * @param Boolean $annotations Whether to use annotations for * retrieving mapping information * @param string $staticMethod The name of the static method to * use, if static method loading should * be enabled * * @throws MappingException If any of the files in $mappingFiles * has neither the extension ".xml" nor * ".yml" nor ".yaml" */ public static function buildDefault(array $mappingFiles = array(), $annotations = true, $staticMethod = null) { $xmlMappingFiles = array(); $yamlMappingFiles = array(); $loaders = array(); $context = new ValidatorContext(); foreach ($mappingFiles as $file) { $extension = pathinfo($file, PATHINFO_EXTENSION); if ($extension === 'xml') { $xmlMappingFiles[] = $file; } elseif ($extension === 'yaml' || $extension === 'yml') { $yamlMappingFiles[] = $file; } else { throw new MappingException('The only supported mapping file formats are XML and YAML'); } } if (count($xmlMappingFiles) > 0) { $loaders[] = new XmlFilesLoader($xmlMappingFiles); } if (count($yamlMappingFiles) > 0) { $loaders[] = new YamlFilesLoader($yamlMappingFiles); } if ($annotations) { $loaders[] = new AnnotationLoader(new AnnotationReader()); } if ($staticMethod) { $loaders[] = new StaticMethodLoader($staticMethod); } if (count($loaders) > 1) { $loader = new LoaderChain($loaders); } elseif (count($loaders) === 1) { $loader = $loaders[0]; } else { throw new MappingException('No mapping loader was found for the given parameters'); } $context->setClassMetadataFactory(new ClassMetadataFactory($loader)); $context->setConstraintValidatorFactory(new ConstraintValidatorFactory()); return new static($context); }
/** * Builds a validator factory with the default mapping loaders * * @param array $mappingFiles A list of XML or YAML file names * where mapping information can be * found. Can be empty. * @param Boolean $annotations Whether to use annotations for * retrieving mapping information * @param string $staticMethod The name of the static method to * use, if static method loading should * be enabled * * @throws MappingException If any of the files in $mappingFiles * has neither the extension ".xml" nor * ".yml" nor ".yaml" */ public static function buildDefault(array $mappingFiles = array(), $annotations = false, $staticMethod = null) { $xmlMappingFiles = array(); $yamlMappingFiles = array(); $loaders = array(); $context = new ValidatorContext(); foreach ($mappingFiles as $file) { $extension = pathinfo($file, PATHINFO_EXTENSION); if ($extension === 'xml') { $xmlMappingFiles[] = $file; } elseif ($extension === 'yaml' || $extension === 'yml') { $yamlMappingFiles[] = $file; } else { throw new MappingException('The only supported mapping file formats are XML and YAML'); } } if (count($xmlMappingFiles) > 0) { $loaders[] = new XmlFilesLoader($xmlMappingFiles); } if (count($yamlMappingFiles) > 0) { $loaders[] = new YamlFilesLoader($yamlMappingFiles); } if ($annotations) { if (!class_exists('Doctrine\\Common\\Annotations\\AnnotationReader')) { throw new \RuntimeException('Requested a ValidatorFactory with an AnnotationLoader, but the AnnotationReader was not found. You should add Doctrine Common to your project.'); } $loaders[] = new AnnotationLoader(new AnnotationReader()); } if ($staticMethod) { $loaders[] = new StaticMethodLoader($staticMethod); } if (count($loaders) > 1) { $loader = new LoaderChain($loaders); } elseif (count($loaders) === 1) { $loader = $loaders[0]; } else { throw new MappingException('No mapping loader was found for the given parameters'); } $context->setClassMetadataFactory(new ClassMetadataFactory($loader)); $context->setConstraintValidatorFactory(new ConstraintValidatorFactory()); return new static($context); }
public function testBuildDefaultFromMultipleLoaders() { if (!class_exists('Doctrine\\Common\\Annotations\\AnnotationReader')) { $this->markTestSkipped('Doctrine is required for this test'); } $xmlPath = __DIR__ . '/Mapping/Loader/constraint-mapping.xml'; $yamlPath = __DIR__ . '/Mapping/Loader/constraint-mapping.yml'; $factory = ValidatorFactory::buildDefault(array($xmlPath, $yamlPath), true, null, 'loadMetadata'); $chain = new LoaderChain(array(new XmlFilesLoader(array($xmlPath)), new YamlFilesLoader(array($yamlPath)), new AnnotationLoader(), new StaticMethodLoader('loadMetadata'))); $context = new ValidatorContext(); $context->classMetadataFactory(new ClassMetadataFactory($chain))->constraintValidatorFactory(new ConstraintValidatorFactory()); $this->assertEquals(new ValidatorFactory($context), $factory); }