1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Respect/Validation. |
5
|
|
|
* |
6
|
|
|
* (c) Alexandre Gomes Gaigalas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the "LICENSE.md" |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Respect\Validation\Rules; |
13
|
|
|
|
14
|
|
|
use Respect\Validation\Exceptions\ComponentException; |
15
|
|
|
use Respect\Validation\Exceptions\KeySetException; |
16
|
|
|
use Respect\Validation\Validatable; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Validates a keys in a defined structure. |
20
|
|
|
* |
21
|
|
|
* @author Henrique Moody <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class KeySet |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @param AllOf $rule |
27
|
|
|
* |
28
|
|
|
* @return Validatable |
29
|
|
|
*/ |
30
|
|
|
private function filterAllOf(AllOf $rule) |
31
|
|
|
{ |
32
|
|
|
$rules = $rule->getRules(); |
|
|
|
|
33
|
|
|
if (count($rules) != 1) { |
34
|
|
|
throw new ComponentException('AllOf rule must have only one Key rule'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return current($rules); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function addRule($rule, $arguments = []) |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
if ($rule instanceof AllOf) { |
46
|
|
|
$rule = $this->filterAllOf($rule); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (!$rule instanceof Key) { |
50
|
|
|
throw new ComponentException('KeySet rule accepts only Key rules'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->appendRule($rule); |
|
|
|
|
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function addRules(array $rules) |
62
|
|
|
{ |
63
|
|
|
foreach ($rules as $rule) { |
64
|
|
|
$this->addRule($rule); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
public function getKeys() |
74
|
|
|
{ |
75
|
|
|
$keys = []; |
76
|
|
|
foreach ($this->getRules() as $keyRule) { |
|
|
|
|
77
|
|
|
$keys[] = $keyRule->reference; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $keys; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param array $input |
85
|
|
|
* |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
|
private function hasValidStructure($input) |
89
|
|
|
{ |
90
|
|
|
if (!is_array($input)) { |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
foreach ($this->getRules() as $keyRule) { |
|
|
|
|
95
|
|
|
if (!array_key_exists($keyRule->reference, $input) && $keyRule->mandatory) { |
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
unset($input[$keyRule->reference]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return count($input) == 0; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @throws KeySetException |
107
|
|
|
*/ |
108
|
|
|
private function checkKeys($input) |
109
|
|
|
{ |
110
|
|
|
if (!$this->hasValidStructure($input)) { |
111
|
|
|
$params = ['keys' => $this->getKeys()]; |
112
|
|
|
$exception = $this->reportError($input, $params); |
|
|
|
|
113
|
|
|
|
114
|
|
|
throw $exception; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
|
|
public function assert($input) |
122
|
|
|
{ |
123
|
|
|
$this->checkKeys($input); |
124
|
|
|
|
125
|
|
|
return parent::assert($input); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
|
|
public function check($input) |
132
|
|
|
{ |
133
|
|
|
$this->checkKeys($input); |
134
|
|
|
|
135
|
|
|
return parent::check($input); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritdoc} |
140
|
|
|
*/ |
141
|
|
|
public function validate($input) |
142
|
|
|
{ |
143
|
|
|
if (!$this->hasValidStructure($input)) { |
144
|
|
|
return false; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return parent::validate($input); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.