1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Meraki\Route; |
5
|
|
|
|
6
|
|
|
use Meraki\Route\Pattern; |
7
|
|
|
use Psr\Http\Server\RequestHandlerInterface as RequestHandler; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* A mapping of an incoming request to its appropriate request-handler. |
12
|
|
|
* |
13
|
|
|
* @author Nathan Bishop <[email protected]> (https://nathanbishop.name) |
14
|
|
|
* @copyright 2019 Nathan Bishop |
15
|
|
|
* @license The MIT license. |
16
|
|
|
*/ |
17
|
|
|
final class Rule |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string [$method description] |
21
|
|
|
*/ |
22
|
|
|
private $method; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Pattern [$pattern description] |
26
|
|
|
*/ |
27
|
|
|
private $pattern; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var RequestHandler [$handler description] |
31
|
|
|
*/ |
32
|
|
|
private $handler; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string [$name description] |
36
|
|
|
*/ |
37
|
|
|
private $name; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* [__construct description] |
41
|
|
|
* |
42
|
|
|
* @param string $method [description] |
43
|
|
|
* @param Pattern $pattern [description] |
44
|
|
|
* @param RequestHandler $handler [description] |
45
|
|
|
* @throws InvalidArgumentException [<description>] |
46
|
|
|
*/ |
47
|
|
|
public function __construct(string $method, Pattern $pattern, RequestHandler $handler) |
48
|
|
|
{ |
49
|
|
|
if (empty($method)) { |
50
|
|
|
throw new InvalidArgumentException('A request method was not provided.'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->method = $method; |
54
|
|
|
$this->pattern = $pattern; |
55
|
|
|
$this->handler = $handler; |
56
|
|
|
$this->name = ''; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* [getMethod description] |
61
|
|
|
* |
62
|
|
|
* @return string [description] |
63
|
|
|
*/ |
64
|
|
|
public function getMethod(): string |
65
|
|
|
{ |
66
|
|
|
return $this->method; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* [getPattern description] |
71
|
|
|
* |
72
|
|
|
* @return Pattern [description] |
73
|
|
|
*/ |
74
|
|
|
public function getPattern(): Pattern |
75
|
|
|
{ |
76
|
|
|
return $this->pattern; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* [getHandler description] |
81
|
|
|
* |
82
|
|
|
* @return RequestHandler [description] |
83
|
|
|
*/ |
84
|
|
|
public function getHandler(): RequestHandler |
85
|
|
|
{ |
86
|
|
|
return $this->handler; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* [getName description] |
91
|
|
|
* |
92
|
|
|
* @return string [description] |
93
|
|
|
*/ |
94
|
|
|
public function getName(): string |
95
|
|
|
{ |
96
|
|
|
return $this->name; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* [name description] |
101
|
|
|
* |
102
|
|
|
* @param string $name [description] |
103
|
|
|
* @throws InvalidArgumentException [description] |
104
|
|
|
* @throws InvalidArgumentException [description] |
105
|
|
|
* @return self [description] |
106
|
|
|
*/ |
107
|
|
|
public function name(string $name): self |
108
|
|
|
{ |
109
|
|
|
if ($this->name) { |
110
|
|
|
throw new InvalidArgumentException('Name is immutable and cannot be changed once set.'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if (empty($name)) { |
114
|
|
|
throw new InvalidArgumentException('Name cannot be empty.'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$this->name = $name; |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* [matchesMethod description] |
124
|
|
|
* |
125
|
|
|
* @param string $requestMethod [description] |
126
|
|
|
* @return boolean [description] |
127
|
|
|
*/ |
128
|
|
|
public function matchesMethod(string $requestMethod): bool |
129
|
|
|
{ |
130
|
|
|
return strcasecmp($this->method, $requestMethod) === 0; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* [create description] |
135
|
|
|
* |
136
|
|
|
* @param string $method [description] |
137
|
|
|
* @param string $pattern [description] |
138
|
|
|
* @param RequestHandler $handler [description] |
139
|
|
|
* @return self [description] |
140
|
|
|
*/ |
141
|
|
|
public static function create(string $method, string $pattern, RequestHandler $handler): self |
142
|
|
|
{ |
143
|
|
|
return new self($method, new Pattern($pattern), $handler); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|