Failed Conditions
Branch refactor/route-groups (acb44c)
by Atanas
01:39
created

HasRoutesTrait::post()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 2
ccs 1
cts 1
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 Closure;
13
use WPEmerge\Controllers\WordPressController;
14
15
/**
16
 * Allow objects to have routes
17
 */
18
trait HasRoutesTrait {
19
	/**
20
	 * Array of registered routes
21
	 *
22
	 * @var RouteInterface[]
23
	 */
24
	protected $routes = [];
25
26
	/**
27
	 * Get registered routes
28
	 *
29
	 * @return RouteInterface[]
30
	 */
31 1
	public function getRoutes() {
32 1
		return $this->routes;
33
	}
34
35
	/**
36
	 * Add a route
37
	 *
38
	 * @param  RouteInterface $route
39
	 * @return RouteInterface
40
	 */
41 1
	public function addRoute( $route ) {
42 1
		$this->routes[] = $route;
43 1
		return $route;
44
	}
45
46
	/**
47
	 * Create and add a new route
48
	 *
49
	 * @param  string[] $methods
50
	 * @param  mixed $condition
51
	 * @param  string|Closure|null $handler
52
	 * @return RouteInterface
53
	 * @throws \WPEmerge\Exceptions\Exception
54
	 */
55 2
	public function route( $methods, $condition, $handler = null ) {
56 2
		if ( $handler === null ) {
57 1
			$handler = WordPressController::class . '@handle';
58 1
		}
59
60 2
		$route = new Route( $methods, $condition, $handler );
61 2
		return $this->addRoute( $route );
62
	}
63
64
	/**
65
	 * Create and add a route for the GET and HEAD methods
66
	 *
67
	 * @param  mixed               $condition
68
	 * @param  string|Closure|null $handler
69
	 * @return RouteInterface
70
	 */
71 1
	public function get( $condition, $handler = null ) {
72 1
		return $this->route( ['GET', 'HEAD'], $condition, $handler );
73 1
	}
74
75
	/**
76
	 * Create and add a route for the POST method
77
	 *
78
	 * @param  mixed               $condition
79
	 * @param  string|Closure|null $handler
80
	 * @return RouteInterface
81
	 */
82
	public function post( $condition, $handler = null ) {
83 1
		return $this->route( ['POST'], $condition, $handler );
84 1
	}
85
86
	/**
87
	 * Create and add a route for the PUT method
88
	 *
89
	 * @param  mixed               $condition
90
	 * @param  string|Closure|null $handler
91
	 * @return RouteInterface
92
	 */
93
	public function put( $condition, $handler = null ) {
94 1
		return $this->route( ['PUT'], $condition, $handler );
95 1
	}
96
97
	/**
98
	 * Create and add a route for the PATCH method
99
	 *
100
	 * @param  mixed               $condition
101
	 * @param  string|Closure|null $handler
102
	 * @return RouteInterface
103
	 */
104
	public function patch( $condition, $handler = null ) {
105 1
		return $this->route( ['PATCH'], $condition, $handler );
106 1
	}
107
108
	/**
109
	 * Create and add a route for the DELETE method
110
	 *
111
	 * @param  mixed               $condition
112
	 * @param  string|Closure|null $handler
113
	 * @return RouteInterface
114
	 */
115
	public function delete( $condition, $handler = null ) {
116 1
		return $this->route( ['DELETE'], $condition, $handler );
117 1
	}
118
119
	/**
120
	 * Create and add a route for the OPTIONS method
121
	 *
122
	 * @param  mixed               $condition
123
	 * @param  string|Closure|null $handler
124
	 * @return RouteInterface
125
	 */
126
	public function options( $condition, $handler = null ) {
127 1
		return $this->route( ['OPTIONS'], $condition, $handler );
128 1
	}
129
130
	/**
131
	 * Create and add a route for all supported methods
132
	 *
133
	 * @param  mixed               $condition
134
	 * @param  string|Closure|null $handler
135
	 * @return RouteInterface
136
	 */
137
	public function any( $condition, $handler = null ) {
138 1
		return $this->route( ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], $condition, $handler );
139 1
	}
140
}
141