1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of php-simple-request. |
4
|
|
|
* |
5
|
|
|
* php-simple-request is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* php-simple-request is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with php-simple-request. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
namespace Mcustiel\SimpleRequest\Validator; |
19
|
|
|
|
20
|
|
|
use Mcustiel\SimpleRequest\Interfaces\ValidatorInterface; |
21
|
|
|
use Mcustiel\SimpleRequest\Exception\UnspecifiedValidatorException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Checks if the specified list of properties names are keys in a given array |
25
|
|
|
* or properties in a given object. |
26
|
|
|
* |
27
|
|
|
* @author mcustiel |
28
|
|
|
*/ |
29
|
|
|
class Required implements ValidatorInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* |
33
|
|
|
* @var number[]|string[] |
34
|
|
|
*/ |
35
|
|
|
protected $items; |
36
|
|
|
|
37
|
82 |
|
public function setSpecification($specification = null) |
38
|
|
|
{ |
39
|
82 |
|
$this->specificationIsArrayOrThrowException($specification); |
40
|
81 |
|
foreach ($specification as $item) { |
41
|
81 |
|
$this->specificationItemisValidOrThrowException($item); |
42
|
79 |
|
} |
43
|
|
|
|
44
|
79 |
|
$this->items = $specification; |
|
|
|
|
45
|
79 |
|
} |
46
|
|
|
|
47
|
81 |
|
private function specificationItemisValidOrThrowException($item) |
48
|
|
|
{ |
49
|
81 |
|
if (!is_string($item) || is_numeric($item)) { |
50
|
2 |
|
throw new UnspecifiedValidatorException( |
51
|
|
|
'The validator Required is being initialized without a valid array' |
52
|
2 |
|
); |
53
|
|
|
} |
54
|
79 |
|
} |
55
|
|
|
|
56
|
82 |
|
private function specificationIsArrayOrThrowException($specification) |
57
|
|
|
{ |
58
|
82 |
|
if (!is_array($specification)) { |
59
|
1 |
|
throw new UnspecifiedValidatorException( |
60
|
|
|
'The validator Required is being initialized without an array' |
61
|
1 |
|
); |
62
|
|
|
} |
63
|
81 |
|
} |
64
|
|
|
|
65
|
80 |
|
public function validate($value) |
66
|
|
|
{ |
67
|
80 |
|
if (is_array($value)) { |
68
|
3 |
|
return $this->validateArray($value); |
69
|
|
|
} |
70
|
77 |
|
if ($value instanceof \stdClass) { |
71
|
77 |
|
return $this->validateObject($value); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
77 |
|
private function validateObject(\stdClass $object) |
78
|
|
|
{ |
79
|
77 |
|
foreach ($this->items as $item) { |
80
|
77 |
|
if (!property_exists($object, $item)) { |
81
|
1 |
|
return false; |
82
|
|
|
} |
83
|
77 |
|
} |
84
|
|
|
|
85
|
76 |
|
return true; |
86
|
|
|
} |
87
|
|
|
|
88
|
3 |
|
private function validateArray(array $array) |
89
|
|
|
{ |
90
|
3 |
|
foreach ($this->items as $item) { |
91
|
3 |
|
if (!array_key_exists($item, $array)) { |
92
|
1 |
|
return false; |
93
|
|
|
} |
94
|
3 |
|
} |
95
|
|
|
|
96
|
2 |
|
return true; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..