Failed Conditions
Branch refactor/kernels (4c6e24)
by Atanas
01:47
created

RouteRegistrar::patch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
crap 2
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
/**
13
 * Provide a fluent interface for registering routes with the router.
14
 */
15
class RouteRegistrar {
16
	/**
17
	 * Router.
18
	 *
19
	 * @var Router
20
	 */
21
	protected $router = null;
22
23
	/**
24
	 * Attributes.
25
	 *
26
	 * @var array<string, mixed>
27
	 */
28
	protected $attributes = [];
29
30
	/**
31
	 * Constructor.
32
	 *
33
	 * @codeCoverageIgnore
34
	 * @param Router $router
35
	 */
36
	public function __construct( Router $router ) {
37
		$this->router = $router;
38
	}
39
40
	/**
41
	 * Set the condition attribute.
42
	 *
43
	 * @param  mixed  $condition
44
	 * @return static $this
45
	 */
46
	public function condition( $condition ) {
47
		$this->attributes['condition'] = $condition;
48
49
		return $this;
50
	}
51
52
	/**
53
	 * Set a key for the where attribute.
54
	 *
55
	 * @param  string $parameter
56
	 * @param  string $pattern
57
	 * @return static                $this
58
	 */
59
	public function where( $parameter, $pattern ) {
60
		if ( ! isset( $this->attributes['where'] ) ) {
61
			$this->attributes['where'] = [];
62
		}
63
64
		$this->attributes['where'][ $parameter ] = $pattern;
65
66
		return $this;
67
	}
68
69
	/**
70
	 * Set the middleware attribute.
71
	 *
72
	 * @param  string|array<string> $middleware
73
	 * @return static               $this
74
	 */
75
	public function middleware( $middleware ) {
76
		$this->attributes['middleware'] = $middleware;
77
78
		return $this;
79
	}
80
81
	/**
82
	 * Create a new route group.
83
	 *
84
	 * @param \Closure|string $routes Closure or path to file.
85
	 * @return void
86
	 */
87
	public function group( $routes ) {
88
		$this->router->group( $this->attributes, $routes );
89
	}
90
91
	/**
92
	 * Create a new route.
93
	 *
94
	 * @param  string         $method
95
	 * @param  array          $arguments
96
	 * @return RouteInterface
97
	 */
98
	protected function makeRoute( $method, $arguments ) {
99
		$route = call_user_func_array( [$this->router, $method], $arguments );
100
101
		if ( ! empty( $this->attributes['where'] ) ) {
102
			foreach ( $this->attributes['where'] as $parameter => $pattern ) {
103
				$route->where( $parameter, $pattern );
104
			}
105
		}
106
107
		if ( ! empty( $this->attributes['middleware'] ) ) {
108
			$route->middleware( $this->attributes['middleware'] );
109
		}
110
111
		return $route;
112
	}
113
114
	/**
115
	 * Create and add a new route.
116
	 *
117
	 * @param  string[]             $methods
118
	 * @param  mixed                $condition
119
	 * @param  string|\Closure|null $handler
120
	 * @return RouteInterface
121
	 */
122
	public function route( $methods, $condition, $handler = null ) {
123
		return $this->makeRoute( 'route', func_get_args() );
124
	}
125
126
	/**
127
	 * Create and add a route for the GET and HEAD methods.
128
	 *
129
	 * @param  mixed                $condition
130
	 * @param  string|\Closure|null $handler
131
	 * @return RouteInterface
132
	 */
133
	public function get( $condition, $handler = null ) {
134
		return $this->makeRoute( 'get', func_get_args() );
135
	}
136
137
	/**
138
	 * Create and add a route for the POST method.
139
	 *
140
	 * @param  mixed                $condition
141
	 * @param  string|\Closure|null $handler
142
	 * @return RouteInterface
143
	 */
144
	public function post( $condition, $handler = null ) {
145
		return $this->makeRoute( 'post', func_get_args() );
146
	}
147
148
	/**
149
	 * Create and add a route for the PUT method.
150
	 *
151
	 * @param  mixed                $condition
152
	 * @param  string|\Closure|null $handler
153
	 * @return RouteInterface
154
	 */
155
	public function put( $condition, $handler = null ) {
156
		return $this->makeRoute( 'put', func_get_args() );
157
	}
158
159
	/**
160
	 * Create and add a route for the PATCH method.
161
	 *
162
	 * @param  mixed                $condition
163
	 * @param  string|\Closure|null $handler
164
	 * @return RouteInterface
165
	 */
166
	public function patch( $condition, $handler = null ) {
167
		return $this->makeRoute( 'patch', func_get_args() );
168
	}
169
170
	/**
171
	 * Create and add a route for the DELETE method.
172
	 *
173
	 * @param  mixed                $condition
174
	 * @param  string|\Closure|null $handler
175
	 * @return RouteInterface
176
	 */
177
	public function delete( $condition, $handler = null ) {
178
		return $this->makeRoute( 'delete', func_get_args() );
179
	}
180
181
	/**
182
	 * Create and add a route for the OPTIONS method.
183
	 *
184
	 * @param  mixed                $condition
185
	 * @param  string|\Closure|null $handler
186
	 * @return RouteInterface
187
	 */
188
	public function options( $condition, $handler = null ) {
189
		return $this->makeRoute( 'options', func_get_args() );
190
	}
191
192
	/**
193
	 * Create and add a route for all supported methods.
194
	 *
195
	 * @param  mixed                $condition
196
	 * @param  string|\Closure|null $handler
197
	 * @return RouteInterface
198
	 */
199
	public function any( $condition, $handler = null ) {
200
		return $this->makeRoute( 'any', func_get_args() );
201
	}
202
203
	/**
204
	 * Create and add a route that will always be satisfied.
205
	 *
206
	 * @param  string|\Closure|null $handler
207
	 * @return RouteInterface
208
	 */
209
	public function all( $handler = null ) {
210
		return $this->makeRoute( 'all', func_get_args() );
211
	}
212
}
213