1 | <?php |
||
21 | class AnnotationReader |
||
22 | { |
||
23 | /** |
||
24 | * @var BaseReader |
||
25 | */ |
||
26 | private $reader; |
||
27 | /** |
||
28 | * @var DocBlockFactory |
||
29 | */ |
||
30 | private $docBlockFactory; |
||
31 | |||
32 | 14 | public function __construct(BaseReader $reader, DocBlockFactory $docBlockFactory) |
|
33 | { |
||
34 | 14 | $this->reader = $reader; |
|
35 | 14 | $this->docBlockFactory = $docBlockFactory; |
|
36 | 14 | } |
|
37 | |||
38 | 4 | public function isApi($method): bool |
|
39 | { |
||
40 | 4 | return (bool) $this->getApi($method); |
|
41 | } |
||
42 | |||
43 | 3 | public function getDescription($method): string |
|
44 | { |
||
45 | 3 | return $this->docBlockFactory->create($this->getMethod($method))->getSummary(); |
|
46 | } |
||
47 | |||
48 | 5 | public function getName($method): string |
|
49 | { |
||
50 | 5 | return (string) $this->getApi($method)->name; |
|
51 | } |
||
52 | |||
53 | /** |
||
54 | * @return array|Param[] |
||
55 | */ |
||
56 | 4 | public function getParameters($method): array |
|
57 | { |
||
58 | 4 | $reflection = $this->getMethod($method); |
|
59 | 4 | $parameters = []; |
|
60 | 4 | $docBlock = $this->docBlockFactory->create($reflection->getDocComment()); |
|
61 | 4 | $params = $docBlock->getTagsByName('param'); |
|
62 | |||
63 | 4 | foreach ($reflection->getParameters() as $parameter) { |
|
64 | 4 | $parameters[$parameter->name] = new Param( |
|
65 | 4 | $parameter->name, |
|
66 | 4 | $this->parseParameterDocblock($params, $parameter), |
|
67 | 4 | $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : Param::class |
|
68 | ); |
||
69 | } |
||
70 | |||
71 | 4 | return $parameters; |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * @return Api|null |
||
76 | */ |
||
77 | 7 | private function getApi($method) |
|
78 | { |
||
79 | 7 | return $this->reader->getMethodAnnotation( |
|
80 | 7 | $this->getMethod($method), |
|
81 | 7 | Api::class |
|
82 | ); |
||
83 | } |
||
84 | |||
85 | 10 | private function getMethod($method): \ReflectionMethod |
|
95 | |||
96 | 4 | private function parseParameterDocblock(array $params, \ReflectionParameter $parameter): string |
|
97 | { |
||
98 | 4 | foreach ($params as $param) { |
|
99 | 4 | if ($parameter->name === $param->getVariableName()) { |
|
100 | 4 | return $param->getDescription()->render(); |
|
101 | } |
||
106 | } |
||
107 |