Completed
Push — master ( 1776d4...322507 )
by Neomerx
02:47
created

FormRuleSerializer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
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 1
    public function __construct()
59
    {
60 1
        $this->blockSerializer = $this->createBlockSerializer();
61 1
        $this->ruleSets        = [];
62
    }
63
64
    /**
65
     * @param string          $name
66
     * @param RuleInterface[] $attributeRules
67
     *
68
     * @return self
69
     */
70 1
    public function addResourceRules(string $name, array $attributeRules): self
71
    {
72 1
        assert(!empty($name), 'Rule set name cannot be empty.');
73 1
        assert(!array_key_exists($name, $this->ruleSets), "A rule set with name `$name` has been added already.");
74
75 1
        $this->ruleSets[$name] = $this->serializeRulesArray($attributeRules);
76
77 1
        $this->getSerializer()->clearBlocksWithStart()->clearBlocksWithEnd();
78
79 1
        return $this;
80
    }
81
82
    /**
83
     * @return array
84
     *
85
     * @SuppressWarnings(PHPMD.StaticAccess)
86
     */
87 1
    public function getData(): array
88
    {
89
        return [
90 1
            static::SERIALIZED_RULE_SETS => $this->ruleSets,
91 1
            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
    public static function getAttributeRules(string $name, array $data): array
102
    {
103
        assert($data[static::SERIALIZED_RULE_SETS] ?? false);
104
        $indexes = $data[static::SERIALIZED_RULE_SETS][$name];
105
        assert(is_array($indexes));
106
107
        return $indexes;
108
    }
109
110
    /**
111
     * @param array $data
112
     *
113
     * @return array
114
     */
115
    public static function extractBlocks(array $data): array
116
    {
117
        assert(array_key_exists(static::SERIALIZED_BLOCKS, $data));
118
        $serializedRules = $data[static::SERIALIZED_BLOCKS];
119
        assert(is_array($serializedRules));
120
121
        return $serializedRules;
122
    }
123
124
    /**
125
     * @param array $serializedRules
126
     *
127
     * @return array
128
     */
129
    public static function getRulesIndexes(array $serializedRules): array
130
    {
131
        assert(array_key_exists(static::RULES_ARRAY_INDEXES, $serializedRules));
132
        $result = $serializedRules[static::RULES_ARRAY_INDEXES];
133
        assert(is_array($result));
134
135
        return $result;
136
    }
137
138
    /**
139
     * @param array $serializedRules
140
     *
141
     * @return array
142
     */
143
    public static function getRulesStartIndexes(array $serializedRules): array
144
    {
145
        assert(array_key_exists(static::RULES_ARRAY_START_INDEXES, $serializedRules));
146
        $result = $serializedRules[static::RULES_ARRAY_START_INDEXES];
147
        assert(is_array($result));
148
149
        return $result;
150
    }
151
152
    /**
153
     * @param array $serializedRules
154
     *
155
     * @return array
156
     */
157
    public static function getRulesEndIndexes(array $serializedRules): array
158
    {
159
        assert(array_key_exists(static::RULES_ARRAY_END_INDEXES, $serializedRules));
160
        $result = $serializedRules[static::RULES_ARRAY_END_INDEXES];
161
        assert(is_array($result));
162
163
        return $result;
164
    }
165
166
    /**
167
     * @param int   $index
168
     * @param array $blocks
169
     *
170
     * @return bool
171
     */
172
    public static function isRuleExist(int $index, array $blocks): bool
173
    {
174
        $result = array_key_exists($index, $blocks);
175
176
        return $result;
177
    }
178
179
    /**
180
     * @param array $serializedRules
181
     *
182
     * @return array
183
     *
184
     * @SuppressWarnings(PHPMD.StaticAccess)
185
     */
186 1
    protected static function getBlocks(array $serializedRules): array
187
    {
188 1
        $blocks = BlockSerializer::unserializeBlocks($serializedRules);
189
190 1
        return $blocks;
191
    }
192
193
    /**
194
     * @return BlockSerializerInterface
195
     */
196 1
    protected function getSerializer(): BlockSerializerInterface
197
    {
198 1
        return $this->blockSerializer;
199
    }
200
201
    /**
202
     * @return BlockSerializerInterface
203
     */
204 1
    protected function createBlockSerializer(): BlockSerializerInterface
205
    {
206 1
        return new BlockSerializer();
207
    }
208
209
    /**
210
     * @param RuleInterface[] $rules
211
     *
212
     * @return array
213
     */
214 1
    private function serializeRulesArray(array $rules): array
215
    {
216 1
        $this->getSerializer()->clearBlocksWithStart()->clearBlocksWithEnd();
217
218 1
        $indexes = [];
219 1
        foreach ($rules as $name => $rule) {
220 1
            assert(is_string($name) === true && empty($name) === false);
221 1
            assert($rule instanceof RuleInterface);
222
223 1
            $block          = $rule->setName($name)->enableCapture()->toBlock();
224 1
            $indexes[$name] = $this->getSerializer()->addBlock($block);
225
        }
226
227
        return [
228 1
            static::RULES_ARRAY_INDEXES       => $indexes,
229 1
            static::RULES_ARRAY_START_INDEXES => $this->getSerializer()->getBlocksWithStart(),
230 1
            static::RULES_ARRAY_END_INDEXES   => $this->getSerializer()->getBlocksWithEnd(),
231
        ];
232
    }
233
}
234