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 RuleInterface[] |
35
|
|
|
*/ |
36
|
|
|
private $rules; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return RuleInterface[] |
40
|
|
|
*/ |
41
|
|
|
protected function getRules(): array |
42
|
|
|
{ |
43
|
|
|
return $this->rules; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param RuleInterface[] $rules |
48
|
|
|
* |
49
|
|
|
* @return self |
50
|
|
|
*/ |
51
|
|
|
private function setRules(array $rules): self |
52
|
|
|
{ |
53
|
|
|
$this->rules = $rules; |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param array $input |
60
|
|
|
* @param CaptureAggregatorInterface $captures |
61
|
|
|
* @param ErrorAggregatorInterface $errors |
62
|
|
|
* |
63
|
|
|
* @return void |
64
|
|
|
* |
65
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
66
|
|
|
*/ |
67
|
|
|
private function validateArrayImplementation( |
68
|
|
|
array $input, |
69
|
|
|
CaptureAggregatorInterface $captures, |
70
|
|
|
ErrorAggregatorInterface $errors |
71
|
|
|
): void { |
72
|
|
|
list($indexMap, $serialized) = $this->getSerializedRules($this->getRules()); |
73
|
|
|
|
74
|
|
|
$blocks = BlockSerializer::unserializeBlocks($serialized); |
75
|
|
|
|
76
|
|
|
// the method is expected to be implemented by a class that uses this trait |
77
|
|
|
$context = $this->createContextStorageFromBlocks($blocks); |
|
|
|
|
78
|
|
|
|
79
|
|
|
BlockInterpreter::executeStarts( |
80
|
|
|
BlockSerializer::unserializeBlocksWithStart($serialized), |
81
|
|
|
$blocks, |
82
|
|
|
$context, |
83
|
|
|
$errors |
84
|
|
|
); |
85
|
|
|
foreach ($input as $key => $value) { |
86
|
|
|
$blockIndex = $indexMap[$key]; |
87
|
|
|
BlockInterpreter::executeBlock($value, $blockIndex, $blocks, $context, $captures, $errors); |
88
|
|
|
} |
89
|
|
|
BlockInterpreter::executeEnds( |
90
|
|
|
BlockSerializer::unserializeBlocksWithEnd($serialized), |
91
|
|
|
$blocks, |
92
|
|
|
$context, |
93
|
|
|
$errors |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param RuleInterface[] $rules |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
private function getSerializedRules(array $rules): array |
103
|
|
|
{ |
104
|
|
|
$serializer = new BlockSerializer(); |
105
|
|
|
|
106
|
|
|
$indexMap = []; |
107
|
|
|
foreach ($rules as $name => $rule) { |
108
|
|
|
$indexMap[$name] = $serializer->addBlock($rule->setName($name)->enableCapture()->toBlock()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$serialized = $serializer->get(); |
112
|
|
|
|
113
|
|
|
return [$indexMap, $serialized]; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
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.