Passed
Push — master ( 8df7ad...310ad9 )
by Divine Niiquaye
07:56
created

RouteTrait::setPatterns()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Flight Routing.
7
 *
8
 * PHP version 7.1 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Flight\Routing\Traits;
19
20
use Flight\Routing\Interfaces\RouteInterface;
21
22
trait RouteTrait
23
{
24
    /** @var string[] */
25
    private $methods = [];
26
27
    /** @var string */
28
    private $path;
29
30
    /** @var null|string */
31
    private $domain;
32
33
    /** @var string */
34
    private $name;
35
36
    /** @var string[] */
37
    private $schemes = [];
38
39
    /** @var array<int|string,mixed> */
40
    private $arguments = [];
41
42
    /** @var array<string,mixed> */
43
    private $defaults = [];
44
45
    /** @var array<string,string|string[]> */
46
    private $patterns = [];
47
48
    /** @var array<int,mixed> */
49
    private $middlewares = [];
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 120
    public function getPath(): string
55
    {
56 120
        return $this->path;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 15
    public function addPrefix(string $prefix): RouteInterface
63
    {
64 15
        $this->path = $this->castPrefix($this->path, $prefix);
0 ignored issues
show
Bug introduced by
It seems like castPrefix() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        /** @scrutinizer ignore-call */ 
65
        $this->path = $this->castPrefix($this->path, $prefix);
Loading history...
65
66 15
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Flight\Routing\Traits\RouteTrait which is incompatible with the type-hinted return Flight\Routing\Interfaces\RouteInterface.
Loading history...
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 81
    public function getMethods(): array
73
    {
74 81
        return $this->methods;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 8
    public function addMethod(string ...$methods): RouteInterface
81
    {
82 8
        foreach ($methods as $method) {
83 4
            $this->methods[] = \strtoupper($method);
84
        }
85
86 8
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Flight\Routing\Traits\RouteTrait which is incompatible with the type-hinted return Flight\Routing\Interfaces\RouteInterface.
Loading history...
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 88
    public function getName(): string
93
    {
94 88
        return $this->name;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 13
    public function setName(string $name): RouteInterface
101
    {
102 13
        $this->name = $name;
103
104 13
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Flight\Routing\Traits\RouteTrait which is incompatible with the type-hinted return Flight\Routing\Interfaces\RouteInterface.
Loading history...
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 95
    public function getDomain(): string
111
    {
112 95
        return \str_replace(['http://', 'https://'], '', (string) $this->domain);
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118 24
    public function setDomain(string $domain): RouteInterface
119
    {
120 24
        if (false !== \preg_match('@^(?:(https?):)?(\/\/[^/]+)@i', $domain, $matches)) {
121 24
            if (empty($matches)) {
122 5
                $matches = [$domain, null, $domain];
123
            }
124
125 24
            [, $scheme, $domain] = $matches;
126
127 24
            if (!empty($scheme)) {
128 16
                $this->setScheme($scheme);
129
            }
130
        }
131 24
        $this->domain = \trim((string) $domain, '//');
132
133 24
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Flight\Routing\Traits\RouteTrait which is incompatible with the type-hinted return Flight\Routing\Interfaces\RouteInterface.
Loading history...
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139 70
    public function getSchemes(): array
140
    {
141 70
        return $this->schemes;
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147 19
    public function setScheme(string ...$schemes): RouteInterface
148
    {
149 19
        foreach ($schemes as $scheme) {
150 18
            $this->schemes[] = $scheme;
151
        }
152
153 19
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Flight\Routing\Traits\RouteTrait which is incompatible with the type-hinted return Flight\Routing\Interfaces\RouteInterface.
Loading history...
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159 52
    public function getArguments(): array
160
    {
161 52
        $arguments = [];
162
163 52
        foreach ($this->arguments as $key => $value) {
164 19
            if (\is_int($key)) {
165 3
                continue;
166
            }
167 18
            $value = \is_numeric($value) ? (int) $value : $value;
168
169 18
            $arguments[$key] = \is_string($value) ? \rawurldecode($value) : $value;
170
        }
171
172 52
        return $arguments;
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178 57
    public function setArguments(array $arguments): RouteInterface
179
    {
180 57
        foreach ($arguments as $key => $value) {
181 32
            $this->arguments[$key] = $value;
182
        }
183
184 57
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Flight\Routing\Traits\RouteTrait which is incompatible with the type-hinted return Flight\Routing\Interfaces\RouteInterface.
Loading history...
185
    }
186
187
    /**
188
     * {@inheritdoc}
189
     */
190 90
    public function getDefaults(): array
191
    {
192 90
        return $this->defaults;
193
    }
194
195
    /**
196
     * {@inheritdoc}
197
     */
198 62
    public function setDefaults(array $defaults): RouteInterface
199
    {
200 62
        foreach ($defaults as $key => $value) {
201 61
            $this->defaults[$key] = $value;
202
        }
203
204 62
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Flight\Routing\Traits\RouteTrait which is incompatible with the type-hinted return Flight\Routing\Interfaces\RouteInterface.
Loading history...
205
    }
206
207
    /**
208
     * {@inheritdoc}
209
     */
210 11
    public function addPattern(string $name, $expression): RouteInterface
211
    {
212 11
        $this->patterns[$name] = $expression;
213
214 11
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Flight\Routing\Traits\RouteTrait which is incompatible with the type-hinted return Flight\Routing\Interfaces\RouteInterface.
Loading history...
215
    }
216
217
    /**
218
     * {@inheritdoc}
219
     */
220 54
    public function getPatterns(): array
221
    {
222 54
        return $this->patterns;
223
    }
224
225
    /**
226
     * {@inheritdoc}
227
     */
228 7
    public function setPatterns(array $patterns): RouteInterface
229
    {
230 7
        foreach ($patterns as $key => $expression) {
231 6
            $this->addPattern($key, $expression);
232
        }
233
234 7
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Flight\Routing\Traits\RouteTrait which is incompatible with the type-hinted return Flight\Routing\Interfaces\RouteInterface.
Loading history...
235
    }
236
237
    /**
238
     * {@inheritdoc}
239
     */
240 71
    public function getMiddlewares(): array
241
    {
242 71
        return $this->middlewares;
243
    }
244
245
    /**
246
     * {@inheritdoc}
247
     */
248 68
    public function addMiddleware(...$middlewares): RouteInterface
249
    {
250 68
        foreach ($middlewares as $middleware) {
251 67
            $this->middlewares[] = $middleware;
252
        }
253
254 68
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Flight\Routing\Traits\RouteTrait which is incompatible with the type-hinted return Flight\Routing\Interfaces\RouteInterface.
Loading history...
255
    }
256
}
257