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