File tree Expand file tree Collapse file tree 5 files changed +84
-24
lines changed
src/Symfony/Component/Validator Expand file tree Collapse file tree 5 files changed +84
-24
lines changed Original file line number Diff line number Diff line change @@ -26,20 +26,17 @@ class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
26
26
{
27
27
protected $ validators = [];
28
28
29
- public function __construct ()
29
+ public function __construct (array $ validators = [] )
30
30
{
31
+ $ this ->validators = $ validators ;
31
32
}
32
33
33
34
public function getInstance (Constraint $ constraint ): ConstraintValidatorInterface
34
35
{
35
- $ className = $ constraint ->validatedBy ();
36
-
37
- if (!isset ($ this ->validators [$ className ])) {
38
- $ this ->validators [$ className ] = 'validator.expression ' === $ className
39
- ? new ExpressionValidator ()
40
- : new $ className ();
36
+ if ('validator.expression ' === $ name = $ class = $ constraint ->validatedBy ()) {
37
+ $ class = ExpressionValidator::class;
41
38
}
42
39
43
- return $ this ->validators [$ className ] ;
40
+ return $ this ->validators [$ name ] ??= new $ class () ;
44
41
}
45
42
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ /*
4
+ * This file is part of the Symfony package.
5
+ *
6
+ * (c) Fabien Potencier <fabien@symfony.com>
7
+ *
8
+ * For the full copyright and license information, please view the LICENSE
9
+ * file that was distributed with this source code.
10
+ */
11
+
12
+ namespace Symfony \Component \Validator \Tests ;
13
+
14
+ use PHPUnit \Framework \TestCase ;
15
+ use Symfony \Component \Validator \ConstraintValidatorFactory ;
16
+ use Symfony \Component \Validator \Tests \Fixtures \DummyConstraint ;
17
+ use Symfony \Component \Validator \Tests \Fixtures \DummyConstraintValidator ;
18
+
19
+ class ConstraintValidatorFactoryTest extends TestCase
20
+ {
21
+ public function testGetInstance ()
22
+ {
23
+ $ factory = new ConstraintValidatorFactory ();
24
+ $ this ->assertInstanceOf (DummyConstraintValidator::class, $ factory ->getInstance (new DummyConstraint ()));
25
+ }
26
+
27
+ public function testPredefinedGetInstance ()
28
+ {
29
+ $ validator = new DummyConstraintValidator ();
30
+ $ factory = new ConstraintValidatorFactory ([DummyConstraintValidator::class => $ validator ]);
31
+ $ this ->assertSame ($ validator , $ factory ->getInstance (new DummyConstraint ()));
32
+ }
33
+ }
Original file line number Diff line number Diff line change 15
15
use Symfony \Component \DependencyInjection \Container ;
16
16
use Symfony \Component \Validator \Constraint ;
17
17
use Symfony \Component \Validator \Constraints \Blank as BlankConstraint ;
18
- use Symfony \Component \Validator \ConstraintValidator ;
19
18
use Symfony \Component \Validator \ContainerConstraintValidatorFactory ;
20
19
use Symfony \Component \Validator \Exception \ValidatorException ;
20
+ use Symfony \Component \Validator \Tests \Fixtures \DummyConstraint ;
21
+ use Symfony \Component \Validator \Tests \Fixtures \DummyConstraintValidator ;
21
22
22
23
class ContainerConstraintValidatorFactoryTest extends TestCase
23
24
{
@@ -59,18 +60,3 @@ public function testGetInstanceInvalidValidatorClass()
59
60
$ factory ->getInstance ($ constraint );
60
61
}
61
62
}
62
-
63
- class DummyConstraint extends Constraint
64
- {
65
- public function validatedBy (): string
66
- {
67
- return DummyConstraintValidator::class;
68
- }
69
- }
70
-
71
- class DummyConstraintValidator extends ConstraintValidator
72
- {
73
- public function validate ($ value , Constraint $ constraint )
74
- {
75
- }
76
- }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ /*
4
+ * This file is part of the Symfony package.
5
+ *
6
+ * (c) Fabien Potencier <fabien@symfony.com>
7
+ *
8
+ * For the full copyright and license information, please view the LICENSE
9
+ * file that was distributed with this source code.
10
+ */
11
+
12
+ namespace Symfony \Component \Validator \Tests \Fixtures ;
13
+
14
+ use Symfony \Component \Validator \Constraint ;
15
+
16
+ class DummyConstraint extends Constraint
17
+ {
18
+ public function validatedBy (): string
19
+ {
20
+ return DummyConstraintValidator::class;
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ /*
4
+ * This file is part of the Symfony package.
5
+ *
6
+ * (c) Fabien Potencier <fabien@symfony.com>
7
+ *
8
+ * For the full copyright and license information, please view the LICENSE
9
+ * file that was distributed with this source code.
10
+ */
11
+
12
+ namespace Symfony \Component \Validator \Tests \Fixtures ;
13
+
14
+ use Symfony \Component \Validator \Constraint ;
15
+ use Symfony \Component \Validator \ConstraintValidator ;
16
+
17
+ class DummyConstraintValidator extends ConstraintValidator
18
+ {
19
+ public function validate ($ value , Constraint $ constraint )
20
+ {
21
+ }
22
+ }
You can’t perform that action at this time.
0 commit comments