1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dgame\Object; |
4
|
|
|
|
5
|
|
|
use Dgame\Type\Type; |
6
|
|
|
use ReflectionMethod; |
7
|
|
|
use ReflectionParameter; |
8
|
|
|
use ReflectionProperty; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Validator |
12
|
|
|
* @package Dgame\Object |
13
|
|
|
*/ |
14
|
|
|
final class Validator |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var ObjectFacade |
18
|
|
|
*/ |
19
|
|
|
private $facade; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Validator constructor. |
23
|
|
|
* |
24
|
|
|
* @param ObjectFacade $facade |
25
|
|
|
*/ |
26
|
|
|
public function __construct(ObjectFacade $facade) |
27
|
|
|
{ |
28
|
|
|
$this->facade = $facade; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param ObjectFacade $facade |
33
|
|
|
* |
34
|
|
|
* @return Validator |
35
|
|
|
*/ |
36
|
|
|
public static function new(ObjectFacade $facade): self |
37
|
|
|
{ |
38
|
|
|
return new self($facade); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param ReflectionProperty $property |
43
|
|
|
* |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
|
|
public function isValidProperty(ReflectionProperty $property): bool |
47
|
|
|
{ |
48
|
|
|
return $property->isPublic() && !$property->isStatic(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param ReflectionMethod $method |
53
|
|
|
* |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
public function isValidMethod(ReflectionMethod $method): bool |
57
|
|
|
{ |
58
|
|
|
return $method->isPublic() && !$method->isStatic(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param ReflectionMethod $method |
63
|
|
|
* @param $value |
64
|
|
|
* |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public function isValidSetterMethod(ReflectionMethod $method, $value): bool |
68
|
|
|
{ |
69
|
|
|
if (!$this->isValidMethod($method)) { |
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($method->getNumberOfParameters() === 0) { |
74
|
|
|
return true; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($method->getNumberOfRequiredParameters() > 1) { |
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($value === null) { |
82
|
|
|
return $method->getParameters()[0]->allowsNull(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->isValidParameterValue($method->getParameters()[0], $value); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param ReflectionParameter $parameter |
90
|
|
|
* @param $value |
91
|
|
|
* |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
public function isValidParameterValue(ReflectionParameter $parameter, $value): bool |
95
|
|
|
{ |
96
|
|
|
return !$parameter->hasType() || Type::from($parameter)->accept($value); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param ReflectionMethod $method |
101
|
|
|
* |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
|
|
public function isValidGetterMethod(ReflectionMethod $method): bool |
105
|
|
|
{ |
106
|
|
|
if (!$this->isValidMethod($method)) { |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$value = $method->invoke($this->facade->getObject()); |
111
|
|
|
|
112
|
|
|
return $value !== null || !$method->hasReturnType() || $method->getReturnType()->allowsNull(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param ReflectionMethod $method |
117
|
|
|
* @param array ...$args |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public function areValidMethodArguments(ReflectionMethod $method, ...$args): bool |
122
|
|
|
{ |
123
|
|
|
if (!$this->isValidMethod($method)) { |
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if (count($args) < $method->getNumberOfRequiredParameters()) { |
128
|
|
|
return false; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $this->validateMethodArguments($method, ...$args); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param ReflectionMethod $method |
136
|
|
|
* @param array ...$args |
137
|
|
|
* |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
|
|
private function validateMethodArguments(ReflectionMethod $method, ...$args): bool |
141
|
|
|
{ |
142
|
|
|
$parameters = $method->getParameters(); |
143
|
|
|
foreach ($args as $i => $arg) { |
144
|
|
|
if (!array_key_exists($i, $parameters)) { |
145
|
|
|
break; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if ($arg === null && !$parameters[$i]->allowsNull()) { |
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if (!$this->isValidParameterValue($parameters[$i], $arg)) { |
153
|
|
|
return false; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return true; |
158
|
|
|
} |
159
|
|
|
} |