Passed
Pull Request — master (#22)
by
unknown
01:40
created

Route   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 200
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 200
ccs 45
cts 45
cp 1
rs 10
wmc 17

16 Methods

Rating   Name   Duplication   Size   Complexity  
A addMethod() 0 5 1
A addSuffix() 0 5 1
A addMiddleware() 0 5 1
A addPrefix() 0 5 1
A setPath() 0 5 1
A setId() 0 5 1
A addPattern() 0 5 1
A getPath() 0 3 1
A getMethods() 0 3 1
A getId() 0 3 1
A getPatterns() 0 3 1
A getMiddlewareStack() 0 3 1
A getAttributes() 0 3 1
A __construct() 0 9 2
A withAttributes() 0 7 1
A buildRegex() 0 3 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Fenric <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Fenric
8
 * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-router
10
 */
11
12
namespace Sunrise\Http\Router;
13
14
/**
15
 * Import classes
16
 */
17
use Psr\Http\Server\MiddlewareInterface;
18
19
/**
20
 * Route
21
 */
22
class Route implements RouteInterface
23
{
24
25
	/**
26
	 * The route ID
27
	 *
28
	 * @var string
29
	 */
30
	protected $id;
31
32
	/**
33
	 * The route path
34
	 *
35
	 * @var string
36
	 */
37
	protected $path;
38
39
	/**
40
	 * The route methods
41
	 *
42
	 * @var string[]
43
	 */
44
	protected $methods = [];
45
46
	/**
47
	 * The route patterns
48
	 *
49
	 * @var array
50
	 */
51
	protected $patterns = [];
52
53
	/**
54
	 * The route attributes
55
	 *
56
	 * @var array
57
	 */
58
	protected $attributes = [];
59
60
	/**
61
	 * The route middleware stack
62
	 *
63
	 * @var MiddlewareInterface[]
64
	 */
65
	protected $middlewareStack = [];
66
67
	/**
68
	 * Constructor of the class
69
	 *
70
	 * @param string $id
71
	 * @param string $path
72
	 * @param string[] $methods
73
	 */
74 49
	public function __construct(string $id, string $path, array $methods)
75
	{
76 49
		$this->setId($id);
77
78 49
		$this->setPath($path);
79
80 49
		foreach ($methods as $method)
81
		{
82 25
			$this->addMethod($method);
83
		}
84 49
	}
85
86
	/**
87
	 * {@inheritDoc}
88
	 */
89 49
	public function setId(string $id) : RouteInterface
90
	{
91 49
		$this->id = $id;
92
93 49
		return $this;
94
	}
95
96
	/**
97
	 * {@inheritDoc}
98
	 */
99 49
	public function setPath(string $path) : RouteInterface
100
	{
101 49
		$this->path = $path;
102
103 49
		return $this;
104
	}
105
106
	/**
107
	 * {@inheritDoc}
108
	 */
109 3
	public function addPrefix(string $prefix) : RouteInterface
110
	{
111 3
		$this->path = $prefix . $this->path;
112
113 3
		return $this;
114
	}
115
116
	/**
117
	 * {@inheritDoc}
118
	 */
119 2
	public function addSuffix(string $suffix) : RouteInterface
120
	{
121 2
		$this->path .= $suffix;
122
123 2
		return $this;
124
	}
125
126
	/**
127
	 * {@inheritDoc}
128
	 */
129 25
	public function addMethod(string $method) : RouteInterface
130
	{
131 25
		$this->methods[] = \strtoupper($method);
132
133 25
		return $this;
134
	}
135
136
	/**
137
	 * {@inheritDoc}
138
	 */
139 4
	public function addPattern(string $name, string $value) : RouteInterface
140
	{
141 4
		$this->patterns[$name] = $value;
142
143 4
		return $this;
144
	}
145
146
	/**
147
	 * {@inheritDoc}
148
	 */
149 3
	public function addMiddleware(MiddlewareInterface $middleware) : RouteInterface
150
	{
151 3
		$this->middlewareStack[] = $middleware;
152
153 3
		return $this;
154
	}
155
156
	/**
157
	 * {@inheritDoc}
158
	 */
159 13
	public function getId() : string
160
	{
161 13
		return $this->id;
162
	}
163
164
	/**
165
	 * {@inheritDoc}
166
	 */
167 17
	public function getPath() : string
168
	{
169 17
		return $this->path;
170
	}
171
172
	/**
173
	 * {@inheritDoc}
174
	 */
175 22
	public function getMethods() : array
176
	{
177 22
		return $this->methods;
178
	}
179
180
	/**
181
	 * {@inheritDoc}
182
	 */
183 3
	public function getPatterns() : array
184
	{
185 3
		return $this->patterns;
186
	}
187
188
	/**
189
	 * {@inheritDoc}
190
	 */
191 8
	public function getAttributes() : array
192
	{
193 8
		return $this->attributes;
194
	}
195
196
	/**
197
	 * {@inheritDoc}
198
	 */
199 4
	public function getMiddlewareStack() : array
200
	{
201 4
		return $this->middlewareStack;
202
	}
203
204
	/**
205
	 * {@inheritDoc}
206
	 */
207 9
	public function withAttributes(array $attributes) : RouteInterface
208
	{
209 9
		$clone = clone $this;
210
211 9
		$clone->attributes = \array_merge($clone->attributes, $attributes);
212
213 9
		return $clone;
214
	}
215
216
	/**
217
	 * {@inheritDoc}
218
	 */
219 10
	public function buildRegex() : string
220
	{
221 10
		return route_regex($this->path, $this->patterns);
0 ignored issues
show
Bug introduced by
The function route_regex was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

221
		return /** @scrutinizer ignore-call */ route_regex($this->path, $this->patterns);
Loading history...
222
	}
223
}
224