-
Notifications
You must be signed in to change notification settings - Fork 0
/
PimpleConstraintValidatorFactory.php
61 lines (49 loc) · 1.62 KB
/
PimpleConstraintValidatorFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
/**
*
* @author M.Paraiso
*
*/
namespace Mparaiso\Doctrine\ORM;
use Pimple;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
/**
* Uses a service container to create constraint validators.
*
*/
class PimpleConstraintValidatorFactory implements ConstraintValidatorFactoryInterface {
protected $container;
protected $validators;
/**
* Constructor.
*
* @param ContainerInterface $container The service container
* @param array $validators An array of validators
*/
public function __construct(Pimple $container, array $validators = array()) {
$this->container = $container;
$this->validators = $validators;
}
/**
* Returns the validator for the supplied constraint.
*
* @param Constraint $constraint A constraint
*
* @return Symfony\Component\Validator\ConstraintValidator A validator for the supplied constraint
*/
public function getInstance(Constraint $constraint) {
$className = $constraint->validatedBy();
if (!isset($this->validators[$className])) {
$this->validators[$className] = new $className();
} elseif (is_string($this->validators[$className])) {
$this->validators[$className] = $this->container[$this->validators[$className]];
}
return $this->validators[$className];
}
/* GETTER ajouter des validators après l'instanciation */
public function getValidators() {
return $this->validators;
}
}