Completed
Push — master ( 1a67d2...dfab50 )
by Neomerx
11:35 queued 22s
created

RulesSerializer   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 186
rs 10
c 0
b 0
f 0
ccs 52
cts 52
cp 1

11 Methods

Rating   Name   Duplication   Size   Complexity  
A addRule() 0 12 1
A __construct() 0 4 1
B addRules() 0 27 3
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 getSerializer() 0 4 1
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