Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cb62d05

Browse files
committed
[Form] [Validator] Fixed issues mentioned in the PR
1 parent 2185ca8 commit cb62d05

File tree

7 files changed

+51
-31
lines changed

7 files changed

+51
-31
lines changed

src/Symfony/Component/Form/Extension/Csrf/CsrfExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class CsrfExtension extends AbstractExtension
2727
*
2828
* @param CsrfProviderInterface $csrfProvider The CSRF provider
2929
*/
30-
public function __construct(CsrfProviderInterface $csrfProvider = null)
30+
public function __construct(CsrfProviderInterface $csrfProvider)
3131
{
3232
$this->csrfProvider = $csrfProvider;
3333
}

src/Symfony/Component/Form/FormFactoryBuilder.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111

1212
namespace Symfony\Component\Form;
1313

14-
/**
15-
* @author Bernhard Schussek <[email protected]>
16-
*/
17-
1814
/**
1915
* The default implementation of FormFactoryBuilderInterface.
2016
*

src/Symfony/Component/Form/PreloadedExtension.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111

1212
namespace Symfony\Component\Form;
1313

14-
/**
15-
* @author Bernhard Schussek <[email protected]>
16-
*/
1714
use Symfony\Component\Form\Exception\FormException;
1815

1916
/**
@@ -87,7 +84,7 @@ public function getTypeExtensions($name)
8784
*/
8885
public function hasTypeExtensions($name)
8986
{
90-
return isset($this->typeExtensions[$name]) && count($this->typeExtensions[$name]) > 0;
87+
return !empty($this->typeExtensions[$name]);
9188
}
9289

9390
/**

src/Symfony/Component/Validator/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ method results are matched against the constraints.
8585
}
8686

8787
$validator = Validation::createValidatorBuilder()
88-
->setAnnotationMapping(true)
88+
->enableAnnotationMapping()
8989
->getValidator();
9090

9191
$user = new User('John Doe', '[email protected]');
@@ -94,8 +94,9 @@ method results are matched against the constraints.
9494

9595
This example uses the annotation support of Doctrine Common to
9696
map constraints to properties and methods. You can also map constraints
97-
using XML, YAML or plain PHP. Check the documentation for more information
98-
about these drivers.
97+
using XML, YAML or plain PHP, if you dislike annotations or don't want
98+
to include Doctrine. Check the documentation for more information about
99+
these drivers.
99100

100101
Resources
101102
---------

src/Symfony/Component/Validator/Validation.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,11 @@ public static function createValidatorBuilder()
4040
{
4141
return new ValidatorBuilder();
4242
}
43+
44+
/**
45+
* This class cannot be instantiated.
46+
*/
47+
private function __construct()
48+
{
49+
}
4350
}

src/Symfony/Component/Validator/ValidatorBuilder.php

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
use Symfony\Component\Validator\Mapping\Loader\YamlFilesLoader;
2424
use Symfony\Component\Validator\Mapping\Loader\XmlFileLoader;
2525
use Symfony\Component\Validator\Mapping\Loader\XmlFilesLoader;
26+
use Doctrine\Common\Annotations\Reader;
2627
use Doctrine\Common\Annotations\AnnotationReader;
28+
use Doctrine\Common\Annotations\CachedReader;
29+
use Doctrine\Common\Cache\ArrayCache;
2730

2831
/**
2932
* The default implementation of {@link ValidatorBuilderInterface}.
@@ -53,9 +56,9 @@ class ValidatorBuilder implements ValidatorBuilderInterface
5356
private $methodMappings = array();
5457

5558
/**
56-
* @var Boolean
59+
* @var Reader
5760
*/
58-
private $annotationMapping = false;
61+
private $annotationReader = null;
5962

