2011-01-19 14:02:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Symfony\Component\Validator;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-19 14:02:00 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2011-05-19 21:46:34 +01:00
|
|
|
use Doctrine\Common\Annotations\AnnotationReader;
|
2011-01-19 14:02:00 +00:00
|
|
|
use Symfony\Component\Validator\Exception\MappingException;
|
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadataFactory;
|
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
|
|
|
|
use Symfony\Component\Validator\Mapping\Loader\XmlFilesLoader;
|
|
|
|
use Symfony\Component\Validator\Mapping\Loader\YamlFilesLoader;
|
|
|
|
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
|
|
|
|
use Symfony\Component\Validator\Mapping\Loader\StaticMethodLoader;
|
|
|
|
use Symfony\Component\Validator\Mapping\Loader\LoaderChain;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates and configures new validator objects
|
|
|
|
*
|
|
|
|
* Usually you will use the static method buildDefault() to initialize a
|
|
|
|
* factory with default configuration. To this method you can pass various
|
|
|
|
* parameters that configure where the validator mapping is found. If you
|
|
|
|
* don't pass a parameter, the mapping will be read from annotations.
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* // read from annotations only
|
|
|
|
* $factory = ValidatorFactory::buildDefault();
|
|
|
|
*
|
|
|
|
* // read from XML and YAML, suppress annotations
|
|
|
|
* $factory = ValidatorFactory::buildDefault(array(
|
|
|
|
* '/path/to/mapping.xml',
|
|
|
|
* '/path/to/other/mapping.yml',
|
|
|
|
* ), false);
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* You then have to call getValidator() to create new validators.
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* $validator = $factory->getValidator();
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* When manually constructing a factory, the default configuration of the
|
|
|
|
* validators can be passed to the constructor as a ValidatorContextInterface
|
|
|
|
* object.
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* $defaultContext = new ValidatorContext();
|
2011-03-30 22:10:30 +01:00
|
|
|
* $defaultContext->setClassMetadataFactory($metadataFactory);
|
|
|
|
* $defaultContext->setConstraintValidatorFactory($validatorFactory);
|
2011-01-19 14:02:00 +00:00
|
|
|
* $factory = new ValidatorFactory($defaultContext);
|
|
|
|
*
|
|
|
|
* $form = $factory->getValidator();
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* You can also override the default configuration by calling any of the
|
|
|
|
* methods in this class. These methods return a ValidatorContextInterface object
|
|
|
|
* on which you can override further settings or call getValidator() to create
|
|
|
|
* a form.
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* $form = $factory
|
2011-03-30 22:10:30 +01:00
|
|
|
* ->setClassMetadataFactory($customFactory);
|
2011-01-19 14:02:00 +00:00
|
|
|
* ->getValidator();
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* ValidatorFactory instances should be cached and reused in your application.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Bernhard Schussek <bernhard.schussek@symfony.com>
|
2011-01-19 14:02:00 +00:00
|
|
|
*/
|
|
|
|
class ValidatorFactory implements ValidatorContextInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Holds the context with the default configuration
|
|
|
|
* @var ValidatorContextInterface
|
|
|
|
*/
|
|
|
|
protected $defaultContext;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2011-04-27 06:25:26 +01:00
|
|
|
* @param Boolean $annotations Whether to use annotations for
|
2011-01-19 14:02:00 +00:00
|
|
|
* retrieving mapping information
|
|
|
|
* @param string $staticMethod The name of the static method to
|
|
|
|
* use, if static method loading should
|
|
|
|
* be enabled
|
2011-05-19 08:34:54 +01:00
|
|
|
* @throws MappingException If any of the files in $mappingFiles
|
2011-01-19 14:02:00 +00:00
|
|
|
* has neither the extension ".xml" nor
|
|
|
|
* ".yml" nor ".yaml"
|
|
|
|
*/
|
2011-06-14 11:16:59 +01:00
|
|
|
static public function buildDefault(array $mappingFiles = array(), $annotations = true, $staticMethod = null)
|
2011-01-19 14:02:00 +00:00
|
|
|
{
|
|
|
|
$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) {
|
2011-05-19 21:46:34 +01:00
|
|
|
$loaders[] = new AnnotationLoader(new AnnotationReader());
|
2011-01-19 14:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2011-03-30 22:10:30 +01:00
|
|
|
$context->setClassMetadataFactory(new ClassMetadataFactory($loader));
|
|
|
|
$context->setConstraintValidatorFactory(new ConstraintValidatorFactory());
|
2011-01-19 14:02:00 +00:00
|
|
|
|
|
|
|
return new static($context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the given context as default context
|
|
|
|
*
|
|
|
|
* @param ValidatorContextInterface $defaultContext A preconfigured context
|
|
|
|
*/
|
|
|
|
public function __construct(ValidatorContextInterface $defaultContext = null)
|
|
|
|
{
|
|
|
|
$this->defaultContext = null === $defaultContext ? new ValidatorContext() : $defaultContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overrides the class metadata factory of the default context and returns
|
|
|
|
* the new context
|
|
|
|
*
|
|
|
|
* @param ClassMetadataFactoryInterface $metadataFactory The new factory instance
|
|
|
|
* @return ValidatorContextInterface The preconfigured form context
|
|
|
|
*/
|
2011-03-30 22:10:30 +01:00
|
|
|
public function setClassMetadataFactory(ClassMetadataFactoryInterface $metadataFactory)
|
2011-01-19 14:02:00 +00:00
|
|
|
{
|
|
|
|
$context = clone $this->defaultContext;
|
|
|
|
|
2011-03-30 22:10:30 +01:00
|
|
|
return $context->setClassMetadataFactory($metadataFactory);
|
2011-01-19 14:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overrides the constraint validator factory of the default context and
|
|
|
|
* returns the new context
|
|
|
|
*
|
|
|
|
* @param ClassMetadataFactoryInterface $validatorFactory The new factory instance
|
|
|
|
* @return ValidatorContextInterface The preconfigured form context
|
|
|
|
*/
|
2011-03-30 22:10:30 +01:00
|
|
|
public function setConstraintValidatorFactory(ConstraintValidatorFactoryInterface $validatorFactory)
|
2011-01-19 14:02:00 +00:00
|
|
|
{
|
|
|
|
$context = clone $this->defaultContext;
|
|
|
|
|
2011-03-30 22:10:30 +01:00
|
|
|
return $context->setConstraintValidatorFactory($validatorFactory);
|
2011-01-19 14:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new validator with the settings stored in the default context
|
|
|
|
*
|
|
|
|
* @return ValidatorInterface The new validator
|
|
|
|
*/
|
|
|
|
public function getValidator()
|
|
|
|
{
|
|
|
|
return $this->defaultContext->getValidator();
|
|
|
|
}
|
2011-06-08 11:16:48 +01:00
|
|
|
}
|