Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

  • Loading branch information...
commit 9b3f340f99aa920607ef2a7e56a8cbcc2df05aaf 1 parent 19695e9
@webmozart webmozart authored
View
7 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
---------
View
7 Validation.php
@@ -40,4 +40,11 @@ public static function createValidatorBuilder()
{
return new ValidatorBuilder();
}
+
+ /**
+ * This class cannot be instantiated.
+ */
+ private function __construct()
+ {
+ }
}
View
45 ValidatorBuilder.php
@@ -23,7 +23,10 @@
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 @@ public function addMethodMappings(array $methodNames)
/**
* {@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 @@ public function setAnnotationMapping($enabled)
*/
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 @@ public function getValidator()
$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
12 ValidatorBuilderInterface.php
@@ -13,6 +13,7 @@
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 @@ public function addMethodMapping($methodName);
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.
Please sign in to comment.
Something went wrong with that request. Please try again.