Completed
Push — master ( fc0f8c...4e267c )
by Alexander
30:35 queued 15:36
created

Route::__set_state()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
namespace Yiisoft\Router;
3
4
use Psr\Http\Message\ResponseInterface;
5
use Psr\Http\Message\ServerRequestInterface;
6
use Psr\Http\Server\MiddlewareInterface;
7
use Psr\Http\Server\RequestHandlerInterface;
8
9
/**
10
 * Route defines a mapping from URL to callback / name and vice versa
11
 */
12
class Route implements MiddlewareInterface
13
{
14
    /**
15
     * @var string
16
     */
17
    private $name;
18
19
    /**
20
     * @var array
21
     */
22
    private $methods;
23
24
    /**
25
     * @var string
26
     */
27
    private $pattern;
28
29
    /**
30
     * @var string
31
     */
32
    private $host;
33
34
    /**
35
     * @var MiddlewareInterface
36
     */
37
    private $middleware;
38
39
    /**
40
     * @var array
41
     */
42
    private $parameters = [];
43
44
    /**
45
     * @var array
46
     */
47
    private $defaults = [];
48
49
    private function __construct()
50
    {
51
    }
52
53
    public static function get(string $pattern): self
54
    {
55
        $new = new static();
56
        $new->methods = [Method::GET];
57
        $new->pattern = $pattern;
58
        return $new;
59
    }
60
61
    public static function post(string $pattern): self
62
    {
63
        $new = new static();
64
        $new->methods = [Method::POST];
65
        $new->pattern = $pattern;
66
        return $new;
67
    }
68
69
    public static function put(string $pattern): self
70
    {
71
        $new = new static();
72
        $new->methods = [Method::PUT];
73
        $new->pattern = $pattern;
74
        return $new;
75
    }
76
77
    public static function delete(string $pattern): self
78
    {
79
        $new = new static();
80
        $new->methods = [Method::DELETE];
81
        $new->pattern = $pattern;
82
        return $new;
83
    }
84
85
    public static function patch(string $pattern): self
86
    {
87
        $new = new static();
88
        $new->methods = [Method::PATCH];
89
        $new->pattern = $pattern;
90
        return $new;
91
    }
92
93
    public static function head(string $pattern): self
94
    {
95
        $new = new static();
96
        $new->methods = [Method::HEAD];
97
        $new->pattern = $pattern;
98
        return $new;
99
    }
100
101
    public static function options(string $pattern): self
102
    {
103
        $new = new static();
104
        $new->methods = [Method::OPTIONS];
105
        $new->pattern = $pattern;
106
        return $new;
107
    }
108
109
    public static function methods(array $methods, string $pattern): self
110
    {
111
        $new = new static();
112
        $new->methods = $methods;
113
        $new->pattern = $pattern;
114
        return $new;
115
    }
116
117
    public static function anyMethod(string $pattern): self
118
    {
119
        $new = new static();
120
        $new->methods = Method::ANY;
121
        $new->pattern = $pattern;
122
        return $new;
123
    }
124
125
    public function name(string $name): self
126
    {
127
        $new = clone $this;
128
        $new->name = $name;
129
        return $new;
130
    }
131
132
    public function host(string $host): self
133
    {
134
        $new = clone $this;
135
        $new->host = rtrim($host, '/');
136
        return $new;
137
    }
138
139
    /**
140
     * Parameter validation rules indexed by parameter names
141
     *
142
     * @param array $parameters
143
     * @return Route
144
     */
145
    public function parameters(array $parameters): self
146
    {
147
        $new = clone $this;
148
        $new->parameters = $parameters;
149
        return $new;
150
    }
151
152
    /**
153
     * Parameter default values indexed by parameter names
154
     *
155
     * @param array $defaults
156
     * @return Route
157
     */
158
    public function defaults(array $defaults): self
159
    {
160
        $new = clone $this;
161
        $new->defaults = $defaults;
162
        return $new;
163
    }
164
165
    /**
166
     * Speicifes a handler that should be invoked for a matching route.
167
     * It can be either a PSR middleware or a callable with the following signature:
168
     *
169
     * ```
170
     * function (ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
171
     * ```
172
     *
173
     * @param MiddlewareInterface|callable $middleware
174
     * @return Route
175
     */
176
    public function to($middleware): self
177
    {
178
        $new = clone $this;
179
        if (is_callable($middleware)) {
180
            $middleware = $this->wrapCallable($middleware);
181
        }
182
183
        if (!$middleware instanceof MiddlewareInterface) {
184
            throw new \InvalidArgumentException('Parameter should be either a PSR middleware or a callable.');
185
        }
186
187
        $new->middleware = $middleware;
188
        return $new;
189
    }
190
191
    private function wrapCallable(callable $callback): MiddlewareInterface
192
    {
193
        return new class($callback) implements MiddlewareInterface {
194
            private $callback;
195
196
            public function __construct(callable $callback)
197
            {
198
                $this->callback = $callback;
199
            }
200
201
            public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
202
            {
203
                return \call_user_func($this->callback, $request, $handler);
204
            }
205
        };
206
    }
207
208
    public function __toString()
209
    {
210
        $result = '';
211
212
        if ($this->name !== null) {
213
            $result .= '[' . $this->name . '] ';
214
        }
215
216
        if ($this->methods !== null) {
217
            $result .= implode(',', $this->methods) . ' ';
218
        }
219
        if ($this->host !== null && strrpos($this->pattern, $this->host) === false) {
220
            $result .= $this->host;
221
        }
222
        $result .= $this->pattern;
223
224
        return $result;
225
    }
226
227
    public function getName(): string
228
    {
229
        return $this->name ?? implode(', ', $this->methods) . ' ' . $this->pattern;
230
    }
231
232
    public function getMethods(): array
233
    {
234
        return $this->methods;
235
    }
236
237
    public function getPattern(): string
238
    {
239
        return $this->pattern;
240
    }
241
242
    public function getHost(): ?string
243
    {
244
        return $this->host;
245
    }
246
247
    public function getParameters(): array
248
    {
249
        return $this->parameters;
250
    }
251
252
    public function getDefaults(): array
253
    {
254
        return $this->defaults;
255
    }
256
257
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
258
    {
259
        return $this->middleware->process($request, $handler);
260
    }
261
262
    public static function __set_state(array $properties): self
263
    {
264
        $new = new self();
265
        $new->name = $properties['name'];
266
        $new->methods = $properties['methods'];
267
        $new->pattern = $properties['pattern'];
268
        $new->host = $properties['host'];
269
        $new->middleware = $properties['middleware'];
270
        $new->parameters = $properties['parameters'];
271
        $new->defaults = $properties['defaults'];
272
        return $new;
273
    }
274
}
275