1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Silverback\ApiComponentBundle\Validator\Constraints; |
6
|
|
|
|
7
|
|
|
use Silverback\ApiComponentBundle\Entity\SortableInterface; |
8
|
|
|
use Silverback\ApiComponentBundle\Repository\ComponentLocationRepository; |
9
|
|
|
use Symfony\Component\Validator\Constraint; |
10
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
11
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
12
|
|
|
use Silverback\ApiComponentBundle\Entity\Component\ComponentLocation as ComponentLocationEntity; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* THIS WAS CREATED TO SORT ABSTRACT PAGES - IT DOES NOT DO THAT AND APPEARS REDUNDANT - NEEDS CHECKING AND REMOVING |
16
|
|
|
* Class SortableValidator |
17
|
|
|
* @package Silverback\ApiComponentBundle\Validator\Constraints |
18
|
|
|
*/ |
19
|
|
|
class SortableValidator extends ConstraintValidator |
20
|
|
|
{ |
21
|
|
|
private $componentLocationRepository; |
22
|
|
|
|
23
|
|
|
public function __construct(ComponentLocationRepository $componentLocationRepository) |
24
|
|
|
{ |
25
|
|
|
$this->componentLocationRepository = $componentLocationRepository; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function validate($sortable, Constraint $constraint): void |
29
|
|
|
{ |
30
|
|
|
if (!$constraint instanceof Sortable) { |
31
|
|
|
throw new UnexpectedTypeException($constraint, Sortable::class); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if (!$sortable instanceof SortableInterface) { |
35
|
|
|
throw new UnexpectedTypeException($sortable, SortableInterface::class); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if ($sortable->getSort() === null) { |
39
|
|
|
$collection = $sortable->getSortCollection(); |
40
|
|
|
if ( |
41
|
|
|
$collection === null && |
42
|
|
|
$sortable instanceof ComponentLocationEntity && |
43
|
|
|
($dynamicPageClass = $sortable->getDynamicPageClass()) |
44
|
|
|
) { |
45
|
|
|
$collection = $this->componentLocationRepository->findBy([ |
46
|
|
|
'dynamicPageClass' => $dynamicPageClass |
47
|
|
|
]); |
48
|
|
|
} |
49
|
|
|
$sortable->setSort($sortable->calculateSort(true, $collection)); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if ($sortable->getSort() === null) { |
53
|
|
|
$this->context->buildViolation($constraint->message) |
54
|
|
|
->atPath('sort') |
55
|
|
|
->addViolation(); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|