1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Synapse\Page\Bundle\Validator\Constraints; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Validator\Constraint; |
6
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
7
|
|
|
use Synapse\Page\Bundle\Action\Page\AbstractAction; |
8
|
|
|
use Synapse\Page\Bundle\Action\Page\CreateAction; |
9
|
|
|
use Synapse\Page\Bundle\Entity\Page; |
10
|
|
|
use Synapse\Page\Bundle\Loader\Page\LoaderInterface as PageLoader; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Validator class for UniquePagePath constraint, checks into persistence if |
14
|
|
|
* given page path already exists or not. |
15
|
|
|
*/ |
16
|
|
|
class UniquePagePathValidator extends ConstraintValidator |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var PageLoader |
20
|
|
|
*/ |
21
|
|
|
protected $pageLoader; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Construct. |
25
|
|
|
* |
26
|
|
|
* @param PageLoader $pageLoader |
27
|
|
|
*/ |
28
|
|
|
public function __construct(PageLoader $pageLoader) |
29
|
|
|
{ |
30
|
|
|
$this->pageLoader = $pageLoader; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Validate given page path is unique. |
35
|
|
|
* Works with page create action too. |
36
|
|
|
* |
37
|
|
|
* @param Page $page |
38
|
|
|
* @param Constraint $constraint |
39
|
|
|
*/ |
40
|
|
|
public function validate($page, Constraint $constraint) |
41
|
|
|
{ |
42
|
|
|
if (!$page instanceof CreateAction && !$page instanceof Page) { |
43
|
|
|
throw new \InvalidArgumentException(sprintf( |
44
|
|
|
'%s can only validate %s or %s objects; %s given.', |
45
|
|
|
self::class, |
46
|
|
|
Page::class, |
47
|
|
|
CreateAction::class, |
48
|
|
|
is_object($page) ? get_class($page) : gettype($page) |
49
|
|
|
)); |
50
|
|
|
} |
51
|
|
|
if ($actualPage = $this->pageLoader->retrieveByPath( |
52
|
|
|
$page instanceof CreateAction |
53
|
|
|
? $page->getFullPath() |
54
|
|
|
: $page->getPath() |
55
|
|
|
)) { |
56
|
|
|
$this->context |
57
|
|
|
->buildViolation($constraint->message) |
58
|
|
|
->atPath('path') |
59
|
|
|
->setParameter('{{ name }}', $actualPage->getName()) |
60
|
|
|
->setParameter('{{ path }}', $actualPage->getPath()) |
61
|
|
|
->setParameter('{{ parent }}', $actualPage->getParent()->getName()) |
62
|
|
|
->addViolation() |
63
|
|
|
; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|