1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Mgid\Component\Pagination\Validator; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Validator\Exception; |
6
|
|
|
use Symfony\Component\Validator\Constraint; |
7
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
8
|
|
|
use Mgid\Component\Pagination\InputInterface; |
9
|
|
|
|
10
|
|
|
final class StructureValidator extends ConstraintValidator |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* {@inheritdoc} |
14
|
|
|
*/ |
15
|
|
|
public function validate($input, Constraint $constraint): void |
16
|
|
|
{ |
17
|
|
|
if (!$input instanceof InputInterface) { |
18
|
|
|
throw new Exception\UnexpectedTypeException($input, InputInterface::class); // @codeCoverageIgnore |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
if (!$constraint instanceof Structure) { |
22
|
|
|
throw new Exception\UnexpectedTypeException($constraint, Structure::class); // @codeCoverageIgnore |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
$this->validateOrders($input->getOrders(), $constraint); |
26
|
|
|
$this->validateFilters($input->getFilters(), $constraint); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param array<mixed> $orders |
31
|
|
|
* @param Structure $constraint |
32
|
|
|
*/ |
33
|
|
|
private function validateOrders(array $orders, Structure $constraint): void |
34
|
|
|
{ |
35
|
|
|
foreach ($orders as $fieldName => $order) { |
36
|
|
|
if (!\is_string($fieldName) || !\is_string($order)) { |
37
|
|
|
$this->context->buildViolation($constraint->message)->atPath('orders')->addViolation(); |
38
|
|
|
|
39
|
|
|
break; |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param array<mixed> $filters |
46
|
|
|
* @param Structure $constraint |
47
|
|
|
*/ |
48
|
|
|
private function validateFilters(array $filters, Structure $constraint): void |
49
|
|
|
{ |
50
|
|
|
foreach ($filters as $fieldName => $operators) { |
51
|
|
|
if (!\is_string($fieldName) || !\is_array($operators)) { |
52
|
|
|
$this->context->buildViolation($constraint->message)->atPath('filters')->addViolation(); |
53
|
|
|
|
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
foreach ($operators as $operator => $value) { |
58
|
|
|
if (!\is_string($operator)) { |
59
|
|
|
$this->context->buildViolation($constraint->message)->atPath('filters.operators')->addViolation(); |
60
|
|
|
|
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|