Route::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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