1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Validation\Validator; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2020 [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\Captures\CaptureAggregatorInterface; |
22
|
|
|
use Limoncello\Validation\Contracts\Errors\ErrorAggregatorInterface; |
23
|
|
|
use Limoncello\Validation\Contracts\Rules\RuleInterface; |
24
|
|
|
use Limoncello\Validation\Execution\BlockInterpreter; |
25
|
|
|
use Limoncello\Validation\Execution\BlockSerializer; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @package Limoncello\Validation |
29
|
|
|
* |
30
|
|
|
* The trait expects the following method to be implemented by a class that uses this trait. |
31
|
|
|
* - createContextStorageFromBlocks(array $blocks): ContextStorageInterface |
32
|
|
|
*/ |
33
|
|
|
trait ArrayValidation |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $serializedRules = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
12 |
|
public function getSerializedRules(): array |
44
|
|
|
{ |
45
|
12 |
|
return $this->serializedRules; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param array $serialized |
50
|
|
|
* |
51
|
|
|
* @return self |
52
|
|
|
*/ |
53
|
12 |
|
public function setSerializedRules(array $serialized): self |
54
|
|
|
{ |
55
|
12 |
|
$this->serializedRules = $serialized; |
56
|
|
|
|
57
|
12 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param RuleInterface[]|iterable $rules |
62
|
|
|
* |
63
|
|
|
* @return self |
64
|
|
|
*/ |
65
|
12 |
|
private function setRules(iterable $rules): self |
66
|
|
|
{ |
67
|
12 |
|
return $this->setSerializedRules($this->serializeRules($rules)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param array $input |
72
|
|
|
* @param CaptureAggregatorInterface $captures |
73
|
|
|
* @param ErrorAggregatorInterface $errors |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
* |
77
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
78
|
|
|
*/ |
79
|
12 |
|
private function validateArrayImplementation( |
80
|
|
|
array $input, |
81
|
|
|
CaptureAggregatorInterface $captures, |
82
|
|
|
ErrorAggregatorInterface $errors |
83
|
|
|
): void { |
84
|
12 |
|
list($indexMap, $serialized) = $this->getSerializedRules(); |
85
|
|
|
|
86
|
12 |
|
$blocks = BlockSerializer::unserializeBlocks($serialized); |
87
|
|
|
|
88
|
|
|
// the method is expected to be implemented by a class that uses this trait |
89
|
12 |
|
$context = $this->createContextStorageFromBlocks($blocks); |
|
|
|
|
90
|
|
|
|
91
|
12 |
|
BlockInterpreter::executeStarts( |
92
|
12 |
|
BlockSerializer::unserializeBlocksWithStart($serialized), |
93
|
12 |
|
$blocks, |
94
|
12 |
|
$context, |
95
|
12 |
|
$errors |
96
|
|
|
); |
97
|
12 |
|
foreach ($input as $key => $value) { |
98
|
12 |
|
$blockIndex = $indexMap[$key]; |
99
|
12 |
|
BlockInterpreter::executeBlock($value, $blockIndex, $blocks, $context, $captures, $errors); |
100
|
|
|
} |
101
|
12 |
|
BlockInterpreter::executeEnds( |
102
|
12 |
|
BlockSerializer::unserializeBlocksWithEnd($serialized), |
103
|
12 |
|
$blocks, |
104
|
12 |
|
$context, |
105
|
12 |
|
$errors |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @var RuleInterface[]|iterable $rules |
111
|
|
|
* |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
12 |
|
private function serializeRules(iterable $rules): array |
115
|
|
|
{ |
116
|
12 |
|
$serializer = new BlockSerializer(); |
117
|
|
|
|
118
|
12 |
|
$indexMap = []; |
119
|
12 |
|
foreach ($rules as $name => $rule) { |
120
|
12 |
|
$indexMap[$name] = $serializer->addBlock($rule->setName($name)->enableCapture()->toBlock()); |
121
|
|
|
} |
122
|
|
|
|
123
|
12 |
|
return [$indexMap, $serializer->get()]; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
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
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. 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.