1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MatthiasMullie\Api\Routes\Providers; |
4
|
|
|
|
5
|
|
|
use MatthiasMullie\Api\Routes\Route; |
6
|
|
|
use Generator; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author Matthias Mullie <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2016, Matthias Mullie. All rights reserved |
11
|
|
|
* @license LICENSE MIT |
12
|
|
|
*/ |
13
|
|
|
class ArrayRouteProvider implements RouteProviderInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array[] |
17
|
|
|
*/ |
18
|
|
|
protected $routes = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param array[] $data |
22
|
|
|
*/ |
23
|
|
|
public function __construct(array $data) |
24
|
|
|
{ |
25
|
|
|
$this->routes = $data; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return Generator |
30
|
|
|
*/ |
31
|
|
|
public function getRoutes(): Generator |
32
|
|
|
{ |
33
|
|
|
foreach ($this->routes as $data) { |
34
|
|
|
yield new Route( |
35
|
|
|
$this->getMethods($data), |
36
|
|
|
$this->getPath($data), |
37
|
|
|
$this->getHandler($data) |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param array $data |
44
|
|
|
* |
45
|
|
|
* @return string[] |
46
|
|
|
* |
47
|
|
|
* @throws Exception |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
protected function getMethods(array $data): array |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
if (!isset($data['method'])) { |
52
|
|
|
$serialized = serialize($data); |
53
|
|
|
throw new Exception("Missing method. (input: $serialized)"); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return (array) $data['method']; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param array $data |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
* |
64
|
|
|
* @throws Exception |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
protected function getPath(array $data): string |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
if (!isset($data['path'])) { |
69
|
|
|
$serialized = serialize($data); |
70
|
|
|
throw new Exception("Missing path. (input: $serialized)"); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $data['path']; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param array $data |
78
|
|
|
* |
79
|
|
|
* @return callable |
80
|
|
|
* |
81
|
|
|
* @throws Exception |
82
|
|
|
*/ |
83
|
|
|
protected function getHandler(array $data): callable |
84
|
|
|
{ |
85
|
|
|
if (!isset($data['handler'])) { |
86
|
|
|
$serialized = serialize($data); |
87
|
|
|
throw new Exception("Missing handler. (input: $serialized)"); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$handler = $data['handler']; |
91
|
|
|
|
92
|
|
|
if (is_callable($handler)) { |
93
|
|
|
return $handler; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (is_string($handler) && method_exists($handler, '__invoke')) { |
97
|
|
|
return new $handler(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$serialized = serialize($data); |
101
|
|
|
throw new Exception("Handler must be callable. (input: $serialized)"); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.