[Form] [Validator] Fixed issues mentioned in the PR

This commit is contained in:
Bernhard Schussek 2012-07-30 12:49:07 +02:00
parent 2185ca80e2
commit cb62d05f8d
7 changed files with 51 additions and 31 deletions

View File

@ -27,7 +27,7 @@ class CsrfExtension extends AbstractExtension
*
* @param CsrfProviderInterface $csrfProvider The CSRF provider
*/
public function __construct(CsrfProviderInterface $csrfProvider = null)
public function __construct(CsrfProviderInterface $csrfProvider)
{
$this->csrfProvider = $csrfProvider;
}

View File

@ -11,10 +11,6 @@
namespace Symfony\Component\Form;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
/**
* The default implementation of FormFactoryBuilderInterface.
*

View File

@ -11,9 +11,6 @@
namespace Symfony\Component\Form;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
use Symfony\Component\Form\Exception\FormException;
/**
@ -87,7 +84,7 @@ class PreloadedExtension implements FormExtensionInterface
*/
public function hasTypeExtensions($name)
{
return isset($this->typeExtensions[$name]) && count($this->typeExtensions[$name]) > 0;
return !empty($this->typeExtensions[$name]);
}
/**

View File

@ -85,7 +85,7 @@ method results are matched against the constraints.
}
$validator = Validation::createValidatorBuilder()
->setAnnotationMapping(true)
->enableAnnotationMapping()
->getValidator();
$user = new User('John Doe', 'john@example.com');
@ -94,8 +94,9 @@ method results are matched against the constraints.
This example uses the annotation support of Doctrine Common to
map constraints to properties and methods. You can also map constraints
using XML, YAML or plain PHP. Check the documentation for more information
about these drivers.
using XML, YAML or plain PHP, if you dislike annotations or don't want
to include Doctrine. Check the documentation for more information about
these drivers.
Resources
---------

View File

@ -40,4 +40,11 @@ final class Validation
{
return new ValidatorBuilder();
}
/**
* This class cannot be instantiated.
*/
private function __construct()
{
}
}

View File

@ -23,7 +23,10 @@ use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Validator\Mapping\Loader\YamlFilesLoader;
use Symfony\Component\Validator\Mapping\Loader\XmlFileLoader;
use Symfony\Component\Validator\Mapping\Loader\XmlFilesLoader;
use Doctrine\Common\Annotations\Reader;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Cache\ArrayCache;
/**
* The default implementation of {@link ValidatorBuilderInterface}.
@ -53,9 +56,9 @@ class ValidatorBuilder implements ValidatorBuilderInterface
private $methodMappings = array();
/**
* @var Boolean
* @var Reader
*/
private $annotationMapping = false;
private $annotationReader = null;
/**
* @var ClassMetadataFactoryInterface
@ -163,13 +166,29 @@ class ValidatorBuilder implements ValidatorBuilderInterface
/**
* {@inheritdoc}
*/
public function setAnnotationMapping($enabled)
public function enableAnnotationMapping(Reader $annotationReader = null)
{
if ($enabled && null !== $this->metadataFactory) {
if (null !== $this->metadataFactory) {
throw new ValidatorException('You cannot enable annotation mapping after setting a custom metadata factory. Configure your metadata factory instead.');
}
$this->annotationMapping = $enabled;
if (null === $annotationReader) {
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.');
}
$annotationReader = new CachedReader(new AnnotationReader(), new ArrayCache());
}
$this->annotationReader = $annotationReader;
}
/**
* {@inheritdoc}
*/
public function disableAnnotationMapping()
{
$this->annotationReader = null;
}
/**
@ -177,7 +196,7 @@ class ValidatorBuilder implements ValidatorBuilderInterface
*/
public function setMetadataFactory(ClassMetadataFactoryInterface $metadataFactory)
{
if (count($this->xmlMappings) > 0 || count($this->yamlMappings) > 0 || count($this->methodMappings) > 0 || $this->annotationMapping) {
if (count($this->xmlMappings) > 0 || count($this->yamlMappings) > 0 || count($this->methodMappings) > 0 || null !== $this->annotationReader) {
throw new ValidatorException('You cannot set a custom metadata factory after adding custom mappings. You should do either of both.');
}
@ -226,18 +245,12 @@ class ValidatorBuilder implements ValidatorBuilderInterface
$loaders[] = new YamlFileLoader($this->yamlMappings[0]);
}
if (count($this->methodMappings) > 0) {
foreach ($this->methodMappings as $methodName) {
$loaders[] = new StaticMethodLoader($methodName);
}
foreach ($this->methodMappings as $methodName) {
$loaders[] = new StaticMethodLoader($methodName);
}
if ($this->annotationMapping) {
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 ($this->annotationReader) {
$loaders[] = new AnnotationLoader($this->annotationReader);
}
$loader = null;

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Validator;
use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
use Symfony\Component\Validator\Mapping\Cache\CacheInterface;
use Doctrine\Common\Annotations\Reader;
/**
* A configurable builder for ValidatorInterface objects.
@ -78,11 +79,16 @@ interface ValidatorBuilderInterface
public function addMethodMappings(array $methodNames);
/**
* Enables or disables annotation based constraint mapping.
* Enables annotation based constraint mapping.
*
* @param Boolean $enabled Whether annotation mapping should be enabled.
* @param Reader $annotationReader The annotation reader to be used.
*/
public function setAnnotationMapping($enabled);
public function enableAnnotationMapping(Reader $annotationReader = null);
/**
* Disables annotation based constraint mapping.
*/
public function disableAnnotationMapping();
/**
* Sets the class metadata factory used by the validator.