1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Routify; |
4
|
|
|
|
5
|
|
|
use Routify\Exceptions\InvalidMiddlewareException; |
6
|
|
|
use Routify\Exceptions\MethodException; |
7
|
|
|
|
8
|
|
|
class Order { |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var string Name of the route |
12
|
|
|
*/ |
13
|
|
|
private $name; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string The path pattern. |
17
|
|
|
*/ |
18
|
|
|
private $uri; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var callable The callback response. |
22
|
|
|
*/ |
23
|
|
|
private $response; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string The request method for the path pattern. |
27
|
|
|
*/ |
28
|
|
|
private $method; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array Contains all middlewares associate to the action. |
32
|
|
|
*/ |
33
|
|
|
private $middlewares = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array The types that are supported(for middlewares). |
37
|
|
|
*/ |
38
|
|
|
private $middlewareTypes = ['before', 'after']; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array request methods availables. |
42
|
|
|
*/ |
43
|
|
|
private $availableRequestMethods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH']; |
44
|
|
|
|
45
|
|
|
public function __construct($uri, $method, $response, array $middlewares = [], $name = null) { |
46
|
|
|
$this->uri = $uri; |
47
|
|
|
if(!in_array(mb_strtoupper($method), $this->availableRequestMethods)) { |
48
|
|
|
throw new MethodException("The request method is invalid"); |
49
|
|
|
} |
50
|
|
|
$this->method = $method; |
51
|
|
|
$this->response = $response; |
52
|
|
|
if($this->isValidMiddleware($middlewares)) { |
53
|
|
|
$this->middlewares = $middlewares; |
54
|
|
|
} |
55
|
|
|
$this->name = $name; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns the route name. |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
|
64
|
|
|
public function getName() { |
65
|
|
|
return $this->name; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns the uri(the pattern path). |
70
|
|
|
* |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
|
|
|
74
|
|
|
public function getUri() { |
75
|
|
|
return $this->uri; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns the request method registered. |
80
|
|
|
* |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
|
|
84
|
|
|
public function getMethod() { |
85
|
|
|
return $this->method; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns the callback associated to the pattern. |
90
|
|
|
* |
91
|
|
|
* @return mixed |
92
|
|
|
*/ |
93
|
|
|
|
94
|
|
|
public function getResponse() { |
95
|
|
|
return $this->response; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns a list of middlewares registered. |
100
|
|
|
* |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
|
104
|
|
|
public function getMiddlewares() { |
105
|
|
|
return $this->middlewares; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Checks if the middleware contains before callback. |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
|
114
|
|
|
public function hasBefore() { |
115
|
|
|
return array_key_exists('before', $this->middlewares); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Checks if the middleware contains after callback. |
120
|
|
|
* |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
|
|
|
124
|
|
|
public function hasAfter() { |
125
|
|
|
return array_key_exists('after', $this->middlewares); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Checks if middlewares passed as an associative array |
130
|
|
|
* don't contain keys different to "before" and "after". |
131
|
|
|
* |
132
|
|
|
* @param array $middleware The associative array containing |
133
|
|
|
* middleware callbacks. |
134
|
|
|
* @return bool |
135
|
|
|
* @throws InvalidMiddlewareException |
136
|
|
|
*/ |
137
|
|
|
|
138
|
|
|
private function isValidMiddleware($middleware) { |
139
|
|
|
// if there is not middlewares then it is valid |
140
|
|
|
if(count($middleware) === 0) { |
141
|
|
|
return true; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
foreach($middleware as $key => $value) { |
145
|
|
|
if(!in_array($key, $this->middlewareTypes)) { |
146
|
|
|
throw new InvalidMiddlewareException("Only before and after middleware types are valid"); |
147
|
|
|
} |
148
|
|
|
if(!is_callable($value)) { |
149
|
|
|
throw new InvalidMiddlewareException("The middleware must be callable"); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return true; |
154
|
|
|
} |
155
|
|
|
} |