1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dgame\Annotation; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Annotation |
9
|
|
|
* @package Dgame\Annotation |
10
|
|
|
*/ |
11
|
|
|
final class Annotation |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var VariableAnnotation[] |
15
|
|
|
*/ |
16
|
|
|
private $properties = []; |
17
|
|
|
/** |
18
|
|
|
* @var VariableAnnotation[][] |
19
|
|
|
*/ |
20
|
|
|
private $parameters = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Annotation constructor. |
24
|
|
|
* |
25
|
|
|
* @param ReflectionClass $class |
26
|
|
|
*/ |
27
|
|
|
public function __construct(ReflectionClass $class) |
28
|
|
|
{ |
29
|
|
|
$parser = new AnnotationParser(); |
30
|
|
|
|
31
|
|
|
foreach ($parser->parsePropertyAnnotations($class->getDocComment()) as $annotation) { |
32
|
|
|
$this->properties[$annotation->getName()] = $annotation; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
foreach ($class->getProperties() as $property) { |
36
|
|
|
$annotations = $parser->parseVariableAnnotations($property->getDocComment()); |
37
|
|
|
$this->properties[$property->name] = array_pop($annotations); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
foreach ($class->getMethods() as $method) { |
41
|
|
|
$name = $method->getName(); |
42
|
|
|
|
43
|
|
|
$this->parameters[$name] = []; |
44
|
|
|
foreach ($parser->parseParameterAnnotations($method->getDocComment()) as $annotation) { |
45
|
|
|
$this->parameters[$name][$annotation->getName()] = $annotation; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return VariableAnnotation[] |
52
|
|
|
*/ |
53
|
|
|
public function getAllProperties(): array |
54
|
|
|
{ |
55
|
|
|
return $this->properties; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $property |
60
|
|
|
* |
61
|
|
|
* @return VariableAnnotation |
62
|
|
|
*/ |
63
|
|
|
public function getProperty(string $property): VariableAnnotation |
64
|
|
|
{ |
65
|
|
|
return $this->properties[$property]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $property |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public function hasProperty(string $property): bool |
74
|
|
|
{ |
75
|
|
|
return array_key_exists($property, $this->properties); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return VariableAnnotation[] |
80
|
|
|
*/ |
81
|
|
|
public function getAllMethodsParameters(): array |
82
|
|
|
{ |
83
|
|
|
return $this->parameters; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $method |
88
|
|
|
* |
89
|
|
|
* @return VariableAnnotation[] |
90
|
|
|
*/ |
91
|
|
|
public function getMethodParameters(string $method): array |
92
|
|
|
{ |
93
|
|
|
return $this->parameters[$method]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $method |
98
|
|
|
* @param string $param |
99
|
|
|
* |
100
|
|
|
* @return VariableAnnotation |
101
|
|
|
*/ |
102
|
|
|
public function getMethodParameter(string $method, string $param): VariableAnnotation |
103
|
|
|
{ |
104
|
|
|
return $this->parameters[$method][$param]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $method |
109
|
|
|
* @param string|null $param |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
public function hasMethodParameter(string $method, string $param = null): bool |
114
|
|
|
{ |
115
|
|
|
return array_key_exists($method, $this->parameters) && ($param === null ? true : array_key_exists($param, $this->parameters[$method])); |
116
|
|
|
} |
117
|
|
|
} |