1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Webino (http://webino.sk) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/webino for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2015-2017 Webino, s.r.o. (http://webino.sk) |
7
|
|
|
* @author Peter Bačinský <[email protected]> |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace WebinoConfigLib\Router; |
12
|
|
|
|
13
|
|
|
use WebinoConfigLib\AbstractConfig; |
14
|
|
|
use WebinoConfigLib\Exception\InvalidArgumentException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class AbstractRoute |
18
|
|
|
*/ |
19
|
|
|
abstract class AbstractRoute extends AbstractConfig implements |
20
|
|
|
RouteInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $name; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $path; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return bool |
34
|
|
|
*/ |
35
|
|
|
public function hasName() |
36
|
|
|
{ |
37
|
|
|
return !empty($this->name); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function getName() |
44
|
|
|
{ |
45
|
|
|
return $this->name; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function setName($name) |
52
|
|
|
{ |
53
|
|
|
$this->name = (string) $name; |
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
protected function hasPath() |
61
|
|
|
{ |
62
|
|
|
return !is_null($this->path); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
protected function getPath() |
69
|
|
|
{ |
70
|
|
|
return $this->path; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function setPath($path) |
77
|
|
|
{ |
78
|
|
|
$this->path = (string) $path; |
79
|
|
|
$path and $this->setRouteOption($path); |
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $route |
85
|
|
|
* @return $this |
86
|
|
|
*/ |
87
|
|
|
protected function setRouteOption($route) |
88
|
|
|
{ |
89
|
|
|
$this->getData()->options['route'] = (string) $route; |
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public function getType() |
97
|
|
|
{ |
98
|
|
|
return $this->getData()->type; |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function setType($type = self::LITERAL) |
105
|
|
|
{ |
106
|
|
|
$this->getData()->type = (string) $type; |
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
|
|
public function setMayTerminate($mayTerminate = true) |
114
|
|
|
{ |
115
|
|
|
$this->getData()->may_terminate = $mayTerminate; |
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
protected function getDefaults() |
123
|
|
|
{ |
124
|
|
|
return $this->getData()->options['defaults']; |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
|
View Code Duplication |
public function setDefaults(array $defaults) |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
$data = $this->getData(); |
133
|
|
|
foreach ($defaults as $key => $value) { |
134
|
|
|
$data->options['defaults'][$key] = $value; |
|
|
|
|
135
|
|
|
} |
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
|
|
public function setChild(array $routes) |
143
|
|
|
{ |
144
|
|
|
foreach ($routes as $route) { |
145
|
|
|
$this->appendChildRoute($route); |
146
|
|
|
} |
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
|
|
public function chain(array $routes) |
154
|
|
|
{ |
155
|
|
|
foreach ($routes as $route) { |
156
|
|
|
$this->appendRoute('chain_routes', $route); |
157
|
|
|
} |
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param array $handlers |
163
|
|
|
* @return $this |
164
|
|
|
*/ |
165
|
|
View Code Duplication |
protected function setHandlers(array $handlers) |
|
|
|
|
166
|
|
|
{ |
167
|
|
|
$data = $this->getData(); |
168
|
|
|
foreach ($handlers as $key => $value) { |
169
|
|
|
$data->options['defaults']['handlers'][$key] = $value; |
|
|
|
|
170
|
|
|
} |
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param string $section |
176
|
|
|
* @param self $route |
177
|
|
|
* @return $this |
178
|
|
|
*/ |
179
|
|
|
protected function appendRoute($section, self $route) |
180
|
|
|
{ |
181
|
|
|
if ($route->hasName()) { |
182
|
|
|
$this->mergeArray([$section => [$route->getName() => $route->toArray()]]); |
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
if ($this->getData()->offsetExists($section)) { |
187
|
|
|
$this->getData()->{$section}[] = $route->toArray(); |
188
|
|
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$this->mergeArray([$section => [$route->toArray()]]); |
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param RouteInterface $route |
197
|
|
|
* @return $this |
198
|
|
|
*/ |
199
|
|
|
protected function appendChildRoute(RouteInterface $route) |
200
|
|
|
{ |
201
|
|
|
if (!($route instanceof self)) { |
202
|
|
|
throw (new InvalidArgumentException('Expected route of type %s but got %s')) |
203
|
|
|
->format(self::class, get_class($route)); |
204
|
|
|
} |
205
|
|
|
$this->appendRoute('child_routes', $route); |
|
|
|
|
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.