Test Failed
Pull Request — master (#225)
by Alexander
12:36
created

RouteBuilder::toRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 12
rs 9.9332
c 1
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router\Builder;
6
7
use Stringable;
8
use Yiisoft\Http\Method;
9
use Yiisoft\Router\Group;
10
use Yiisoft\Router\RoutableInterface;
11
use Yiisoft\Router\Route;
12
13
/**
14
 * RouteBuilder allows you to build routes using a flexible syntax.
15
 */
16
final class RouteBuilder implements RoutableInterface
17
{
18
    private ?string $name = null;
19
20
    /**
21
     * @var array|callable|string|null
22
     */
23
    private $action = null;
24
25
    /**
26
     * @var string[]
27
     */
28
    private array $hosts = [];
29
30
    private bool $override = false;
31
32
    private array $disabledMiddlewares = [];
33
34
    /**
35
     * @var array[]|callable[]|string[]
36
     * @psalm-var list<array|callable|string>
37
     */
38
    private array $middlewares = [];
39
40
    /**
41
     * @var array<array-key,scalar|Stringable|null>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key,scalar|Stringable|null> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key,scalar|Stringable|null>.
Loading history...
42
     */
43
    private array $defaults = [];
44
45
    /**
46
     * @param string[] $methods
47
     */
48
    private function __construct(
49
        private array $methods,
50
        private string $pattern,
51
    ) {
52
    }
53
54
    public static function get(string $pattern): self
55
    {
56
        return self::methods([Method::GET], $pattern);
57
    }
58
59
    public static function post(string $pattern): self
60
    {
61
        return self::methods([Method::POST], $pattern);
62
    }
63
64
    public static function put(string $pattern): self
65
    {
66
        return self::methods([Method::PUT], $pattern);
67
    }
68
69
    public static function delete(string $pattern): self
70
    {
71
        return self::methods([Method::DELETE], $pattern);
72
    }
73
74
    public static function patch(string $pattern): self
75
    {
76
        return self::methods([Method::PATCH], $pattern);
77
    }
78
79
    public static function head(string $pattern): self
80
    {
81
        return self::methods([Method::HEAD], $pattern);
82
    }
83
84
    public static function options(string $pattern): self
85
    {
86
        return self::methods([Method::OPTIONS], $pattern);
87
    }
88
89
    /**
90
     * @param string[] $methods
91
     */
92
    public static function methods(array $methods, string $pattern): self
93
    {
94
        return new self(methods: $methods, pattern: $pattern);
95
    }
96
97
    public function name(string $name): self
98
    {
99
        $route = clone $this;
100
        $route->name = $name;
101
        return $route;
102
    }
103
104
    public function pattern(string $pattern): self
105
    {
106
        $new = clone $this;
107
        $new->pattern = $pattern;
108
        return $new;
109
    }
110
111
    public function host(string $host): self
112
    {
113
        return $this->hosts($host);
114
    }
115
116
    public function hosts(string ...$hosts): self
117
    {
118
        $route = clone $this;
119
        $route->hosts = array_values($hosts);
120
121
        return $route;
122
    }
123
124
    /**
125
     * Marks route as override. When added it will replace existing route with the same name.
126
     */
127
    public function override(): self
128
    {
129
        $route = clone $this;
130
        $route->override = true;
131
        return $route;
132
    }
133
134
    /**
135
     * Parameter default values indexed by parameter names.
136
     *
137
     * @psalm-param array<array-key,null|Stringable|scalar> $defaults
138
     */
139
    public function defaults(array $defaults): self
140
    {
141
        $route = clone $this;
142
        $route->defaults = $defaults;
143
        return $route;
144
    }
145
146
    /**
147
     * Appends a handler middleware definition that should be invoked for a matched route.
148
     * First added handler will be executed first.
149
     */
150
    public function middleware(array|callable|string ...$definition): self
151
    {
152
        $route = clone $this;
153
        array_push(
154
            $route->middlewares,
155
            ...array_values($definition)
156
        );
157
158
        return $route;
159
    }
160
161
    /**
162
     * Prepends a handler middleware definition that should be invoked for a matched route.
163
     * Last added handler will be executed first.
164
     */
165
    public function prependMiddleware(array|callable|string ...$definition): self
166
    {
167
        $route = clone $this;
168
        array_unshift(
169
            $route->middlewares,
170
            ...array_values($definition)
171
        );
172
173
        return $route;
174
    }
175
176
    /**
177
     * Appends action handler. It is a primary middleware definition that should be invoked last for a matched route.
178
     */
179
    public function action(array|callable|string $middlewareDefinition): self
180
    {
181
        $route = clone $this;
182
        $route->action = $middlewareDefinition;
183
        return $route;
184
    }
185
186
    /**
187
     * Excludes middleware from being invoked when action is handled.
188
     * It is useful to avoid invoking one of the parent group middleware for
189
     * a certain route.
190
     */
191
    public function disableMiddleware(mixed ...$definition): self
192
    {
193
        $route = clone $this;
194
        array_push(
195
            $route->disabledMiddlewares,
196
            ...array_values($definition)
197
        );
198
199
        return $route;
200
    }
201
202
    public function toRoute(): Group|Route
203
    {
204
        return new Route(
205
            methods: $this->methods,
206
            pattern: $this->pattern,
207
            name: $this->name,
208
            action: $this->action,
209
            middlewares: $this->middlewares,
210
            defaults: $this->defaults,
211
            hosts: $this->hosts,
212
            override: $this->override,
213
            disabledMiddlewares: $this->disabledMiddlewares
214
        );
215
    }
216
}
217