|
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 SingleValidation |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var RuleInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $rule; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return RuleInterface |
|
40
|
|
|
*/ |
|
41
|
4 |
|
protected function getRule(): RuleInterface |
|
42
|
|
|
{ |
|
43
|
4 |
|
return $this->rule; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param RuleInterface $rule |
|
48
|
|
|
* |
|
49
|
|
|
* @return self |
|
50
|
|
|
*/ |
|
51
|
4 |
|
private function setRule(RuleInterface $rule): self |
|
52
|
|
|
{ |
|
53
|
4 |
|
$this->rule = $rule; |
|
54
|
|
|
|
|
55
|
4 |
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param mixed $input |
|
60
|
|
|
* @param CaptureAggregatorInterface $captures |
|
61
|
|
|
* @param ErrorAggregatorInterface $errors |
|
62
|
|
|
* |
|
63
|
|
|
* @return void |
|
64
|
|
|
* |
|
65
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
66
|
|
|
*/ |
|
67
|
4 |
|
private function validateSingleImplementation( |
|
68
|
|
|
$input, |
|
69
|
|
|
CaptureAggregatorInterface $captures, |
|
70
|
|
|
ErrorAggregatorInterface $errors |
|
71
|
|
|
): void { |
|
72
|
4 |
|
$serialized = (new BlockSerializer())->serialize($this->getRule()->toBlock())->get(); |
|
73
|
4 |
|
$blocks = BlockSerializer::unserializeBlocks($serialized); |
|
74
|
|
|
|
|
75
|
|
|
// the method is expected to be implemented by a class that uses this trait |
|
76
|
4 |
|
$context = $this->createContextStorageFromBlocks($blocks); |
|
|
|
|
|
|
77
|
|
|
|
|
78
|
4 |
|
BlockInterpreter::execute($input, $serialized, $context, $captures, $errors); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
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.