-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionAnnotation.php
More file actions
163 lines (146 loc) · 4.28 KB
/
ReflectionAnnotation.php
File metadata and controls
163 lines (146 loc) · 4.28 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
<?php
namespace TuxBoy;
use Doctrine\Common\Annotations\AnnotationReader;
use Exception;
use ReflectionClass;
/**
* ReflectionAnnotation.
*/
class ReflectionAnnotation
{
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $value;
/**
* @var string
*/
private $docComment;
/**
* @var ReflectionClass
*/
private $argument;
/**
* @var null|string
*/
private $propertyName;
/**
* ReflectionAnnotation constructor.
*
* @param string|object $argument Le nom de la classe ou l'object que l'on souhaite récupérer les annotations
* @param string|null $propertyName
*/
public function __construct($argument, string $propertyName = null)
{
$this->argument = new ReflectionClass($argument);
$this->propertyName = $propertyName;
// Si le property_name est null, alors on souhaite obtenir les annotations de la classe
$this->docComment = null === $this->propertyName
? $this->argument->getDocComment()
: $this->argument->getProperty($this->propertyName)->getDocComment();
}
/**
* Récupère l'annotation de la proriété demandé.
*
* @param string $annotationName
*
* @throws Exception
*
* @return ReflectionAnnotation
*/
public function getAnnotation(string $annotationName): self
{
list($this->name, $this->value) = $this->parseDocComment($annotationName);
return $this;
}
/**
* Vérifie si l'annotation passé en paramètre existe.
*
* @param string $annotationName
*
* @return bool
*/
public function hasAnnotation(string $annotationName): bool
{
return in_array($annotationName, $this->parseDocComment($annotationName), true);
}
/**
* Parse la phpdoc pour y extraire le nom de l'annotation de la proriété en question et sa
* valeur (facultatif).
*
* @param string $annotationName Le nom de l'annotation à parser
*
* @throws Exception
*
* @return array|null
*/
private function parseDocComment(string $annotationName): ?array
{
$docComment = $this->docComment;
$commentsDocs = array_filter(explode('*', $docComment), function ($annotation) {
return !empty(trim($annotation));
});
$annotations = array_filter(array_map(function ($annotation) {
return trim(trim($annotation, '/'));
}, $commentsDocs), function ($item) {
return preg_match('#@.+#', $item, $annotations);
});
$getAnnotationValue = current(array_filter($annotations, function ($item) use ($annotationName) {
return preg_match('#@' . $annotationName . '.+#', $item);
}));
// L'annotation sans valeur @example @length
$getAnnotation = current(array_filter($annotations, function ($item) use ($annotationName) {
return preg_match('#@' . $annotationName . '#', $item);
}));
$name = null;
$value = null;
if ($getAnnotationValue) {
list($name, $value) = explode(' ', $getAnnotationValue);
} elseif ($getAnnotation) {
list($name) = explode(' ', $getAnnotation);
}
return [str_replace('@', '', $name), $value];
}
/**
* Récupère la valeur de l'anotation s'il y en a une.
*
* @return null|string
*/
public function getValue(): ?string
{
return $this->value;
}
/**
* Récupère le nom de l'annotation.
*
* @return string
*/
public function getName(): ?string
{
return $this->name;
}
/**
* @param string $annotationName
*
* @return null|object
*/
public function getClassAnnotation(string $annotationName)
{
return (new AnnotationReader())->getClassAnnotation($this->argument, $annotationName);
}
/**
* @param string $annotationName
* @return null|object
*/
public function getPropertyAnnotation(string $annotationName)
{
return (new AnnotationReader)->getPropertyAnnotation(
new \ReflectionProperty($this->argument->getName(), $this->propertyName),
$annotationName
);
}
}