1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Philip Bergman <[email protected]> |
4
|
|
|
* @copyright Zicht Online <http://www.zicht.nl> |
5
|
|
|
*/ |
6
|
|
|
namespace Zicht\Bundle\UrlBundle\Validator\Constraints; |
7
|
|
|
|
8
|
|
|
use Doctrine\Bundle\DoctrineBundle\Registry; |
9
|
|
|
use Symfony\Component\Validator\Constraint; |
10
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
11
|
|
|
use Zicht\Bundle\UrlBundle\Entity\UrlAlias; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class ContainsUrlAliasValidator |
15
|
|
|
* |
16
|
|
|
* @package Zicht\Bundle\UrlBundle\Validator\Constraints |
17
|
|
|
*/ |
18
|
|
|
class ContainsUrlAliasValidator extends ConstraintValidator |
19
|
|
|
{ |
20
|
|
|
/** @var Registry */ |
21
|
|
|
protected $doctrine; |
22
|
|
|
/** @var bool */ |
23
|
|
|
protected $isStrict; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor |
27
|
|
|
* |
28
|
|
|
* @param Registry $doctrine |
29
|
|
|
* @param bool $isStrict |
30
|
|
|
*/ |
31
|
|
|
public function __construct(Registry $doctrine, $isStrict = false) |
32
|
|
|
{ |
33
|
|
|
$this->doctrine = $doctrine; |
34
|
|
|
$this->isStrict = $isStrict; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Checks if the passed value is valid. |
40
|
|
|
* |
41
|
|
|
* @param mixed $value The value that should be validated |
42
|
|
|
* @param Constraint $constraint The constraint for the validation |
43
|
|
|
*/ |
44
|
|
|
public function validate($value, Constraint $constraint) |
45
|
|
|
{ |
46
|
|
|
if (!$value instanceof UrlAlias) { |
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (false === $this->isStrict) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// check for a managed state, if a empty array is returned |
55
|
|
|
// it is save to say we got a new entity. |
56
|
|
|
if ([] !== ($original = $this->getOriginalData($value))) { |
57
|
|
|
// no change in public_url field while updating, so no validation needed. |
58
|
|
|
if ($original['public_url'] === $value->getPublicUrl()) { |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (0 < $this->getPublicUrlCount($value->getPublicUrl())) { |
64
|
|
|
$this->addViolation($value->getPublicUrl(), $constraint); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $url |
71
|
|
|
* @param Constraint $constraint |
72
|
|
|
*/ |
73
|
|
|
public function addViolation($url, Constraint $constraint) |
74
|
|
|
{ |
75
|
|
|
$this->context->addViolation($constraint->message, ['%url%' => $url]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param mixed $value |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
public function getOriginalData($value) |
83
|
|
|
{ |
84
|
|
|
return $this->doctrine->getManager()->getUnitOfWork()->getOriginalEntityData($value); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $url |
89
|
|
|
* @return int |
90
|
|
|
*/ |
91
|
|
|
protected function getPublicUrlCount($url) |
92
|
|
|
{ |
93
|
|
|
return (int)$this->doctrine->getConnection()->query($this->fmtQuery($url))->fetchColumn(0); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Create a select (count) query with the given url. |
98
|
|
|
* |
99
|
|
|
* @param string $url |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
protected function fmtQuery($url) |
103
|
|
|
{ |
104
|
|
|
return sprintf('SELECT COUNT(*) FROM url_alias WHERE public_url = %s', $this->doctrine->getConnection()->quote($url)); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|