6063
/**
6164
* @var ClassMetadataFactoryInterface
@@ -163,21 +166,37 @@ public function addMethodMappings(array $methodNames)
163166
/**
164167
* {@inheritdoc}
165168
*/
166-
public function setAnnotationMapping($enabled)
169+
public function enableAnnotationMapping(Reader $annotationReader = null)
167170
{
168-
if ($enabled && null !== $this->metadataFactory) {
171+
if (null !== $this->metadataFactory) {
169172
throw new ValidatorException('You cannot enable annotation mapping after setting a custom metadata factory. Configure your metadata factory instead.');
170173
}
171174

172-
$this->annotationMapping = $enabled;
175+
if (null === $annotationReader) {
176+
if (!class_exists('Doctrine\Common\Annotations\AnnotationReader')) {
177+
throw new \RuntimeException('Requested a ValidatorFactory with an AnnotationLoader, but the AnnotationReader was not found. You should add Doctrine Common to your project.');
178+
}
179+
180+
$annotationReader = new CachedReader(new AnnotationReader(), new ArrayCache());
181+
}
182+
183+
$this->annotationReader = $annotationReader;
184+
}
185+
186+
/**
187+
* {@inheritdoc}
188+
*/
189+
public function disableAnnotationMapping()
190+
{
191+
$this->annotationReader = null;
173192
}
174193

175194
/**
176195
* {@inheritdoc}
177196
*/
178197
public function setMetadataFactory(ClassMetadataFactoryInterface $metadataFactory)
179198
{
180-
if (count($this->xmlMappings) > 0 || count($this->yamlMappings) > 0 || count($this->methodMappings) > 0 || $this->annotationMapping) {
199+
if (count($this->xmlMappings) > 0 || count($this->yamlMappings) > 0 || count($this->methodMappings) > 0 || null !== $this->annotationReader) {
181200
throw new ValidatorException('You cannot set a custom metadata factory after adding custom mappings. You should do either of both.');
182201
}
183202

@@ -226,18 +245,12 @@ public function getValidator()
226245
$loaders[] = new YamlFileLoader($this->yamlMappings[0]);
227246
}
228247

229-
if (count($this->methodMappings) > 0) {
230-
foreach ($this->methodMappings as $methodName) {
231-
$loaders[] = new StaticMethodLoader($methodName);
232-
}
248+
foreach ($this->methodMappings as $methodName) {
249+
$loaders[] = new StaticMethodLoader($methodName);
233250
}
234251

235-
if ($this->annotationMapping) {
236-
if (!class_exists('Doctrine\Common\Annotations\AnnotationReader')) {
237-
throw new \RuntimeException('Requested a ValidatorFactory with an AnnotationLoader, but the AnnotationReader was not found. You should add Doctrine Common to your project.');
238-
}
239-
240-
$loaders[] = new AnnotationLoader(new AnnotationReader());
252+
if ($this->annotationReader) {
253+
$loaders[] = new AnnotationLoader($this->annotationReader);
241254
}
242255

243256
$loader = null;

src/Symfony/Component/Validator/ValidatorBuilderInterface.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
1515
use Symfony\Component\Validator\Mapping\Cache\CacheInterface;
16+
use Doctrine\Common\Annotations\Reader;
1617

1718
/**
1819
* A configurable builder for ValidatorInterface objects.
@@ -78,11 +79,16 @@ public function addMethodMapping($methodName);
7879
public function addMethodMappings(array $methodNames);
7980

8081
/**
81-
* Enables or disables annotation based constraint mapping.
82+
* Enables annotation based constraint mapping.
8283
*
83-
* @param Boolean $enabled Whether annotation mapping should be enabled.
84+
* @param Reader $annotationReader The annotation reader to be used.
8485
*/
85-
public function setAnnotationMapping($enabled);
86+
public function enableAnnotationMapping(Reader $annotationReader = null);
87+
88+
/**
89+
* Disables annotation based constraint mapping.
90+
*/
91+
public function disableAnnotationMapping();
8692

8793
/**
8894
* Sets the class metadata factory used by the validator.

0 commit comments

Comments
 (0)