|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This software package is licensed under AGPL or Commercial license. |
|
5
|
|
|
* |
|
6
|
|
|
* @package maslosoft/mangan |
|
7
|
|
|
* @licence AGPL or Commercial |
|
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> |
|
9
|
|
|
* @copyright Copyright (c) Maslosoft |
|
10
|
|
|
* @copyright Copyright (c) Others as mentioned in code |
|
11
|
|
|
* @link https://maslosoft.com/mangan/ |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Maslosoft\Mangan\Validators\BuiltIn; |
|
15
|
|
|
|
|
16
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
|
17
|
|
|
use Maslosoft\Mangan\Criteria; |
|
18
|
|
|
use Maslosoft\Mangan\Finder; |
|
19
|
|
|
use Maslosoft\Mangan\Helpers\PkManager; |
|
20
|
|
|
use Maslosoft\Mangan\Interfaces\Validators\ValidatorInterface; |
|
21
|
|
|
use Maslosoft\Mangan\Meta\ManganMeta; |
|
22
|
|
|
use Maslosoft\Mangan\ScenarioManager; |
|
23
|
|
|
use Maslosoft\Mangan\Validators\Traits\AllowEmpty; |
|
24
|
|
|
use Maslosoft\Mangan\Validators\Traits\Messages; |
|
25
|
|
|
use Maslosoft\Mangan\Validators\Traits\OnScenario; |
|
26
|
|
|
use Maslosoft\Mangan\Validators\Traits\Safe; |
|
27
|
|
|
use Maslosoft\Mangan\Validators\Traits\SkipOnError; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* UniqueValidator class file. |
|
31
|
|
|
* |
|
32
|
|
|
* @author Ianaré Sévi |
|
33
|
|
|
* @author Florian Fackler <[email protected]> |
|
34
|
|
|
* @link http://mintao.com |
|
35
|
|
|
* @copyright Copyright (c) 2008-2010 Yii Software LLC |
|
36
|
|
|
* @license New BSD license |
|
37
|
|
|
*/ |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* UniqueValidator validates that the attribute value is unique in the corresponding database table. |
|
41
|
|
|
* |
|
42
|
|
|
* @author Florian Fackler <[email protected]> |
|
43
|
|
|
* @version $Id$ |
|
44
|
|
|
* @package system.validators |
|
45
|
|
|
* @since 1.0 |
|
46
|
|
|
*/ |
|
47
|
|
|
class UniqueValidator implements ValidatorInterface |
|
48
|
|
|
{ |
|
49
|
|
|
|
|
50
|
|
|
use AllowEmpty, |
|
51
|
|
|
SkipOnError, |
|
52
|
|
|
Messages, |
|
53
|
|
|
OnScenario, |
|
54
|
|
|
Safe; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var string the document class name that should be used to |
|
58
|
|
|
* look for the attribute value being validated. Defaults to null, meaning using |
|
59
|
|
|
* the class of the object currently being validated. |
|
60
|
|
|
* |
|
61
|
|
|
* @see attributeName |
|
62
|
|
|
* @since 1.0.8 |
|
63
|
|
|
*/ |
|
64
|
|
|
public $className; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var string the ActiveRecord class attribute name that should be |
|
68
|
|
|
* used to look for the attribute value being validated. Defaults to null, |
|
69
|
|
|
* meaning using the name of the attribute being validated. |
|
70
|
|
|
* |
|
71
|
|
|
* @see className |
|
72
|
|
|
* @since 1.0.8 |
|
73
|
|
|
*/ |
|
74
|
|
|
public $attributeName; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @var array additional query criteria. This will be combined with the condition |
|
78
|
|
|
* that checks if the attribute value exists in the corresponding table column. |
|
79
|
|
|
* This array will be used to instantiate a {@link Criteria} object. |
|
80
|
|
|
* @since 1.0.8 |
|
81
|
|
|
*/ |
|
82
|
|
|
public $criteria = []; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @Label('{attribute} "{value}" has already been taken') |
|
86
|
|
|
* @var string |
|
87
|
|
|
*/ |
|
88
|
|
|
public $msgTaken = ''; |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Validates the attribute of the object. |
|
92
|
|
|
* If there is any error, the error message is added to the object. |
|
93
|
|
|
* @param AnnotatedInterface $model the object being validated |
|
94
|
|
|
* @param string $attribute the attribute being validated |
|
95
|
|
|
* @return bool |
|
96
|
|
|
*/ |
|
97
|
4 |
|
public function isValid(AnnotatedInterface $model, $attribute) |
|
98
|
|
|
{ |
|
99
|
4 |
|
$value = $model->$attribute; |
|
100
|
4 |
|
if ($this->allowEmpty && empty($value)) |
|
101
|
|
|
{ |
|
102
|
|
|
return true; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
4 |
|
if(empty($this->className)) |
|
106
|
|
|
{ |
|
107
|
|
|
// NOTE: Clone model, as it's attribute value might |
|
108
|
|
|
// depend on other attributes and could be |
|
109
|
|
|
// corrupted by condition decorator in criteria. |
|
110
|
|
|
// The clone is needed as scenario is changed later. |
|
111
|
4 |
|
$compareModel = clone $model; |
|
112
|
|
|
} |
|
113
|
|
|
else |
|
114
|
|
|
{ |
|
115
|
|
|
$compareModel = new $this->className; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
4 |
|
$criteria = new Criteria(null, $compareModel); |
|
119
|
4 |
|
$criteria->addCond($attribute, '==', $value); |
|
120
|
|
|
|
|
121
|
4 |
|
if ($this->criteria !== []) |
|
122
|
|
|
{ |
|
123
|
1 |
|
$criteria->mergeWith($this->criteria); |
|
124
|
|
|
} |
|
125
|
4 |
|
ScenarioManager::setScenario($compareModel, ValidatorInterface::ScenarioValidate); |
|
126
|
4 |
|
$finder = new Finder($compareModel); |
|
127
|
|
|
|
|
128
|
4 |
|
$found = $finder->find($criteria); |
|
129
|
|
|
|
|
130
|
|
|
// Not found entirely |
|
131
|
4 |
|
if (null === $found) |
|
132
|
|
|
{ |
|
133
|
2 |
|
return true; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
// Same pk |
|
137
|
3 |
|
if (PkManager::compare($found, $model)) |
|
138
|
|
|
{ |
|
139
|
2 |
|
return true; |
|
140
|
|
|
} |
|
141
|
3 |
|
$label = ManganMeta::create($model)->field($attribute)->label; |
|
142
|
3 |
|
$this->addError('msgTaken', ['{attribute}' => $label, '{value}' => $value]); |
|
143
|
3 |
|
return false; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
} |
|
147
|
|
|
|