Passed
Pull Request — master (#225)
by Rustam
11:11 queued 08:15
created

RouteBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
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 71
    private function __construct(
49
        private array $methods,
50
        private string $pattern,
51
    ) {
52 71
    }
53
54 59
    public static function get(string $pattern): self
55
    {
56 59
        return self::methods([Method::GET], $pattern);
57
    }
58
59 9
    public static function post(string $pattern): self
60
    {
61 9
        return self::methods([Method::POST], $pattern);
62
    }
63
64 4
    public static function put(string $pattern): self
65
    {
66 4
        return self::methods([Method::PUT], $pattern);
67
    }
68
69 1
    public static function delete(string $pattern): self
70
    {
71 1
        return self::methods([Method::DELETE], $pattern);
72
    }
73
74 1
    public static function patch(string $pattern): self
75
    {
76 1
        return self::methods([Method::PATCH], $pattern);
77
    }
78
79 1
    public static function head(string $pattern): self
80
    {
81 1
        return self::methods([Method::HEAD], $pattern);
82
    }
83
84 4
    public static function options(string $pattern): self
85
    {
86 4
        return self::methods([Method::OPTIONS], $pattern);
87
    }
88
89
    /**
90
     * @param string[] $methods
91
     */
92 71
    public static function methods(array $methods, string $pattern): self
93
    {
94 71
        return new self(methods: $methods, pattern: $pattern);
95
    }
96
97 22
    public function name(string $name): self
98
    {
99 22
        $route = clone $this;
100 22
        $route->name = $name;
101 22
        return $route;
102
    }
103
104 2
    public function pattern(string $pattern): self
105
    {
106 2
        $new = clone $this;
107 2
        $new->pattern = $pattern;
108 2
        return $new;
109
    }
110
111 9
    public function host(string $host): self
112
    {
113 9
        return $this->hosts($host);
114
    }
115
116 11
    public function hosts(string ...$hosts): self
117
    {
118 11
        $route = clone $this;
119 11
        $route->hosts = array_values($hosts);
120
121 11
        return $route;
122
    }
123
124
    /**
125
     * Marks route as override. When added it will replace existing route with the same name.
126
     */
127 3
    public function override(): self
128
    {
129 3
        $route = clone $this;
130 3
        $route->override = true;
131 3
        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 4
    public function defaults(array $defaults): self
140
    {
141 4
        $route = clone $this;
142 4
        $route->defaults = $defaults;
143 4
        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 19
    public function middleware(array|callable|string ...$definition): self
151
    {
152 19
        $route = clone $this;
153 19
        array_push(
154 19
            $route->middlewares,
155 19
            ...array_values($definition)
156 19
        );
157
158 19
        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 4
    public function prependMiddleware(array|callable|string ...$definition): self
166
    {
167 4
        $route = clone $this;
168 4
        array_unshift(
169 4
            $route->middlewares,
170 4
            ...array_values($definition)
171 4
        );
172
173 4
        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 28
    public function action(array|callable|string $middlewareDefinition): self
180
    {
181 28
        $route = clone $this;
182 28
        $route->action = $middlewareDefinition;
183 28
        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 5
    public function disableMiddleware(mixed ...$definition): self
192
    {
193 5
        $route = clone $this;
194 5
        array_push(
195 5
            $route->disabledMiddlewares,
196 5
            ...array_values($definition)
197 5
        );
198
199 5
        return $route;
200
    }
201
202 64
    public function toRoute(): Group|Route
203
    {
204 64
        return new Route(
205 64
            methods: $this->methods,
206 64
            pattern: $this->pattern,
207 64
            name: $this->name,
208 64
            action: $this->action,
209 64
            middlewares: $this->middlewares,
210 64
            defaults: $this->defaults,
211 64
            hosts: $this->hosts,
212 64
            override: $this->override,
213 64
            disabledMiddlewares: $this->disabledMiddlewares
214 64
        );
215
    }
216
}
217