Passed
Pull Request — master (#1703)
by Tarmo
08:51
created

LocaleValidator::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Validator/Constraints/LocaleValidator.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Validator\Constraints;
10
11
use App\Service\Localization;
12
use Symfony\Component\Validator\Constraint;
13
use Symfony\Component\Validator\ConstraintValidator;
14
use function in_array;
15
16
/**
17
 * Class LocaleValidator
18
 *
19
 * @package App\Validator\Constraints
20
 * @author TLe, Tarmo Leppänen <[email protected]>
21
 */
22
class LocaleValidator extends ConstraintValidator
23
{
24 44
    public function __construct(
25
        private Localization $localization,
26
    ) {
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 44
    public function validate(mixed $value, Constraint $constraint): void
33
    {
34 44
        if (in_array($value, $this->localization->getLocales(), true) !== true) {
35 1
            $this->context
36 1
                ->buildViolation(Locale::MESSAGE)
37 1
                ->setParameter('{{ locale }}', (string)$value)
38 1
                ->setCode(Locale::INVALID_LOCALE)
39 1
                ->addViolation();
40
        }
41
    }
42
}
43