1 | <?php |
||
17 | class AttributeIndexValidator extends Validator |
||
18 | { |
||
19 | /** |
||
20 | * Separator between base value and index. |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | public $separator = '-'; |
||
25 | |||
26 | /** |
||
27 | * Start index value. |
||
28 | * |
||
29 | * @var integer |
||
30 | */ |
||
31 | public $startIndex = 1; |
||
32 | |||
33 | /** |
||
34 | * Additional filter applied to query used to check uniqueness. |
||
35 | * |
||
36 | * @var string|array|Closure |
||
37 | */ |
||
38 | public $filter = null; |
||
39 | |||
40 | /** |
||
41 | * Escaped separator. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $escapedSeparator = null; |
||
46 | |||
47 | /** |
||
48 | * Escape string for regexp. |
||
49 | * |
||
50 | * @param string $string |
||
51 | * @return string |
||
52 | */ |
||
53 | 32 | protected function escapeForRegexp($string) |
|
57 | |||
58 | /** |
||
59 | * Get escaped separator for regexps. |
||
60 | * |
||
61 | * @return string |
||
62 | */ |
||
63 | 32 | protected function getEscapedSeparator() |
|
71 | |||
72 | /** |
||
73 | * Add filter to query (if any exists). |
||
74 | * |
||
75 | * @param ActiveQueryInterface $query |
||
76 | */ |
||
77 | 32 | protected function addFilterToQuery(ActiveQueryInterface $query) |
|
89 | |||
90 | /** |
||
91 | * Get condition to exclude current model by it's primay key. |
||
92 | * |
||
93 | * If model is new, empty array will be returned. |
||
94 | * |
||
95 | * @param ActiveRecordInterface $model |
||
96 | * @return array |
||
97 | */ |
||
98 | 32 | protected function getExcludeByPkCondition(ActiveRecordInterface $model) |
|
108 | |||
109 | /** |
||
110 | * Whether there are an attribute value collision. |
||
111 | * |
||
112 | * @param ActiveRecordInterface $model |
||
113 | * @param string $attribute |
||
114 | * @return boolean |
||
115 | */ |
||
116 | 32 | protected function hasCollision(ActiveRecordInterface $model, $attribute) |
|
126 | |||
127 | /** |
||
128 | * Get attribute value common part, e. g. part without index but with separator. |
||
129 | * |
||
130 | * For example, common part for 'test' and 'test-1' will be 'test-'. |
||
131 | * |
||
132 | * @param ActiveRecordInterface $model |
||
133 | * @param string $attribute |
||
134 | * @return string |
||
135 | */ |
||
136 | 32 | protected function getCommonPart(ActiveRecordInterface $model, $attribute) |
|
142 | |||
143 | /** |
||
144 | * Find max index stored in database. |
||
145 | * |
||
146 | * If no index found, startIndex - 1 will be returned. |
||
147 | * |
||
148 | * @param ActiveRecordInterface $model |
||
149 | * @param string $attribute |
||
150 | * @param string $commonPart |
||
151 | * @return integer |
||
152 | */ |
||
153 | 32 | protected function findMaxIndex(ActiveRecordInterface $model, $attribute, $commonPart) |
|
179 | |||
180 | /** |
||
181 | * @inheritdoc |
||
182 | */ |
||
183 | 32 | public function validateAttribute($model, $attribute) |
|
194 | } |
||
195 |