1
|
|
|
<?php namespace Limoncello\Application\FormValidation\Execution; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2017 [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 FormRuleSerializer |
27
|
|
|
{ |
28
|
|
|
/** Serialized indexes key */ |
29
|
|
|
protected const SERIALIZED_RULE_SETS = 0; |
30
|
|
|
|
31
|
|
|
/** Serialized rules key */ |
32
|
|
|
protected const SERIALIZED_BLOCKS = self::SERIALIZED_RULE_SETS + 1; |
33
|
|
|
|
34
|
|
|
// Rules array serialization keys |
35
|
|
|
|
36
|
|
|
/** Index key */ |
37
|
|
|
protected const RULES_ARRAY_INDEXES = 0; |
38
|
|
|
|
39
|
|
|
/** Index key */ |
40
|
|
|
protected const RULES_ARRAY_START_INDEXES = self::RULES_ARRAY_INDEXES + 1; |
41
|
|
|
|
42
|
|
|
/** Index key */ |
43
|
|
|
protected const RULES_ARRAY_END_INDEXES = self::RULES_ARRAY_START_INDEXES + 1; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var BlockSerializerInterface |
47
|
|
|
*/ |
48
|
|
|
private $blockSerializer; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
private $ruleSets; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Constructor. |
57
|
|
|
*/ |
58
|
3 |
|
public function __construct() |
59
|
|
|
{ |
60
|
3 |
|
$this->blockSerializer = $this->createBlockSerializer(); |
61
|
3 |
|
$this->ruleSets = []; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $name |
66
|
|
|
* @param RuleInterface[] $attributeRules |
67
|
|
|
* |
68
|
|
|
* @return self |
69
|
|
|
*/ |
70
|
3 |
|
public function addResourceRules(string $name, array $attributeRules): self |
71
|
|
|
{ |
72
|
3 |
|
assert(!empty($name), 'Rule set name cannot be empty.'); |
73
|
3 |
|
assert(!array_key_exists($name, $this->ruleSets), "A rule set with name `$name` has been added already."); |
74
|
|
|
|
75
|
3 |
|
$this->ruleSets[$name] = $this->serializeRulesArray($attributeRules); |
76
|
|
|
|
77
|
3 |
|
$this->getSerializer()->clearBlocksWithStart()->clearBlocksWithEnd(); |
78
|
|
|
|
79
|
3 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return array |
84
|
|
|
* |
85
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
86
|
|
|
*/ |
87
|
3 |
|
public function getData(): array |
88
|
|
|
{ |
89
|
|
|
return [ |
90
|
3 |
|
static::SERIALIZED_RULE_SETS => $this->ruleSets, |
91
|
3 |
|
static::SERIALIZED_BLOCKS => static::getBlocks($this->getSerializer()->get()), |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $name |
97
|
|
|
* @param array $data |
98
|
|
|
* |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
2 |
|
public static function getAttributeRules(string $name, array $data): array |
102
|
|
|
{ |
103
|
2 |
|
assert($data[static::SERIALIZED_RULE_SETS] ?? false); |
104
|
2 |
|
$indexes = $data[static::SERIALIZED_RULE_SETS][$name]; |
105
|
2 |
|
assert(is_array($indexes)); |
106
|
|
|
|
107
|
2 |
|
return $indexes; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array $data |
112
|
|
|
* |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
2 |
|
public static function extractBlocks(array $data): array |
116
|
|
|
{ |
117
|
2 |
|
assert(array_key_exists(static::SERIALIZED_BLOCKS, $data)); |
118
|
2 |
|
$serializedRules = $data[static::SERIALIZED_BLOCKS]; |
119
|
2 |
|
assert(is_array($serializedRules)); |
120
|
|
|
|
121
|
2 |
|
return $serializedRules; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param array $serializedRules |
126
|
|
|
* |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
2 |
|
public static function getRulesIndexes(array $serializedRules): array |
130
|
|
|
{ |
131
|
2 |
|
assert(array_key_exists(static::RULES_ARRAY_INDEXES, $serializedRules)); |
132
|
2 |
|
$result = $serializedRules[static::RULES_ARRAY_INDEXES]; |
133
|
2 |
|
assert(is_array($result)); |
134
|
|
|
|
135
|
2 |
|
return $result; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param array $serializedRules |
140
|
|
|
* |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
2 |
|
public static function getRulesStartIndexes(array $serializedRules): array |
144
|
|
|
{ |
145
|
2 |
|
assert(array_key_exists(static::RULES_ARRAY_START_INDEXES, $serializedRules)); |
146
|
2 |
|
$result = $serializedRules[static::RULES_ARRAY_START_INDEXES]; |
147
|
2 |
|
assert(is_array($result)); |
148
|
|
|
|
149
|
2 |
|
return $result; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param array $serializedRules |
154
|
|
|
* |
155
|
|
|
* @return array |
156
|
|
|
*/ |
157
|
2 |
|
public static function getRulesEndIndexes(array $serializedRules): array |
158
|
|
|
{ |
159
|
2 |
|
assert(array_key_exists(static::RULES_ARRAY_END_INDEXES, $serializedRules)); |
160
|
2 |
|
$result = $serializedRules[static::RULES_ARRAY_END_INDEXES]; |
161
|
2 |
|
assert(is_array($result)); |
162
|
|
|
|
163
|
2 |
|
return $result; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param int $index |
168
|
|
|
* @param array $blocks |
169
|
|
|
* |
170
|
|
|
* @return bool |
171
|
|
|
*/ |
172
|
2 |
|
public static function isRuleExist(int $index, array $blocks): bool |
173
|
|
|
{ |
174
|
2 |
|
$result = array_key_exists($index, $blocks); |
175
|
|
|
|
176
|
2 |
|
return $result; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param array $serializedRules |
181
|
|
|
* |
182
|
|
|
* @return array |
183
|
|
|
* |
184
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
185
|
|
|
*/ |
186
|
3 |
|
protected static function getBlocks(array $serializedRules): array |
187
|
|
|
{ |
188
|
3 |
|
$blocks = BlockSerializer::unserializeBlocks($serializedRules); |
189
|
|
|
|
190
|
3 |
|
return $blocks; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return BlockSerializerInterface |
195
|
|
|
*/ |
196
|
3 |
|
protected function getSerializer(): BlockSerializerInterface |
197
|
|
|
{ |
198
|
3 |
|
return $this->blockSerializer; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return BlockSerializerInterface |
203
|
|
|
*/ |
204
|
3 |
|
protected function createBlockSerializer(): BlockSerializerInterface |
205
|
|
|
{ |
206
|
3 |
|
return new BlockSerializer(); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param RuleInterface[] $rules |
211
|
|
|
* |
212
|
|
|
* @return array |
213
|
|
|
*/ |
214
|
3 |
|
private function serializeRulesArray(array $rules): array |
215
|
|
|
{ |
216
|
3 |
|
$this->getSerializer()->clearBlocksWithStart()->clearBlocksWithEnd(); |
217
|
|
|
|
218
|
3 |
|
$indexes = []; |
219
|
3 |
|
foreach ($rules as $name => $rule) { |
220
|
3 |
|
assert(is_string($name) === true && empty($name) === false); |
221
|
3 |
|
assert($rule instanceof RuleInterface); |
222
|
|
|
|
223
|
3 |
|
$block = $rule->setName($name)->enableCapture()->toBlock(); |
224
|
3 |
|
$indexes[$name] = $this->getSerializer()->addBlock($block); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return [ |
228
|
3 |
|
static::RULES_ARRAY_INDEXES => $indexes, |
229
|
3 |
|
static::RULES_ARRAY_START_INDEXES => $this->getSerializer()->getBlocksWithStart(), |
230
|
3 |
|
static::RULES_ARRAY_END_INDEXES => $this->getSerializer()->getBlocksWithEnd(), |
231
|
|
|
]; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|