Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/validator/ContainerConstraintValidatorFactory.php @ 14:1fec387a4317
Update Drupal core to 8.5.2 via Composer
author | Chris Cannam |
---|---|
date | Mon, 23 Apr 2018 09:46:53 +0100 |
parents | |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
13:5fb285c0d0e3 | 14:1fec387a4317 |
---|---|
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; | |
13 | |
14 use Psr\Container\ContainerInterface; | |
15 use Symfony\Component\Validator\Exception\UnexpectedTypeException; | |
16 use Symfony\Component\Validator\Exception\ValidatorException; | |
17 | |
18 /** | |
19 * Uses a service container to create constraint validators. | |
20 * | |
21 * @author Kris Wallsmith <kris@symfony.com> | |
22 */ | |
23 class ContainerConstraintValidatorFactory implements ConstraintValidatorFactoryInterface | |
24 { | |
25 private $container; | |
26 private $validators; | |
27 | |
28 public function __construct(ContainerInterface $container) | |
29 { | |
30 $this->container = $container; | |
31 $this->validators = array(); | |
32 } | |
33 | |
34 /** | |
35 * {@inheritdoc} | |
36 * | |
37 * @throws ValidatorException When the validator class does not exist | |
38 * @throws UnexpectedTypeException When the validator is not an instance of ConstraintValidatorInterface | |
39 */ | |
40 public function getInstance(Constraint $constraint) | |
41 { | |
42 $name = $constraint->validatedBy(); | |
43 | |
44 if (!isset($this->validators[$name])) { | |
45 if ($this->container->has($name)) { | |
46 $this->validators[$name] = $this->container->get($name); | |
47 } else { | |
48 if (!class_exists($name)) { | |
49 throw new ValidatorException(sprintf('Constraint validator "%s" does not exist or it is not enabled. Check the "validatedBy" method in your constraint class "%s".', $name, get_class($constraint))); | |
50 } | |
51 | |
52 $this->validators[$name] = new $name(); | |
53 } | |
54 } | |
55 | |
56 if (!$this->validators[$name] instanceof ConstraintValidatorInterface) { | |
57 throw new UnexpectedTypeException($this->validators[$name], ConstraintValidatorInterface::class); | |
58 } | |
59 | |
60 return $this->validators[$name]; | |
61 } | |
62 } |