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); }
/** * 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 array $annotationNamespaces The annotation namespaces used * for finding the annotation classes. * The namespace "validation" is used * by default * @param string $staticMethod The name of the static method to * use, if static method loading should * be enabled * @throws \InvalidArgumentException 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, $annotationNamespaces = null, $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; } else { if ($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($annotationNamespaces); } if ($staticMethod) { $loaders[] = new StaticMethodLoader($staticMethod); } if (count($loaders) > 1) { $loader = new LoaderChain($loaders); } else { if (count($loaders) === 1) { $loader = $loaders[0]; } else { throw new MappingException('No mapping loader was found for the given parameters'); } } $context->classMetadataFactory(new ClassMetadataFactory($loader)); $context->constraintValidatorFactory(new ConstraintValidatorFactory()); return new static($context); }