Completed
Push — master ( 2df407...de4379 )
by Neomerx
05:05
created

ArrayValidation::setSerializedRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php namespace Limoncello\Validation\Validator;
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\Captures\CaptureAggregatorInterface;
20
use Limoncello\Validation\Contracts\Errors\ErrorAggregatorInterface;
21
use Limoncello\Validation\Contracts\Rules\RuleInterface;
22
use Limoncello\Validation\Execution\BlockInterpreter;
23
use Limoncello\Validation\Execution\BlockSerializer;
24
25
/**
26
 * @package Limoncello\Validation
27
 *
28
 * The trait expects the following method to be implemented by a class that uses this trait.
29
 * - createContextStorageFromBlocks(array $blocks): ContextStorageInterface
30
 */
31
trait ArrayValidation
32
{
33
    /**
34
     * @var array
35
     */
36
    private $serializedRules = [];
37
38
    /**
39
     * @return array
40
     */
41 11
    public function getSerializedRules(): array
42
    {
43 11
        return $this->serializedRules;
44
    }
45
46
    /**
47
     * @param array $serialized
48
     *
49
     * @return self
50
     */
51 11
    public function setSerializedRules(array $serialized): self
52
    {
53 11
        $this->serializedRules = $serialized;
54
55 11
        return $this;
56
    }
57
58
    /**
59
     * @param RuleInterface[] $rules
60
     *
61
     * @return self
62
     */
63 11
    private function setRules(array $rules): self
64
    {
65 11
        return $this->setSerializedRules($this->serializeRules($rules));
66
    }
67
68
    /**
69
     * @param array                      $input
70
     * @param CaptureAggregatorInterface $captures
71
     * @param ErrorAggregatorInterface   $errors
72
     *
73
     * @return void
74
     *
75
     * @SuppressWarnings(PHPMD.StaticAccess)
76
     */
77 11
    private function validateArrayImplementation(
78
        array $input,
79
        CaptureAggregatorInterface $captures,
80
        ErrorAggregatorInterface $errors
81
    ): void {
82 11
        list($indexMap, $serialized) = $this->getSerializedRules();
83
84 11
        $blocks = BlockSerializer::unserializeBlocks($serialized);
85
86
        // the method is expected to be implemented by a class that uses this trait
87 11
        $context = $this->createContextStorageFromBlocks($blocks);
0 ignored issues
show
Bug introduced by
It seems like createContextStorageFromBlocks() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
88
89 11
        BlockInterpreter::executeStarts(
90 11
            BlockSerializer::unserializeBlocksWithStart($serialized),
91 11
            $blocks,
92 11
            $context,
93 11
            $errors
94
        );
95 11
        foreach ($input as $key => $value) {
96 11
            $blockIndex = $indexMap[$key];
97 11
            BlockInterpreter::executeBlock($value, $blockIndex, $blocks, $context, $captures, $errors);
98
        }
99 11
        BlockInterpreter::executeEnds(
100 11
            BlockSerializer::unserializeBlocksWithEnd($serialized),
101 11
            $blocks,
102 11
            $context,
103 11
            $errors
104
        );
105
    }
106
107
    /**
108
     * @var RuleInterface[] $rules
109
     *
110
     * @return array
111
     */
112 11
    private function serializeRules(array $rules): array
113
    {
114 11
        $serializer = new BlockSerializer();
115
116 11
        $indexMap = [];
117 11
        foreach ($rules as $name => $rule) {
118 11
            $indexMap[$name] = $serializer->addBlock($rule->setName($name)->enableCapture()->toBlock());
119
        }
120
121 11
        return [$indexMap, $serializer->get()];
122
    }
123
}
124