Failed Conditions
Push — master ( 1d9c07...86291e )
by Atanas
02:02
created

RouteBlueprint::attribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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