Passed
Push — master ( 65a687...eeed00 )
by Atanas
01:48
created

RouteBlueprint::url()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package   WPEmerge
4
 * @author    Atanas Angelov <[email protected]>
5
 * @copyright 2018 Atanas Angelov
6
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7
 * @link      https://wpemerge.com/
8
 */
9
10
namespace WPEmerge\Routing;
11
12
use WPEmerge\Facades\Response;
13
use WPEmerge\Routing\Conditions\ConditionInterface;
14
use WPEmerge\Support\Arr;
15
16
/**
17
 * Provide a fluent interface for registering routes with the router.
18
 */
19
class RouteBlueprint {
20
	/**
21
	 * Router.
22
	 *
23
	 * @var Router
24
	 */
25
	protected $router = null;
26
27
	/**
28
	 * Attributes.
29
	 *
30
	 * @var array<string, mixed>
31
	 */
32
	protected $attributes = [];
33
34
	/**
35
	 * Constructor.
36
	 *
37
	 * @codeCoverageIgnore
38
	 * @param Router           $router
39
	 */
40
	public function __construct( Router $router ) {
41
		$this->router = $router;
42
	}
43
44
	/**
45
	 * Get attributes.
46
	 *
47
	 * @return array<string, mixed>
48
	 */
49 1
	public function getAttributes() {
50 1
		return $this->attributes;
51
	}
52
53
	/**
54
	 * Set the attributes.
55
	 *
56
	 * @param  array<string, mixed> $attributes
57
	 * @return void
58
	 */
59 1
	public function setAttributes( $attributes ) {
60 1
		$this->attributes = $attributes;
61 1
	}
62
63
	/**
64
	 * Fluent alias for setAttributes().
65
	 *
66
	 * @codeCoverageIgnore
67
	 * @param  array<string, mixed> $attributes
68
	 * @return static               $this
69
	 */
70
	public function attributes( $attributes ) {
71
		$this->setAttributes( $attributes );
72
73
		return $this;
74
	}
75
76
	/**
77
	 * Get attribute.
78
	 *
79
	 * @param  string $key
80
	 * @param  mixed  $default
81
	 * @return mixed
82
	 */
83 1
	public function getAttribute( $key, $default = '' ) {
84 1
		return Arr::get( $this->getAttributes(), $key, $default );
85
	}
86
87
	/**
88
	 * Set attribute.
89
	 *
90
	 * @param  string $key
91
	 * @param  mixed  $value
92
	 * @return void
93
	 */
94 1
	public function setAttribute( $key, $value ) {
95 1
		$this->setAttributes( array_merge(
96 1
			$this->getAttributes(),
97 1
			[$key => $value]
98
		) );
99 1
	}
100
101
	/**
102
	 * Set attribute.
103
	 *
104
	 * @codeCoverageIgnore
105
	 * @param  string $key
106
	 * @param  mixed  $value
107
	 * @return static $this
108
	 */
109
	public function attribute( $key, $value ) {
110
		$this->setAttribute( $key, $value );
111
112
		return $this;
113
	}
114
115
	/**
116
	 * Match requests using one of the specified methods.
117
	 *
118
	 * @param  array<string> $methods
119
	 * @return static        $this
120
	 */
121 1
	public function methods( $methods ) {
122 1
		$methods = array_merge(
123 1
			$this->getAttribute( 'methods', [] ),
124 1
			$methods
125
		);
126
127 1
		return $this->attribute( 'methods', $methods );
128
	}
129
130
	/**
131
	 * Set the condition attribute to a URL.
132
	 *
133
	 * @param  string                $url
134
	 * @param  array<string, string> $where
135
	 * @return static                $this
136
	 */
137 1
	public function url( $url, $where = [] ) {
138 1
		return $this->where( 'url', $url, $where );
139
	}
140
141
	/**
142
	 * Set the condition attribute.
143
	 *
144
	 * @param  string|array|ConditionInterface $condition
145
	 * @param  mixed                           ,...$arguments
146
	 * @return static                          $this
147
	 */
148 3
	public function where( $condition ) {
149 3
		if ( ! $condition instanceof ConditionInterface ) {
150 3
			$condition = func_get_args();
151
		}
152
153 3
		$condition = $this->router->mergeConditionAttribute(
154 3
			$this->getAttribute( 'condition' ),
155 3
			$condition
156
		);
157
158 3
		return $this->attribute( 'condition', $condition );
159
	}
160
161
	/**
162
	 * Set the middleware attribute.
163
	 *
164
	 * @param  string|array<string> $middleware
165
	 * @return static               $this
166
	 */
167 1
	public function middleware( $middleware ) {
168 1
		$middleware = array_merge(
169 1
			(array) $this->getAttribute( 'middleware', [] ),
170 1
			(array) $middleware
171
		);
172
173 1
		return $this->attribute( 'middleware', $middleware );
174
	}
175
176
	/**
177
	 * Set the namespace attribute.
178
	 * This should be renamed to namespace for consistency once minimum PHP
179
	 * version is increased to 7+.
180
	 *
181
	 * @param  string $namespace
182
	 * @return static $this
183
	 */
184 1
	public function setNamespace( $namespace ) {
185 1
		return $this->attribute( 'namespace', $namespace );
186
	}
187
188
	/**
189
	 * Set the query attribute.
190
	 *
191
	 * @param  callable $query
192
	 * @return static   $this
193
	 */
194 1
	public function query( $query ) {
195 1
 		$query = $this->router->mergeQueryAttribute(
196 1
			$this->getAttribute( 'query', null ),
197 1
			$query
198
		);
199
200 1
		return $this->attribute( 'query', $query );
201
	}
202
203
	/**
204
	 * Create a route group.
205
	 *
206
	 * @param \Closure|string $routes Closure or path to file.
207
	 * @return void
208
	 */
209 1
	public function group( $routes ) {
210 1
		$this->router->group( $this->getAttributes(), $routes );
211 1
	}
212
213
	/**
214
	 * Create a route.
215
	 *
216
	 * @param  string|\Closure $handler
217
	 * @return void
218
	 */
219 2
	public function handle( $handler = '' ) {
220 2
		if ( ! empty( $handler ) ) {
221 1
			$this->attribute( 'handler', $handler );
222
		}
223
224 2
		$this->router->addRoute( $this->router->route( $this->getAttributes() ) );
225 2
	}
226
227
	/**
228
	 * Handle a request by directly rendering a view.
229
	 *
230
	 * @param  string|array<string> $views
231
	 * @return void
232
	 */
233
	public function view( $views ) {
234 1
		$this->handle( function () use ( $views ) {
235 1
			return Response::view( $views );
236 1
		} );
237 1
	}
238
239
	/**
240
	 * Match ALL requests.
241
	 *
242
	 * @param  string|\Closure $handler
243
	 * @return void
244
	 */
245 1
	public function all( $handler = '' ) {
246 1
		$this->any()->url( '*' )->handle( $handler );
247 1
	}
248
249
	/**
250
	 * Match requests with a method of GET or HEAD.
251
	 *
252
	 * @return static $this
253
	 */
254 1
	public function get() {
255 1
		return $this->methods( ['GET', 'HEAD'] );
256
	}
257
258
	/**
259
	 * Match requests with a method of POST.
260
	 *
261
	 * @return static $this
262
	 */
263 1
	public function post() {
264 1
		return $this->methods( ['POST'] );
265
	}
266
267
	/**
268
	 * Match requests with a method of PUT.
269
	 *
270
	 * @return static $this
271
	 */
272 1
	public function put() {
273 1
		return $this->methods( ['PUT'] );
274
	}
275
276
	/**
277
	 * Match requests with a method of PATCH.
278
	 *
279
	 * @return static $this
280
	 */
281 1
	public function patch() {
282 1
		return $this->methods( ['PATCH'] );
283
	}
284
285
	/**
286
	 * Match requests with a method of DELETE.
287
	 *
288
	 * @return static $this
289
	 */
290 1
	public function delete() {
291 1
		return $this->methods( ['DELETE'] );
292
	}
293
294
	/**
295
	 * Match requests with a method of OPTIONS.
296
	 *
297
	 * @return static $this
298
	 */
299 1
	public function options() {
300 1
		return $this->methods( ['OPTIONS'] );
301
	}
302
303
	/**
304
	 * Match requests with any method.
305
	 *
306
	 * @return static $this
307
	 */
308 1
	public function any() {
309 1
		return $this->methods( ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'] );
310
	}
311
}
312