1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics) |
6
|
|
|
* |
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License as published |
9
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
18
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace App\Validator\Constraints; |
22
|
|
|
|
23
|
|
|
use Symfony\Component\Validator\Constraint; |
24
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
25
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
26
|
|
|
|
27
|
|
|
class ValidThemeValidator extends ConstraintValidator |
28
|
|
|
{ |
29
|
|
|
private array $available_themes; |
30
|
|
|
|
31
|
|
|
public function __construct(array $available_themes) |
32
|
|
|
{ |
33
|
|
|
$this->available_themes = $available_themes; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function validate($value, Constraint $constraint) |
37
|
|
|
{ |
38
|
|
|
if (!$constraint instanceof ValidTheme) { |
39
|
|
|
throw new UnexpectedTypeException($constraint, ValidTheme::class); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
//Empty values are allowed |
43
|
|
|
if (null === $value || '' === $value) { |
44
|
|
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
//If a value is set, it must be a value from the available themes list |
48
|
|
|
if (!in_array($value, $this->available_themes, true)) { |
49
|
|
|
$this->context->buildViolation($constraint->message) |
50
|
|
|
->setParameter('{{ value }}', $value) |
51
|
|
|
->addViolation(); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |