1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the CRUDlex package. |
5
|
|
|
* |
6
|
|
|
* (c) Philip Lehmann-Böhm <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CRUDlex; |
13
|
|
|
|
14
|
|
|
use \Valdi\Validator\ValidatorInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* A validator to check for an unique field. |
18
|
|
|
*/ |
19
|
|
|
class UniqueValidator implements ValidatorInterface { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Checks whether the unique constraint is valid for a many-to-many field. |
23
|
|
|
* |
24
|
|
|
* @param array $value |
25
|
|
|
* the value to check |
26
|
|
|
* @param AbstractData $data |
27
|
|
|
* the data to perform the check with |
28
|
|
|
* @param Entity $entity |
29
|
|
|
* the entity to perform the check on |
30
|
|
|
* @param $field |
31
|
|
|
* the many field to perform the check on |
32
|
|
|
* |
33
|
|
|
* @return boolean |
34
|
|
|
* true if it is a valid unique many-to-many constraint |
35
|
|
|
*/ |
36
|
|
|
protected function isValidUniqueMany(array $value, AbstractData $data, Entity $entity, $field) { |
37
|
|
|
return !$data->hasManySet($field, array_column($value, 'id'), $entity->get('id')); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Performs the regular unique validation. |
42
|
|
|
* |
43
|
|
|
* @param $value |
44
|
|
|
* the value to validate |
45
|
|
|
* @param $data |
46
|
|
|
* the data instance to validate with |
47
|
|
|
* @param $entity |
48
|
|
|
* the entity of the field |
49
|
|
|
* @param $field |
50
|
|
|
* the field to validate |
51
|
|
|
* |
52
|
|
|
* @return boolean |
53
|
|
|
* true if everything is valid |
54
|
|
|
*/ |
55
|
|
|
protected function isValidUnique($value, AbstractData $data, Entity $entity, $field) |
56
|
|
|
{ |
57
|
|
|
$params = [$field => $value]; |
|
|
|
|
58
|
|
|
$paramsOperators = [$field => '=']; |
59
|
|
|
if ($entity->get('id') !== null) { |
60
|
|
|
$params['id'] = $entity->get('id'); |
|
|
|
|
61
|
|
|
$paramsOperators['id'] = '!='; |
62
|
|
|
} |
63
|
|
|
$amount = intval($data->countBy($data->getDefinition()->getTable(), $params, $paramsOperators, true)); |
64
|
|
|
return $amount == 0; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function isValid($value, array $parameters) { |
71
|
|
|
|
72
|
|
|
if (in_array($value, [null, ''])) { |
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$data = $parameters[0]; |
77
|
|
|
$entity = $parameters[1]; |
78
|
|
|
$field = $parameters[2]; |
79
|
|
|
$type = $data->getDefinition()->getType($field); |
80
|
|
|
|
81
|
|
|
if ($type === 'many') { |
82
|
|
|
return $this->isValidUniqueMany($value, $data, $entity, $field); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->isValidUnique($value, $data, $entity, $field); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function getInvalidDetails() { |
92
|
|
|
return 'unique'; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.