Failed Conditions
Branch refactor/kernels (3a00e4)
by Atanas
01:41
created

RouteBlueprint::options()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
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\ConditionFactory;
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
	public function getAttributes() {
50
		return $this->attributes;
51
	}
52
53
	/**
54
	 * Set the attributes.
55
	 *
56
	 * @param  array<string, mixed> $attributes
57
	 * @return void
58
	 */
59
	public function setAttributes( $attributes ) {
60
		$this->attributes = $attributes;
61
	}
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
	 * @return mixed
81
	 */
82
	public function getAttribute( $key, $default = '' ) {
83
		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
	public function setAttribute( $key, $value ) {
94
		$this->setAttributes( array_merge(
95
			$this->getAttributes(),
96
			[$key => $value]
97
		) );
98
	}
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
	public function methods( $methods ) {
121
		$methods = array_merge(
122
			$this->getAttribute( 'methods', [] ),
123
			$methods
124
		);
125
126
		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
	public function url( $url, $where = [] ) {
137
		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
	public function where( $condition ) {
148
		if ( ! $condition instanceof ConditionInterface ) {
149
			$condition = func_get_args();
150
		}
151
152
		$condition = $this->router->mergeConditionAttribute(
153
			$this->getAttribute( 'condition' ),
0 ignored issues
show
Bug introduced by
It seems like $this->getAttribute('condition') can also be of type array<string,mixed>; however, parameter $old of WPEmerge\Routing\Router::mergeConditionAttribute() does only seem to accept WPEmerge\Routing\Conditi...nditionInterface|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

153
			/** @scrutinizer ignore-type */ $this->getAttribute( 'condition' ),
Loading history...
154
			$condition
0 ignored issues
show
Bug introduced by
It seems like $condition can also be of type array; however, parameter $new of WPEmerge\Routing\Router::mergeConditionAttribute() does only seem to accept WPEmerge\Routing\Conditi...nditionInterface|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

154
			/** @scrutinizer ignore-type */ $condition
Loading history...
155
		);
156
157
		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
	public function middleware( $middleware ) {
167
		$middleware = array_merge(
168
			(array) $this->getAttribute( 'middleware', [] ),
169
			(array) $middleware
170
		);
171
172
		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
	public function setNamespace( $namespace ) {
184
		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
	public function group( $routes ) {
194
		$this->router->group( $this->getAttributes(), $routes );
195
	}
196
197
	/**
198
	 * Create a route.
199
	 *
200
	 * @param  string|\Closure $handler
201
	 * @return void
202
	 */
203
	public function handle( $handler = '' ) {
204
		if ( ! empty( $handler ) ) {
205
			$this->attribute( 'handler', $handler );
206
		}
207
208
		$this->router->addRoute( $this->router->route( $this->getAttributes() ) );
209
	}
210
211
	/**
212
	 * Match requests with a method of GET or HEAD.
213
	 *
214
	 * @return static $this
215
	 */
216
	public function get() {
217
		return $this->methods( ['GET', 'HEAD'] );
218
	}
219
220
	/**
221
	 * Match requests with a method of POST.
222
	 *
223
	 * @return static $this
224
	 */
225
	public function post() {
226
		return $this->methods( ['POST'] );
227
	}
228
229
	/**
230
	 * Match requests with a method of PUT.
231
	 *
232
	 * @return static $this
233
	 */
234
	public function put() {
235
		return $this->methods( ['PUT'] );
236
	}
237
238
	/**
239
	 * Match requests with a method of PATCH.
240
	 *
241
	 * @return static $this
242
	 */
243
	public function patch() {
244
		return $this->methods( ['PATCH'] );
245
	}
246
247
	/**
248
	 * Match requests with a method of DELETE.
249
	 *
250
	 * @return static $this
251
	 */
252
	public function delete() {
253
		return $this->methods( ['DELETE'] );
254
	}
255
256
	/**
257
	 * Match requests with a method of OPTIONS.
258
	 *
259
	 * @return static $this
260
	 */
261
	public function options() {
262
		return $this->methods( ['OPTIONS'] );
263
	}
264
265
	/**
266
	 * Match requests with any method.
267
	 *
268
	 * @return static $this
269
	 */
270
	public function any() {
271
		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
	public function all( $handler = '' ) {
281
		$this->any()->url( '*' )->handle( $handler );
282
	}
283
}
284