|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Jasny\SwitchRoute; |
|
6
|
|
|
|
|
7
|
|
|
use OutOfBoundsException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Endpoint for routing. |
|
11
|
|
|
* @internal |
|
12
|
|
|
*/ |
|
13
|
|
|
final class Endpoint |
|
14
|
|
|
{ |
|
15
|
|
|
protected string $path; |
|
16
|
|
|
protected array $routes = []; |
|
17
|
|
|
protected array $vars = []; |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Endpoint constructor. |
|
22
|
|
|
*/ |
|
23
|
18 |
|
public function __construct(string $path) |
|
24
|
|
|
{ |
|
25
|
18 |
|
$this->path = $path; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Add a route for this endpoint. |
|
30
|
|
|
*/ |
|
31
|
14 |
|
public function withRoute(string $method, mixed $route, array $vars): self |
|
32
|
|
|
{ |
|
33
|
14 |
|
$method = strtoupper($method); |
|
34
|
|
|
|
|
35
|
14 |
|
if (isset($this->routes[$method])) { |
|
36
|
3 |
|
throw new InvalidRouteException("Duplicate route for '$method {$this->path}'"); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
14 |
|
$copy = clone $this; |
|
40
|
14 |
|
$copy->routes[$method] = $route; |
|
41
|
14 |
|
$copy->vars[$method] = $vars; |
|
42
|
|
|
|
|
43
|
14 |
|
return $copy; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get the path of this endpoint. |
|
48
|
|
|
*/ |
|
49
|
2 |
|
public function getPath(): string |
|
50
|
|
|
{ |
|
51
|
2 |
|
return $this->path; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get the allowed methods for this endpoint. |
|
56
|
|
|
*/ |
|
57
|
7 |
|
public function getAllowedMethods(): array |
|
58
|
|
|
{ |
|
59
|
7 |
|
return array_diff(array_keys($this->routes), ['']); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get all routes for this endpoint. |
|
64
|
|
|
*/ |
|
65
|
4 |
|
public function getRoutes(): array |
|
66
|
|
|
{ |
|
67
|
4 |
|
return $this->routes; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get vars for a route. |
|
72
|
|
|
*/ |
|
73
|
7 |
|
public function getVars(string $method): array |
|
74
|
|
|
{ |
|
75
|
7 |
|
$method = strtoupper($method); |
|
76
|
|
|
|
|
77
|
7 |
|
if (!isset($this->vars[$method])) { |
|
78
|
1 |
|
throw new OutOfBoundsException("Method '$method' not available for endpoint '{$this->path}'"); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
6 |
|
return $this->vars[$method]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get unique routes with methods and vars. |
|
87
|
|
|
*/ |
|
88
|
4 |
|
public function getUniqueRoutes(): \Generator |
|
89
|
|
|
{ |
|
90
|
4 |
|
$queue = array_keys($this->routes); |
|
91
|
|
|
|
|
92
|
4 |
|
while ($queue !== []) { |
|
93
|
1 |
|
$method = reset($queue); |
|
94
|
|
|
|
|
95
|
1 |
|
$route = $this->routes[$method]; |
|
96
|
1 |
|
$vars = $this->vars[$method]; |
|
97
|
|
|
|
|
98
|
1 |
|
$methods = array_values(array_intersect( |
|
99
|
1 |
|
array_keys($this->routes, $route, true), |
|
100
|
1 |
|
array_keys($this->vars, $vars, true) |
|
101
|
1 |
|
)); |
|
102
|
|
|
|
|
103
|
1 |
|
yield [$methods, $route, $vars]; |
|
104
|
|
|
|
|
105
|
1 |
|
$queue = array_diff($queue, $methods); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|