|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Validator; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\ServiceAttribute; |
|
6
|
|
|
use App\Entity\ServiceAttributeType; |
|
7
|
|
|
use App\Repository\ServiceAttributeRepositoryInterface; |
|
8
|
|
|
use App\Utils\ArrayUtils; |
|
9
|
|
|
use Symfony\Component\Validator\Constraint; |
|
10
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
|
11
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
|
12
|
|
|
|
|
13
|
|
|
class ValidAttributesArrayValidator extends ConstraintValidator { |
|
14
|
|
|
|
|
15
|
|
|
public function __construct(private ServiceAttributeRepositoryInterface $attributeRepository) |
|
16
|
|
|
{ |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @inheritDoc |
|
21
|
|
|
*/ |
|
22
|
|
|
public function validate($value, Constraint $constraint) { |
|
23
|
|
|
if(!$constraint instanceof ValidAttributesArray) { |
|
24
|
|
|
throw new UnexpectedTypeException($constraint, ValidAttributesArray::class); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** @var ServiceAttribute[] $attributes */ |
|
28
|
|
|
$attributes = ArrayUtils::createArrayWithKeys( |
|
29
|
|
|
$this->attributeRepository->findAll(), |
|
30
|
|
|
fn(ServiceAttribute $attribute) => $attribute->getName()); |
|
31
|
|
|
$validAttributeNames = array_keys($attributes); |
|
32
|
|
|
|
|
33
|
|
|
foreach($value as $attributeName => $attributeValue) { |
|
34
|
|
|
if(!in_array($attributeName, $validAttributeNames)) { |
|
35
|
|
|
$this->context |
|
36
|
|
|
->buildViolation($constraint->messageNotFound) |
|
37
|
|
|
->setParameter('{{ name }}', $attributeName) |
|
38
|
|
|
->addViolation(); |
|
39
|
|
|
continue; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$attribute = $attributes[$attributeName]; |
|
43
|
|
|
|
|
44
|
|
|
if($attribute->getType() === ServiceAttributeType::Text && $attributeValue !== null && !is_scalar($attributeValue)) { |
|
45
|
|
|
$this->context |
|
46
|
|
|
->buildViolation($constraint->messageInvalidValue) |
|
47
|
|
|
->setParameter('{{ name }}', $attributeName) |
|
48
|
|
|
->setParameter('{{ type }}', 'string') |
|
49
|
|
|
->setParameter('{{ given }}', gettype($attributeValue)) |
|
50
|
|
|
->addViolation(); |
|
51
|
|
|
} else if($attribute->getType() === ServiceAttributeType::Select) { |
|
52
|
|
|
if(!is_array($attributeValue)) { |
|
53
|
|
|
$this->context |
|
54
|
|
|
->buildViolation($constraint->messageInvalidValue) |
|
55
|
|
|
->setParameter('{{ name }}', $attributeName) |
|
56
|
|
|
->setParameter('{{ type }}', 'array') |
|
57
|
|
|
->setParameter('{{ given }}', gettype($attributeValue)) |
|
58
|
|
|
->addViolation(); |
|
59
|
|
|
} else { |
|
60
|
|
|
$i = 0; |
|
61
|
|
|
$validValues = array_keys($attribute->getOptions()); |
|
62
|
|
|
|
|
63
|
|
|
foreach($attributeValue as $arrayValue) { |
|
64
|
|
|
if(!in_array($arrayValue, $validValues)) { |
|
65
|
|
|
$this->context |
|
66
|
|
|
->buildViolation($constraint->messageInvalidArrayItem) |
|
67
|
|
|
->setParameter('{{ name }}', $attributeName) |
|
68
|
|
|
->setParameter('{{ valid }}', json_encode($validValues, JSON_THROW_ON_ERROR)) |
|
69
|
|
|
->setParameter('{{ given }}', $arrayValue) |
|
70
|
|
|
->setParameter('{{ pos }}', (string)$i) |
|
71
|
|
|
->addViolation(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$i++; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
} |