|
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); |
|
|
|
|
|
|
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
|
|
|
|
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
Idableprovides a methodequalsIdthat 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.