|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\Restful\Application; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\Restful\Exceptions\InvalidArgumentException; |
|
7
|
|
|
use Nette\Application\UI\MethodReflection; |
|
8
|
|
|
use Nette\Http\IRequest; |
|
9
|
|
|
use ReflectionMethod; |
|
10
|
|
|
use Reflector; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* RouteAnnotation |
|
15
|
|
|
* @package kalanis\Restful\Application |
|
16
|
|
|
*/ |
|
17
|
1 |
|
class RouteAnnotation implements IAnnotationParser |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** @var array<string, int> */ |
|
21
|
|
|
private array $methods = [ |
|
22
|
|
|
IRequest::Get => IResourceRouter::GET, |
|
23
|
|
|
IRequest::Post => IResourceRouter::POST, |
|
24
|
|
|
IRequest::Put => IResourceRouter::PUT, |
|
25
|
|
|
IRequest::Delete => IResourceRouter::DELETE, |
|
26
|
|
|
IRequest::Head => IResourceRouter::HEAD, |
|
27
|
|
|
'PATCH' => IResourceRouter::PATCH |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Get parsed |
|
32
|
|
|
* @return array<string, int> |
|
33
|
|
|
*/ |
|
34
|
|
|
public function getMethods(): array |
|
35
|
|
|
{ |
|
36
|
1 |
|
return $this->methods; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param ReflectionMethod $reflection |
|
41
|
|
|
* @throws InvalidArgumentException |
|
42
|
|
|
* @return array<int, mixed> |
|
43
|
|
|
*/ |
|
44
|
|
|
public function parse(Reflector $reflection): array |
|
45
|
|
|
{ |
|
46
|
1 |
|
if (!$reflection instanceof MethodReflection) { |
|
47
|
|
|
throw new InvalidArgumentException('RouteAnnotation can be parsed only on method from Nette\Application\UI\MethodReflection'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
$result = []; |
|
51
|
1 |
|
foreach ($this->getMethods() as $methodName => $methodFlag) { |
|
52
|
1 |
|
if ($reflection->hasAnnotation($methodName)) { |
|
53
|
1 |
|
$result[$methodFlag] = $reflection->getAnnotation($methodName); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
return $result; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|