• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

contributte / validator / 19673252758

25 Nov 2025 02:37PM UTC coverage: 89.474% (+1.1%) from 88.421%
19673252758

push

github

jiripudil
update gh actions

85 of 95 relevant lines covered (89.47%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.47
/src/ContainerConstraintValidatorFactory.php
1
<?php declare(strict_types = 1);
2

3
namespace Contributte\Validator;
4

5
use Nette\DI\Container;
6
use Symfony\Component\Validator\Constraint;
7
use Symfony\Component\Validator\Constraints\ExpressionValidator;
8
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
9
use Symfony\Component\Validator\ConstraintValidatorInterface;
10
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
11
use Symfony\Component\Validator\Exception\ValidatorException;
12
use function class_exists;
13
use function get_class;
14
use function sprintf;
15

16
final class ContainerConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
17
{
18

19
        /** @var ConstraintValidatorInterface[] */
20
        private array $validators;
21

22
        public function __construct(
23
                private readonly Container $container,
1✔
24
        )
25
        {
26
                $this->validators = [];
1✔
27
        }
1✔
28

29
        /**
30
         * {@inheritdoc}
31
         *
32
         * @throws ValidatorException      when the validator class does not exist
33
         * @throws UnexpectedTypeException when the validator is not an instance of ConstraintValidatorInterface
34
         */
35
        public function getInstance(Constraint $constraint): ConstraintValidatorInterface
1✔
36
        {
37
                /** @var class-string<ConstraintValidatorInterface> $name */
38
                $name = $constraint->validatedBy() === 'validator.expression'
1✔
39
                        ? ExpressionValidator::class
×
40
                        : $constraint->validatedBy();
1✔
41

42
                if (!isset($this->validators[$name])) {
1✔
43
                        $validator = $this->container->getByType($name, false);
1✔
44

45
                        if ($validator !== null) {
1✔
46
                                $this->validators[$name] = $validator;
×
47

48
                        } else {
49
                                if (!class_exists($name)) {
1✔
50
                                        throw new ValidatorException(sprintf('Constraint validator "%s" does not exist or is not enabled. Check the "validatedBy" method in your constraint class "%s".', $name, get_class($constraint)));
×
51
                                }
52

53
                                $this->validators[$name] = $this->container->createInstance($name);
1✔
54
                        }
55
                }
56

57
                if (!$this->validators[$name] instanceof ConstraintValidatorInterface) {
1✔
58
                        throw new UnexpectedTypeException($this->validators[$name], ConstraintValidatorInterface::class);
×
59
                }
60

61
                return $this->validators[$name];
1✔
62
        }
63

64
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc