-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetaData.php
More file actions
287 lines (243 loc) · 7.52 KB
/
MetaData.php
File metadata and controls
287 lines (243 loc) · 7.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php declare(strict_types=1);
namespace Igni\Annotation\MetaData;
use Igni\Annotation\Annotation;
use Igni\Annotation\Enum;
use Igni\Annotation\Exception\MetaDataException;
use Igni\Annotation\NoValidate;
use Igni\Annotation\Required;
use Igni\Annotation\Target;
use Igni\Annotation\Context;
use Igni\Annotation\Parser;
use ReflectionClass;
use ReflectionProperty;
final class MetaData
{
public const BUILT_IN = [
Annotation::class => 1,
Target::class => 1,
Required::class => 1,
Enum::class => 1,
NoValidate::class => 1,
];
/**
* @var Parser
*/
private $parser;
/**
* @var Context
*/
private $context;
/**
* @var array
*/
private $validTargets = [Target::TARGET_ALL];
/**
* @var bool
*/
private $validate = true;
/**
* @var bool
*/
private $hasConstructor = false;
/**
* @var bool
*/
private $isAnnotation = true;
/**
* @var string
*/
private $className;
/**
* @var Attribute[]
*/
private $attributes = [];
/**
* @var Attribute|null
*/
private $lastFailedAttribute;
public function __construct(string $class, Parser $parser = null)
{
$this->className = $class;
$this->parser = $parser ?? new Parser();
$reflection = new ReflectionClass($class);
$this->context = Context::fromReflectionClass($reflection);
// Skip collecting built in annotations meta data, its not needed.
if (isset(self::BUILT_IN[$class])) {
return;
}
$this->collect($reflection);
}
public function getClass() : string
{
return $this->className;
}
public function isAnnotation() : bool
{
return $this->isAnnotation;
}
public function hasConstructor() : bool
{
return $this->hasConstructor;
}
public function hasAttribute(string $name) : bool
{
return isset($this->attributes[$name]);
}
public function getAttributes() : array
{
return $this->attributes;
}
public function getAttribute(string $name) : Attribute
{
if (!$this->hasAttribute($name)) {
throw MetaDataException::forUndefinedAttribute($this, $name);
}
return $this->attributes[$name];
}
public function validateTarget(string $target) : bool
{
return in_array(Target::TARGET_ALL, $this->validTargets) || in_array($target, $this->validTargets);
}
public function validateAttributes(array $data) : bool
{
if (!$this->validate) {
return true;
}
$this->lastFailedAttribute = null;
foreach ($this->attributes as $name => $attribute) {
if (!isset($data[$name])) {
if ($attribute->isRequired()) {
$this->lastFailedAttribute = $attribute;
return false;
}
continue;
}
if (!$attribute->validate($data[$name])) {
$this->lastFailedAttribute = $attribute;
return false;
}
}
return true;
}
public function hasFailedAttribute() : bool
{
return null !== $this->lastFailedAttribute;
}
public function getFailedAttribute() : Attribute
{
if (!$this->hasFailedAttribute()) {
throw MetaDataException::forUnresolvableFailedAttribute($this);
}
return $this->lastFailedAttribute;
}
private function collect(ReflectionClass $class) : void
{
$this->className = $class->getName();
$this->hasConstructor = $class->getConstructor() !== null;
$this->collectClassMeta($class);
}
private function collectClassMeta(ReflectionClass $class)
{
$this->isAnnotation = false;
$annotations = $this->parser->parse($class->getDocComment(), $this->context);
foreach ($annotations as $annotation) {
switch (get_class($annotation)) {
case Annotation::class:
$this->isAnnotation = true;
break;
case Target::class:
$valid = false;
foreach ($annotation->value as $target) {
if (in_array($target, Target::TARGETS)) {
$valid = true;
}
}
if (!$valid) {
throw MetaDataException::forInvalidTarget(
$annotation->value,
$this->context
);
}
$this->validTargets = $annotation->value;
break;
case NoValidate::class:
$this->validate = false;
break;
}
}
$properties = $class->getProperties(ReflectionProperty::IS_PUBLIC);
foreach ($properties as $property) {
$this->collectPropertyMeta($property);
}
}
private function collectPropertyMeta(ReflectionProperty $property) : void
{
$propertyContext = Context::fromReflectionProperty($property);
$docComment = $property->getDocComment();
$name = $property->getName();
$type = $this->parseDeclaredType($docComment, $this->context);
$required = false;
$validate = true;
$enum = null;
$annotations = $this->parser->parse($docComment, $propertyContext);
foreach ($annotations as $annotation) {
switch (get_class($annotation)) {
case Enum::class:
$enum = $annotation->value;
break;
case Required::class:
$required = true;
break;
case NoValidate::class:
$validate = false;
break;
}
}
$attribute = new Attribute($name, $type, $required);
if (!$validate) {
$attribute->disableValidation();
}
if ($enum) {
$attribute->enumerate($enum);
}
$this->attributes[$name] = $attribute;
}
private function parseDeclaredType(string $docComment, Context $context)
{
preg_match('/@var\s+([^\*\n\[]+)\s*?(\[\s*?\])?/', $docComment, $matches);
if (!isset($matches[1])) {
return 'mixed';
}
$type = trim($matches[1]);
$isArray = isset($matches[2]);
switch (true) {
// @var annotation contains multiple viable types for the property so it is mixed, we dont care about it
case strstr($type, '|') !== false:
$type = 'mixed';
break;
// Primitive types
case in_array($type, ['float', 'double', 'bool', 'boolean', 'string', 'object', 'mixed']):
if ($isArray) {
$type = [$type];
}
break;
// If class like that exists
case class_exists($type):
if ($isArray) {
$type = [$type];
}
break;
case ($class = $context->resolveClassName($type)) !== null:
$type = $class;
if ($isArray) {
$type = [$class];
}
break;
// Fallback to mixed
default:
$type = 'mixed';
break;
}
return $type;
}
}