diff --git a/src/Symfony/Component/Form/Extension/Csrf/CsrfExtension.php b/src/Symfony/Component/Form/Extension/Csrf/CsrfExtension.php index fabc492f02..71d06381ad 100644 --- a/src/Symfony/Component/Form/Extension/Csrf/CsrfExtension.php +++ b/src/Symfony/Component/Form/Extension/Csrf/CsrfExtension.php @@ -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; } diff --git a/src/Symfony/Component/Form/FormFactoryBuilder.php b/src/Symfony/Component/Form/FormFactoryBuilder.php index d316c9c87b..65f697902c 100644 --- a/src/Symfony/Component/Form/FormFactoryBuilder.php +++ b/src/Symfony/Component/Form/FormFactoryBuilder.php @@ -11,10 +11,6 @@ namespace Symfony\Component\Form; -/** - * @author Bernhard Schussek - */ - /** * The default implementation of FormFactoryBuilderInterface. * diff --git a/src/Symfony/Component/Form/PreloadedExtension.php b/src/Symfony/Component/Form/PreloadedExtension.php index 73b502435d..8a4eb5f319 100644 --- a/src/Symfony/Component/Form/PreloadedExtension.php +++ b/src/Symfony/Component/Form/PreloadedExtension.php @@ -11,9 +11,6 @@ namespace Symfony\Component\Form; -/** - * @author Bernhard Schussek - */ 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]); } /** diff --git a/src/Symfony/Component/Validator/README.md b/src/Symfony/Component/Validator/README.md index 2c82593337..2901b7cdd9 100644 --- a/src/Symfony/Component/Validator/README.md +++ b/src/Symfony/Component/Validator/README.md @@ -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 --------- diff --git a/src/Symfony/Component/Validator/Validation.php b/src/Symfony/Component/Validator/Validation.php index 784edbc151..de77e838fb 100644 --- a/src/Symfony/Component/Validator/Validation.php +++ b/src/Symfony/Component/Validator/Validation.php @@ -40,4 +40,11 @@ final class Validation { return new ValidatorBuilder(); } + + /** + * This class cannot be instantiated. + */ + private function __construct() + { + } } diff --git a/src/Symfony/Component/Validator/ValidatorBuilder.php b/src/Symfony/Component/Validator/ValidatorBuilder.php index ba06c2c0a2..7819cac5c2 100644 --- a/src/Symfony/Component/Validator/ValidatorBuilder.php +++ b/src/Symfony/Component/Validator/ValidatorBuilder.php @@ -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; diff --git a/src/Symfony/Component/Validator/ValidatorBuilderInterface.php b/src/Symfony/Component/Validator/ValidatorBuilderInterface.php index 812d440f2d..42e1fe1272 100644 --- a/src/Symfony/Component/Validator/ValidatorBuilderInterface.php +++ b/src/Symfony/Component/Validator/ValidatorBuilderInterface.php @@ -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.