1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JetFire\Routing; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Route |
7
|
|
|
* @package JetFire\Routing |
8
|
|
|
* @method getParameters() |
9
|
|
|
* @method getBlock() |
10
|
|
|
* @method getPath() |
11
|
|
|
*/ |
12
|
|
|
class Route |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var |
17
|
|
|
*/ |
18
|
|
|
private $url; |
19
|
|
|
/** |
20
|
|
|
* @var |
21
|
|
|
*/ |
22
|
|
|
private $name; |
23
|
|
|
/** |
24
|
|
|
* @var |
25
|
|
|
*/ |
26
|
|
|
private $callback; |
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $method; |
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $target = []; |
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $detail = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
*/ |
42
|
|
|
public function __construct() |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$this->method = ( |
45
|
|
|
isset($_POST['_METHOD']) |
46
|
|
|
&& in_array($_POST['_METHOD'], array('PUT', 'DELETE')) |
47
|
|
|
) ? $_POST['_METHOD'] : $_SERVER['REQUEST_METHOD']; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array $args |
52
|
|
|
*/ |
53
|
|
|
public function set($args = []) |
54
|
|
|
{ |
55
|
|
|
if (isset($args['name'])) $this->name = $args['name']; |
56
|
|
|
if (isset($args['callback'])) $this->callback = $args['callback']; |
57
|
|
|
if (isset($args['target'])) $this->target = $args['target']; |
58
|
|
|
if (isset($args['detail'])) $this->detail = $args['detail']; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return null |
63
|
|
|
*/ |
64
|
|
|
public function getUrl() |
65
|
|
|
{ |
66
|
|
|
return $this->url; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $url |
71
|
|
|
*/ |
72
|
|
|
public function setUrl($url) |
73
|
|
|
{ |
74
|
|
|
$this->url = $url; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return null |
79
|
|
|
*/ |
80
|
|
|
public function getName() |
81
|
|
|
{ |
82
|
|
|
return $this->name; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param $name |
87
|
|
|
*/ |
88
|
|
|
public function setName($name) |
89
|
|
|
{ |
90
|
|
|
$this->name = $name; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
|
|
public function getCallback() |
97
|
|
|
{ |
98
|
|
|
return $this->callback; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param $callback |
103
|
|
|
*/ |
104
|
|
|
public function setCallback($callback) |
105
|
|
|
{ |
106
|
|
|
$this->callback = $callback; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
public function getMethod() |
113
|
|
|
{ |
114
|
|
|
return $this->method; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
public function getDetail() |
121
|
|
|
{ |
122
|
|
|
return $this->detail; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param $detail |
127
|
|
|
*/ |
128
|
|
|
public function setDetail($detail) |
129
|
|
|
{ |
130
|
|
|
$this->detail = array_merge($detail,$this->detail); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param $key |
135
|
|
|
* @param $value |
136
|
|
|
*/ |
137
|
|
|
public function addDetail($key, $value) |
138
|
|
|
{ |
139
|
|
|
$this->detail[$key] = $value; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param null $key |
144
|
|
|
* @return array|string |
145
|
|
|
*/ |
146
|
|
|
public function getTarget($key = null) |
147
|
|
|
{ |
148
|
|
|
if (!is_null($key)) |
149
|
|
|
return isset($this->target[$key]) ? $this->target[$key] : ''; |
150
|
|
|
return empty($this->target) ? '' : $this->target; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param $target |
155
|
|
|
* @return mixed |
156
|
|
|
*/ |
157
|
|
|
public function setTarget($target = []) |
158
|
|
|
{ |
159
|
|
|
$this->target = $target; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param $key |
164
|
|
|
* @param $value |
165
|
|
|
*/ |
166
|
|
|
public function addTarget($key, $value) |
167
|
|
|
{ |
168
|
|
|
$this->target[$key] = $value; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param null $key |
173
|
|
|
* @return bool |
174
|
|
|
*/ |
175
|
|
|
public function hasTarget($key = null) |
176
|
|
|
{ |
177
|
|
|
if (!is_null($key)) |
178
|
|
|
return isset($this->target[$key]) ? true : false; |
179
|
|
|
return empty($this->target) ? false : true; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return array |
184
|
|
|
*/ |
185
|
|
|
public function getData(){ |
186
|
|
|
return (isset($this->getDetail()['data']) && is_array($this->getDetail()['data']))?$this->getDetail()['data']:[]; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param $name |
191
|
|
|
* @param $arguments |
192
|
|
|
* @return null |
193
|
|
|
*/ |
194
|
|
|
public function __call($name, $arguments) |
195
|
|
|
{ |
196
|
|
|
if (substr($name, 0, 3) === "get") { |
197
|
|
|
$key = strtolower(str_replace('get', '', $name)); |
198
|
|
|
return isset($this->detail[$key]) ? $this->detail[$key] : ''; |
199
|
|
|
} elseif (substr($name, 0, 3) === "set") { |
200
|
|
|
$key = strtolower(str_replace('set', '', $name)); |
201
|
|
|
$this->detail[$key] = $arguments[0]; |
202
|
|
|
} |
203
|
|
|
return ''; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: