1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the slim-annotation-based package. |
7
|
|
|
* |
8
|
|
|
* (c) Gennady Knyazkin <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Redreams\Slim\Annotation; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Annotations\Annotation; |
17
|
|
|
use Redreams\Slim\Exception\InvalidArgumentException; |
18
|
|
|
use function in_array; |
19
|
|
|
use function sprintf; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Route |
23
|
|
|
* |
24
|
|
|
* @Annotation |
25
|
|
|
* @Annotation\Target({"CLASS", "METHOD"}) |
26
|
|
|
* @Annotation\Attributes({ |
27
|
|
|
* @Annotation\Attribute("methods", type="array"), |
28
|
|
|
* @Annotation\Attribute("name", type="string") |
29
|
|
|
* }) |
30
|
|
|
* @author Gennady Knyazkin <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
final class Route |
33
|
|
|
{ |
34
|
|
|
private const AVAILABLE_METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']; |
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $pattern; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array|null |
42
|
|
|
*/ |
43
|
|
|
private $methods; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string|null |
47
|
|
|
*/ |
48
|
|
|
private $name; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Route constructor. |
52
|
|
|
* |
53
|
|
|
* @param array $values |
54
|
|
|
* |
55
|
|
|
* @throws InvalidArgumentException |
56
|
|
|
*/ |
57
|
5 |
|
public function __construct(array $values) |
58
|
|
|
{ |
59
|
5 |
|
$pattern = $values['value'] ?? ''; |
60
|
5 |
|
if (empty($pattern)) { |
61
|
|
|
throw new InvalidArgumentException('Pattern required.'); |
62
|
|
|
} |
63
|
5 |
|
$this->pattern = $pattern; |
64
|
5 |
|
if (isset($values['name'])) { |
65
|
5 |
|
$this->name = $values['name']; |
66
|
|
|
} |
67
|
5 |
|
if (isset($values['methods'])) { |
68
|
5 |
|
$methods = (array)$values['methods']; |
69
|
5 |
|
foreach ($methods as $method) { |
70
|
5 |
|
if (!in_array($method, self::AVAILABLE_METHODS, true)) { |
71
|
|
|
throw new InvalidArgumentException( |
72
|
5 |
|
sprintf('Invalid method "%s". Available methods: [%s]', $method, self::AVAILABLE_METHODS) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
} |
76
|
5 |
|
$this->methods = $methods; |
77
|
|
|
} |
78
|
5 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get pattern |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
5 |
|
public function getPattern(): string |
86
|
|
|
{ |
87
|
5 |
|
return $this->pattern; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get methods |
92
|
|
|
* |
93
|
|
|
* @return array|null |
94
|
|
|
*/ |
95
|
5 |
|
public function getMethods(): ?array |
96
|
|
|
{ |
97
|
5 |
|
return $this->methods; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get name |
102
|
|
|
* |
103
|
|
|
* @return null|string |
104
|
|
|
*/ |
105
|
5 |
|
public function getName(): ?string |
106
|
|
|
{ |
107
|
5 |
|
return $this->name; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|