Skip to content

Commit

Permalink
[Form] [Validator] Fixed issues mentioned in the PR
Browse files Browse the repository at this point in the history
  • Loading branch information
webmozart committed Jul 30, 2012
1 parent 2185ca8 commit cb62d05
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 31 deletions.
Expand Up @@ -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;
}
Expand Down
4 changes: 0 additions & 4 deletions src/Symfony/Component/Form/FormFactoryBuilder.php
Expand Up @@ -11,10 +11,6 @@

namespace Symfony\Component\Form;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/

/**
* The default implementation of FormFactoryBuilderInterface.
*
Expand Down
5 changes: 1 addition & 4 deletions src/Symfony/Component/Form/PreloadedExtension.php
Expand Up @@ -11,9 +11,6 @@

namespace Symfony\Component\Form;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
use Symfony\Component\Form\Exception\FormException;

/**
Expand Down Expand Up @@ -87,7 +84,7 @@ public function getTypeExtensions($name)
*/
public function hasTypeExtensions($name)
{
return isset($this->typeExtensions[$name]) && count($this->typeExtensions[$name]) > 0;
return !empty($this->typeExtensions[$name]);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Symfony/Component/Validator/README.md
Expand Up @@ -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');
Expand All @@ -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
---------
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/Validator/Validation.php
Expand Up @@ -40,4 +40,11 @@ public static function createValidatorBuilder()
{
return new ValidatorBuilder();
}

/**
* This class cannot be instantiated.
*/
private function __construct()
{
}
}
45 changes: 29 additions & 16 deletions src/Symfony/Component/Validator/ValidatorBuilder.php
Expand Up @@ -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}.
Expand Down Expand Up @@ -53,9 +56,9 @@ class ValidatorBuilder implements ValidatorBuilderInterface
private $methodMappings = array();

/**
* @var Boolean
* @var Reader
*/
private $annotationMapping = false;
private $annotationReader = null;

/**
* @var ClassMetadataFactoryInterface
Expand Down Expand Up @@ -163,21 +166,37 @@ 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;
}

/**
* {@inheritdoc}
*/
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.');
}

Expand Down Expand Up @@ -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;
Expand Down
12 changes: 9 additions & 3 deletions src/Symfony/Component/Validator/ValidatorBuilderInterface.php
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit cb62d05

Please sign in to comment.