Passed
Pull Request — master (#32)
by Anatoly
02:32
created

Route::buildRegex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 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
use Psr\Http\Server\RequestHandlerInterface;
19
20
/**
21
 * Import functions
22
 */
23
use function strtoupper;
24
25
/**
26
 * Route
27
 */
28
class Route implements RouteInterface
29
{
30
31
    /**
32
     * The route name
33
     *
34
     * @var string
35
     */
36
    private $name;
37
38
    /**
39
     * The route path
40
     *
41
     * @var string
42
     */
43
    private $path;
44
45
    /**
46
     * The route methods
47
     *
48
     * @var string[]
49
     */
50
    private $methods;
51
52
    /**
53
     * The route request handler
54
     *
55
     * @var RequestHandlerInterface
56
     */
57
    private $requestHandler;
58
59
    /**
60
     * The route middlewares
61
     *
62
     * @var MiddlewareInterface[]
63
     */
64
    private $middlewares = [];
65
66
    /**
67
     * The route attributes
68
     *
69
     * @var array
70
     */
71
    private $attributes = [];
72
73
    /**
74
     * Constructor of the class
75
     *
76
     * @param string $name
77
     * @param string $path
78
     * @param string[] $methods
79
     * @param RequestHandlerInterface $requestHandler
80
     * @param MiddlewareInterface[] $middlewares
81
     * @param array $attributes
82
     */
83 58
    public function __construct(
84
        string $name,
85
        string $path,
86
        array $methods,
87
        RequestHandlerInterface $requestHandler,
88
        array $middlewares = [],
89
        array $attributes = []
90
    ) {
91 58
        $this->setName($name);
92 58
        $this->setPath($path);
93 58
        $this->setMethods(...$methods);
94 58
        $this->setRequestHandler($requestHandler);
95 58
        $this->setMiddlewares(...$middlewares);
96 58
        $this->setAttributes($attributes);
97 58
    }
98
99
    /**
100
     * {@inheritDoc}
101
     */
102 50
    public function getName() : string
103
    {
104 50
        return $this->name;
105
    }
106
107
    /**
108
     * {@inheritDoc}
109
     */
110 37
    public function getPath() : string
111
    {
112 37
        return $this->path;
113
    }
114
115
    /**
116
     * {@inheritDoc}
117
     */
118 32
    public function getMethods() : array
119
    {
120 32
        return $this->methods;
121
    }
122
123
    /**
124
     * {@inheritDoc}
125
     */
126 26
    public function getRequestHandler() : RequestHandlerInterface
127
    {
128 26
        return $this->requestHandler;
129
    }
130
131
    /**
132
     * {@inheritDoc}
133
     */
134 34
    public function getMiddlewares() : array
135
    {
136 34
        return $this->middlewares;
137
    }
138
139
    /**
140
     * {@inheritDoc}
141
     */
142 27
    public function getAttributes() : array
143
    {
144 27
        return $this->attributes;
145
    }
146
147
    /**
148
     * {@inheritDoc}
149
     */
150 58
    public function setName(string $name) : RouteInterface
151
    {
152 58
        $this->name = $name;
153
154 58
        return $this;
155
    }
156
157
    /**
158
     * {@inheritDoc}
159
     */
160 58
    public function setPath(string $path) : RouteInterface
161
    {
162 58
        $this->path = $path;
163
164 58
        return $this;
165
    }
166
167
    /**
168
     * {@inheritDoc}
169
     */
170 58
    public function setMethods(string ...$methods) : RouteInterface
171
    {
172 58
        foreach ($methods as &$method) {
173 58
            $method = strtoupper($method);
174
        }
175
176 58
        $this->methods = $methods;
177
178 58
        return $this;
179
    }
180
181
    /**
182
     * {@inheritDoc}
183
     */
184 58
    public function setRequestHandler(RequestHandlerInterface $requestHandler) : RouteInterface
185
    {
186 58
        $this->requestHandler = $requestHandler;
187
188 58
        return $this;
189
    }
190
191
    /**
192
     * {@inheritDoc}
193
     */
194 58
    public function setMiddlewares(MiddlewareInterface ...$middlewares) : RouteInterface
195
    {
196 58
        $this->middlewares = $middlewares;
197
198 58
        return $this;
199
    }
200
201
    /**
202
     * {@inheritDoc}
203
     */
204 58
    public function setAttributes(array $attributes) : RouteInterface
205
    {
206 58
        $this->attributes = $attributes;
207
208 58
        return $this;
209
    }
210
211
    /**
212
     * {@inheritDoc}
213
     */
214 4
    public function withAddedAttributes(array $attributes) : RouteInterface
215
    {
216 4
        $clone = clone $this;
217
218 4
        foreach ($attributes as $key => $value) {
219 1
            $clone->attributes[$key] = $value;
220
        }
221
222 4
        return $clone;
223
    }
224
}
225