Completed
Pull Request — master (#8)
by Dawid
02:24
created

Route::head()   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
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Igni\Http;
4
5
use Symfony\Component\Routing\Route as BaseRoute;
6
7
/**
8
 * Proxy class for symfony's route.
9
 *
10
 * @package Igni\Http
11
 */
12
class Route
13
{
14
    /**
15
     * @var BaseRoute
16
     */
17
    private $baseRoute;
18
19
    /**
20
     * @var string
21
     */
22
    private $name;
23
24
    /**
25
     * @var array
26
     */
27
    private $requirements = [];
28
29
    /**
30
     * @var array
31
     */
32
    private $defaults = [];
33
34
    /**
35
     * @var array
36
     */
37
    private $attributes = [];
38
39
    /**
40
     * Route constructor.
41
     *
42
     * @param string $path
43
     * @param string $name
44
     * @param array $methods
45
     */
46 17
    public function __construct(string $path, array $methods = ['GET'], string $name = null)
47
    {
48 17
        $this->name = $name ?? self::generateNameFromPath($path);
49 17
        $this->baseRoute = new BaseRoute($path);
50 17
        $this->baseRoute->setMethods($methods);
51 17
    }
52
53 1
    public function setDefaults(array $defaults): self
54
    {
55 1
        $this->defaults = $defaults;
56
57 1
        return $this;
58
    }
59
60 1
    public function setName(string $name): self
61
    {
62 1
        $this->name = $name;
63
64 1
        return $this;
65
    }
66
67 14
    public function getName(): string
68
    {
69 14
        return $this->name;
70
    }
71
72
    /**
73
     * Sets controller that is going to handle all request
74
     * which uri matches the route pattern.
75
     *
76
     * @param string|callable $handler
77
     * @return $this
78
     */
79 11
    public function delegate($handler): self
80
    {
81 11
        $this->defaults['-controller'] = $handler;
82
83 11
        return $this;
84
    }
85
86
    /**
87
     * Returns route pattern.
88
     *
89
     * @return string
90
     */
91 2
    public function getPath(): string
92
    {
93 2
        return $this->baseRoute->getPath();
94
    }
95
96
    /**
97
     * Returns request methods.
98
     *
99
     * @return array
100
     */
101
    public function getMethods(): array
102
    {
103
        return $this->baseRoute->getMethods();
104
    }
105
106
    /**
107
     * Returns controller that listen to request
108
     * which uri matches the route pattern.
109
     *
110
     * @return callable|string|null
111
     */
112 9
    public function getDelegator()
113
    {
114 9
        return $this->defaults['-controller'] ?? null;
115
    }
116
117
    /**
118
     * Factories new instance of the current route with
119
     * attributes retrieved from client's request.
120
     *
121
     * @param array $attributes
122
     * @return Route
123
     */
124 10
    public function withAttributes(array $attributes): Route
125
    {
126 10
        $route = clone $this;
127 10
        $route->attributes = $attributes;
128
129 10
        return $route;
130
    }
131
132
    /**
133
     * @internal
134
     * @return BaseRoute
135
     */
136 13
    public function getBaseRoute(): BaseRoute
137
    {
138 13
        $this->baseRoute->setDefaults($this->defaults);
139 13
        $this->baseRoute->setRequirements($this->requirements);
140
141 13
        return $this->baseRoute;
142
    }
143
144
    /**
145
     * Returns attributes extracted from the uri.
146
     *
147
     * @return array
148
     */
149 10
    public function getAttributes(): array
150
    {
151 10
        return $this->attributes;
152
    }
153
154
    /**
155
     * Factories new instance of the route
156
     * that will be matched against get request.
157
     *
158
     * @param string $path
159
     * @param string $name
160
     * @return Route
161
     */
162 10
    public static function get(string $path, string $name = null): Route
163
    {
164 10
        return new self($path, [Request::METHOD_GET], $name ?? self::generateNameFromPath($path));
165
    }
166
167
    /**
168
     * Factories new instance of the route
169
     * that will be matched against post request.
170
     *
171
     * @param string $path
172
     * @param string $name
173
     * @return Route
174
     */
175 2
    public static function post(string $path, string $name = null): Route
176
    {
177 2
        return new self($path, [Request::METHOD_POST], $name ?? self::generateNameFromPath($path));
178
    }
179
180
    /**
181
     * Factories new instance of the route
182
     * that will be matched against put request.
183
     *
184
     * @param string $path
185
     * @param string $name
186
     * @return Route
187
     */
188 1
    public static function put(string $path, string $name = null): Route
189
    {
190 1
        return new self($path, [Request::METHOD_PUT], $name ?? self::generateNameFromPath($path));
191
    }
192
193
    /**
194
     * Factories new instance of the route
195
     * that will be matched against delete request.
196
     *
197
     * @param string $path
198
     * @param string $name
199
     * @return Route
200
     */
201 1
    public static function delete(string $path, string $name = null): Route
202
    {
203 1
        return new self($path, [Request::METHOD_DELETE], $name ?? self::generateNameFromPath($path));
204
    }
205
206
    /**
207
     * Factories new instance of the route
208
     * that will be matched against patch request.
209
     *
210
     * @param string $path
211
     * @param string $name
212
     * @return Route
213
     */
214 1
    public static function patch(string $path, string $name = null): Route
215
    {
216 1
        return new self($path, [Request::METHOD_PATCH], $name ?? self::generateNameFromPath($path));
217
    }
218
219
    /**
220
     * Factories new instance of the route
221
     * that will be matched against head request.
222
     *
223
     * @param string $path
224
     * @param string $name
225
     * @return Route
226
     */
227 1
    public static function head(string $path, string $name = null): Route
228
    {
229 1
        return new self($path, [Request::METHOD_HEAD, Request::METHOD_GET], $name ?? self::generateNameFromPath($path));
230
    }
231
232
    /**
233
     * Factories new instance of the route
234
     * that will be matched against options request.
235
     *
236
     * @param string $path
237
     * @param string $name
238
     * @return Route
239
     */
240 1
    public static function options(string $path, string $name = null): Route
241
    {
242 1
        return new self($path, [Request::METHOD_OPTIONS], $name ?? self::generateNameFromPath($path));
243
    }
244
245
    /**
246
     * Generates default name from given path expression,
247
     * /some/{resource} becomes some_resource
248
     * @param string $path
249
     * @return string
250
     */
251 17
    private static function generateNameFromPath(string $path): string
252
    {
253 17
        $path = preg_replace('/\<[^\>]+\>/', '', $path);
254 17
        return str_replace(['{', '}', '?', '.', '/'], ['', '', '', '_', '_'], trim($path, '/'));
255
    }
256
}
257