RulesSerializer   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 3
dl 0
loc 216
ccs 62
cts 62
cp 1
rs 10
c 0
b 0
f 0

12 Methods

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