|
1
|
|
|
<?php namespace Limoncello\Flute\Validation\Serialize; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015-2018 [email protected] |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use Limoncello\Validation\Contracts\Execution\BlockSerializerInterface; |
|
20
|
|
|
use Limoncello\Validation\Contracts\Rules\RuleInterface; |
|
21
|
|
|
use Limoncello\Validation\Execution\BlockSerializer; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @package Limoncello\Flute |
|
25
|
|
|
*/ |
|
26
|
|
|
class RulesSerializer |
|
27
|
|
|
{ |
|
28
|
|
|
// Single rule serialization keys |
|
29
|
|
|
|
|
30
|
|
|
/** Index key */ |
|
31
|
|
|
protected const RULES_SINGLE_INDEX = 0; |
|
32
|
|
|
|
|
33
|
|
|
/** Index key */ |
|
34
|
|
|
protected const RULES_SINGLE_START_INDEXES = self::RULES_SINGLE_INDEX + 1; |
|
35
|
|
|
|
|
36
|
|
|
/** Index key */ |
|
37
|
|
|
protected const RULES_SINGLE_END_INDEXES = self::RULES_SINGLE_START_INDEXES + 1; |
|
38
|
|
|
|
|
39
|
|
|
// Rules array serialization keys |
|
40
|
|
|
|
|
41
|
|
|
/** Index key */ |
|
42
|
|
|
protected const RULES_ARRAY_INDEXES = 0; |
|
43
|
|
|
|
|
44
|
|
|
/** Index key */ |
|
45
|
|
|
protected const RULES_ARRAY_START_INDEXES = self::RULES_ARRAY_INDEXES + 1; |
|
46
|
|
|
|
|
47
|
|
|
/** Index key */ |
|
48
|
|
|
protected const RULES_ARRAY_END_INDEXES = self::RULES_ARRAY_START_INDEXES + 1; |
|
49
|
|
|
|
|
50
|
|
|
/** Index key. Every rule is serialized independently */ |
|
51
|
|
|
protected const RULES_ARRAY_SINGLE_INDEXES = self::RULES_ARRAY_END_INDEXES + 1; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var BlockSerializerInterface |
|
55
|
|
|
*/ |
|
56
|
|
|
private $blockSerializer; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param RuleInterface $rule |
|
60
|
|
|
* |
|
61
|
|
|
* @return array |
|
62
|
|
|
*/ |
|
63
|
62 |
|
public function addRule(RuleInterface $rule): array |
|
64
|
|
|
{ |
|
65
|
62 |
|
$this->getSerializer()->clearBlocksWithStart()->clearBlocksWithEnd(); |
|
66
|
|
|
|
|
67
|
|
|
$result = [ |
|
68
|
62 |
|
static::RULES_SINGLE_INDEX => $this->getSerializer()->addBlock($rule->toBlock()), |
|
69
|
62 |
|
static::RULES_SINGLE_START_INDEXES => $this->getSerializer()->getBlocksWithStart(), |
|
70
|
62 |
|
static::RULES_SINGLE_END_INDEXES => $this->getSerializer()->getBlocksWithEnd(), |
|
71
|
|
|
]; |
|
72
|
|
|
|
|
73
|
62 |
|
return $result; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param BlockSerializerInterface $blockSerializer |
|
78
|
|
|
*/ |
|
79
|
62 |
|
public function __construct(BlockSerializerInterface $blockSerializer) |
|
80
|
|
|
{ |
|
81
|
62 |
|
$this->blockSerializer = $blockSerializer; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param RuleInterface[] $rules |
|
86
|
|
|
* |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
62 |
|
public function addRules(array $rules): array |
|
90
|
|
|
{ |
|
91
|
|
|
// serialize the rules altogether |
|
92
|
|
|
|
|
93
|
62 |
|
$this->getSerializer()->clearBlocksWithStart()->clearBlocksWithEnd(); |
|
94
|
|
|
|
|
95
|
62 |
|
$indexes = []; |
|
96
|
62 |
|
foreach ($rules as $name => $rule) { |
|
97
|
57 |
|
assert($rule instanceof RuleInterface); |
|
98
|
|
|
|
|
99
|
57 |
|
$ruleName = $rule->getName(); |
|
100
|
57 |
|
if (empty($ruleName) === true) { |
|
101
|
57 |
|
$ruleName = $name; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
57 |
|
$block = $rule->setName($ruleName)->enableCapture()->toBlock(); |
|
105
|
57 |
|
$indexes[$name] = $this->getSerializer()->addBlock($block); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$ruleIndexes = [ |
|
109
|
62 |
|
static::RULES_ARRAY_INDEXES => $indexes, |
|
110
|
62 |
|
static::RULES_ARRAY_START_INDEXES => $this->getSerializer()->getBlocksWithStart(), |
|
111
|
62 |
|
static::RULES_ARRAY_END_INDEXES => $this->getSerializer()->getBlocksWithEnd(), |
|
112
|
|
|
]; |
|
113
|
|
|
|
|
114
|
62 |
|
$this->getSerializer()->clearBlocksWithStart()->clearBlocksWithEnd(); |
|
115
|
|
|
|
|
116
|
|
|
// sometimes (e.g. update in relationship) an individual validation rule is needed |
|
117
|
|
|
// so we should have a second serialization of each rule individually |
|
118
|
|
|
|
|
119
|
62 |
|
$individualRules = []; |
|
120
|
62 |
|
foreach ($rules as $name => $rule) { |
|
121
|
57 |
|
$individualRules[$name] = $this->addRule($rule); |
|
122
|
|
|
} |
|
123
|
62 |
|
$ruleIndexes[static::RULES_ARRAY_SINGLE_INDEXES] = $individualRules; |
|
124
|
|
|
|
|
125
|
62 |
|
return $ruleIndexes; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return array |
|
130
|
|
|
* |
|
131
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
132
|
|
|
*/ |
|
133
|
62 |
|
public function getBlocks(): array |
|
134
|
|
|
{ |
|
135
|
62 |
|
$blocks = BlockSerializer::unserializeBlocks($this->getSerializer()->get()); |
|
136
|
|
|
|
|
137
|
62 |
|
return $blocks; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param array $singleRuleIndexes |
|
142
|
|
|
* |
|
143
|
|
|
* @return int |
|
144
|
|
|
*/ |
|
145
|
19 |
|
public static function getRuleIndex(array $singleRuleIndexes): int |
|
146
|
|
|
{ |
|
147
|
19 |
|
assert(array_key_exists(static::RULES_SINGLE_INDEX, $singleRuleIndexes)); |
|
148
|
19 |
|
$result = $singleRuleIndexes[static::RULES_SINGLE_INDEX]; |
|
149
|
|
|
|
|
150
|
19 |
|
return $result; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param array $singleRuleIndexes |
|
155
|
|
|
* |
|
156
|
|
|
* @return array |
|
157
|
|
|
*/ |
|
158
|
20 |
|
public static function getRuleStartIndexes(array $singleRuleIndexes): array |
|
159
|
|
|
{ |
|
160
|
20 |
|
assert(array_key_exists(static::RULES_SINGLE_START_INDEXES, $singleRuleIndexes)); |
|
161
|
20 |
|
$result = $singleRuleIndexes[static::RULES_SINGLE_START_INDEXES]; |
|
162
|
|
|
|
|
163
|
20 |
|
return $result; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @param array $singleRuleIndexes |
|
168
|
|
|
* |
|
169
|
|
|
* @return array |
|
170
|
|
|
*/ |
|
171
|
20 |
|
public static function getRuleEndIndexes(array $singleRuleIndexes): array |
|
172
|
|
|
{ |
|
173
|
20 |
|
assert(array_key_exists(static::RULES_SINGLE_END_INDEXES, $singleRuleIndexes)); |
|
174
|
20 |
|
$result = $singleRuleIndexes[static::RULES_SINGLE_END_INDEXES]; |
|
175
|
|
|
|
|
176
|
20 |
|
return $result; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param array $arrayRulesIndexes |
|
181
|
|
|
* |
|
182
|
|
|
* @return array |
|
183
|
|
|
*/ |
|
184
|
52 |
|
public static function getRulesIndexes(array $arrayRulesIndexes): array |
|
185
|
|
|
{ |
|
186
|
52 |
|
assert(array_key_exists(static::RULES_ARRAY_INDEXES, $arrayRulesIndexes)); |
|
187
|
52 |
|
$result = $arrayRulesIndexes[static::RULES_ARRAY_INDEXES]; |
|
188
|
|
|
|
|
189
|
52 |
|
return $result; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @param array $arrayRulesIndexes |
|
194
|
|
|
* |
|
195
|
|
|
* @return array |
|
196
|
|
|
*/ |
|
197
|
61 |
|
public static function getRulesStartIndexes(array $arrayRulesIndexes): array |
|
198
|
|
|
{ |
|
199
|
61 |
|
assert(array_key_exists(static::RULES_ARRAY_START_INDEXES, $arrayRulesIndexes)); |
|
200
|
61 |
|
$result = $arrayRulesIndexes[static::RULES_ARRAY_START_INDEXES]; |
|
201
|
|
|
|
|
202
|
61 |
|
return $result; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @param array $arrayRulesIndexes |
|
207
|
|
|
* |
|
208
|
|
|
* @return array |
|
209
|
|
|
*/ |
|
210
|
61 |
|
public static function getRulesEndIndexes(array $arrayRulesIndexes): array |
|
211
|
|
|
{ |
|
212
|
61 |
|
assert(array_key_exists(static::RULES_ARRAY_END_INDEXES, $arrayRulesIndexes)); |
|
213
|
61 |
|
$result = $arrayRulesIndexes[static::RULES_ARRAY_END_INDEXES]; |
|
214
|
|
|
|
|
215
|
61 |
|
return $result; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @param array $arrayRulesIndexes |
|
220
|
|
|
* @param string $name |
|
221
|
|
|
* |
|
222
|
|
|
* @return array |
|
223
|
|
|
*/ |
|
224
|
6 |
|
public static function geSingleRuleIndexes(array $arrayRulesIndexes, string $name): array |
|
225
|
|
|
{ |
|
226
|
6 |
|
assert(array_key_exists(static::RULES_ARRAY_SINGLE_INDEXES, $arrayRulesIndexes)); |
|
227
|
6 |
|
$rules = $arrayRulesIndexes[static::RULES_ARRAY_SINGLE_INDEXES]; |
|
228
|
6 |
|
assert(array_key_exists($name, $rules)); |
|
229
|
6 |
|
$result = $rules[$name]; |
|
230
|
|
|
|
|
231
|
6 |
|
return $result; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* @return BlockSerializerInterface |
|
236
|
|
|
*/ |
|
237
|
62 |
|
protected function getSerializer(): BlockSerializerInterface |
|
238
|
|
|
{ |
|
239
|
62 |
|
return $this->blockSerializer; |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|