1
|
|
|
<?php namespace Limoncello\Flute\Validation\Serialize; |
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 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
|
|
|
/** |
51
|
|
|
* @var BlockSerializerInterface |
52
|
|
|
*/ |
53
|
|
|
private $blockSerializer; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param RuleInterface $rule |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
40 |
|
public function addRule(RuleInterface $rule): array |
61
|
|
|
{ |
62
|
40 |
|
$this->getSerializer()->clearBlocksWithStart()->clearBlocksWithEnd(); |
63
|
|
|
|
64
|
|
|
$result = [ |
65
|
40 |
|
static::RULES_SINGLE_INDEX => $this->getSerializer()->addBlock($rule->toBlock()), |
66
|
40 |
|
static::RULES_SINGLE_START_INDEXES => $this->getSerializer()->getBlocksWithStart(), |
67
|
40 |
|
static::RULES_SINGLE_END_INDEXES => $this->getSerializer()->getBlocksWithEnd(), |
68
|
|
|
]; |
69
|
|
|
|
70
|
40 |
|
return $result; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param BlockSerializerInterface $blockSerializer |
75
|
|
|
*/ |
76
|
57 |
|
public function __construct(BlockSerializerInterface $blockSerializer) |
77
|
|
|
{ |
78
|
57 |
|
$this->blockSerializer = $blockSerializer; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param RuleInterface[] $rules |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
57 |
|
public function addRules(array $rules): array |
87
|
|
|
{ |
88
|
57 |
|
$this->getSerializer()->clearBlocksWithStart()->clearBlocksWithEnd(); |
89
|
|
|
|
90
|
57 |
|
$indexes = []; |
91
|
57 |
|
foreach ($rules as $name => $rule) { |
92
|
53 |
|
assert($rule instanceof RuleInterface); |
93
|
|
|
|
94
|
53 |
|
$ruleName = $rule->getName(); |
95
|
53 |
|
if (empty($ruleName) === true) { |
96
|
53 |
|
$ruleName = $name; |
97
|
|
|
} |
98
|
|
|
|
99
|
53 |
|
$block = $rule->setName($ruleName)->enableCapture()->toBlock(); |
100
|
53 |
|
$indexes[$name] = $this->getSerializer()->addBlock($block); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$ruleIndexes = [ |
104
|
57 |
|
static::RULES_ARRAY_INDEXES => $indexes, |
105
|
57 |
|
static::RULES_ARRAY_START_INDEXES => $this->getSerializer()->getBlocksWithStart(), |
106
|
57 |
|
static::RULES_ARRAY_END_INDEXES => $this->getSerializer()->getBlocksWithEnd(), |
107
|
|
|
]; |
108
|
|
|
|
109
|
57 |
|
$this->getSerializer()->clearBlocksWithStart()->clearBlocksWithEnd(); |
110
|
|
|
|
111
|
57 |
|
return $ruleIndexes; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return array |
116
|
|
|
* |
117
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
118
|
|
|
*/ |
119
|
57 |
|
public function getBlocks(): array |
120
|
|
|
{ |
121
|
57 |
|
$blocks = BlockSerializer::unserializeBlocks($this->getSerializer()->get()); |
122
|
|
|
|
123
|
57 |
|
return $blocks; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param array $singleRuleIndexes |
128
|
|
|
* |
129
|
|
|
* @return int |
130
|
|
|
*/ |
131
|
14 |
|
public static function getRuleIndex(array $singleRuleIndexes): int |
132
|
|
|
{ |
133
|
14 |
|
assert(array_key_exists(static::RULES_SINGLE_INDEX, $singleRuleIndexes)); |
134
|
14 |
|
$result = $singleRuleIndexes[static::RULES_SINGLE_INDEX]; |
135
|
|
|
|
136
|
14 |
|
return $result; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param array $singleRuleIndexes |
141
|
|
|
* |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
15 |
|
public static function getRuleStartIndexes(array $singleRuleIndexes): array |
145
|
|
|
{ |
146
|
15 |
|
assert(array_key_exists(static::RULES_SINGLE_START_INDEXES, $singleRuleIndexes)); |
147
|
15 |
|
$result = $singleRuleIndexes[static::RULES_SINGLE_START_INDEXES]; |
148
|
|
|
|
149
|
15 |
|
return $result; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param array $singleRuleIndexes |
154
|
|
|
* |
155
|
|
|
* @return array |
156
|
|
|
*/ |
157
|
15 |
|
public static function getRuleEndIndexes(array $singleRuleIndexes): array |
158
|
|
|
{ |
159
|
15 |
|
assert(array_key_exists(static::RULES_SINGLE_END_INDEXES, $singleRuleIndexes)); |
160
|
15 |
|
$result = $singleRuleIndexes[static::RULES_SINGLE_END_INDEXES]; |
161
|
|
|
|
162
|
15 |
|
return $result; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param array $arrayRulesIndexes |
167
|
|
|
* |
168
|
|
|
* @return array |
169
|
|
|
*/ |
170
|
46 |
|
public static function getRulesIndexes(array $arrayRulesIndexes): array |
171
|
|
|
{ |
172
|
46 |
|
assert(array_key_exists(static::RULES_ARRAY_INDEXES, $arrayRulesIndexes)); |
173
|
46 |
|
$result = $arrayRulesIndexes[static::RULES_ARRAY_INDEXES]; |
174
|
|
|
|
175
|
46 |
|
return $result; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param array $arrayRulesIndexes |
180
|
|
|
* |
181
|
|
|
* @return array |
182
|
|
|
*/ |
183
|
54 |
|
public static function getRulesStartIndexes(array $arrayRulesIndexes): array |
184
|
|
|
{ |
185
|
54 |
|
assert(array_key_exists(static::RULES_ARRAY_START_INDEXES, $arrayRulesIndexes)); |
186
|
54 |
|
$result = $arrayRulesIndexes[static::RULES_ARRAY_START_INDEXES]; |
187
|
|
|
|
188
|
54 |
|
return $result; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param array $arrayRulesIndexes |
193
|
|
|
* |
194
|
|
|
* @return array |
195
|
|
|
*/ |
196
|
54 |
|
public static function getRulesEndIndexes(array $arrayRulesIndexes): array |
197
|
|
|
{ |
198
|
54 |
|
assert(array_key_exists(static::RULES_ARRAY_END_INDEXES, $arrayRulesIndexes)); |
199
|
54 |
|
$result = $arrayRulesIndexes[static::RULES_ARRAY_END_INDEXES]; |
200
|
|
|
|
201
|
54 |
|
return $result; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @return BlockSerializerInterface |
206
|
|
|
*/ |
207
|
57 |
|
protected function getSerializer(): BlockSerializerInterface |
208
|
|
|
{ |
209
|
57 |
|
return $this->blockSerializer; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|