Passed
Push — master ( 02c0f4...848e78 )
by Divine Niiquaye
08:42
created

CastingTrait::castPrefix()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 5
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 12
ccs 6
cts 6
cp 1
crap 4
rs 10
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 Closure;
21
use Flight\Routing\Exceptions\InvalidControllerException;
22
23
trait CastingTrait
24
{
25
    /**
26
     * @internal
27
     *
28
     * @return array<string,mixed>
29
     */
30 1
    public function __serialize(): array
31
    {
32
        return [
33 1
            'name'          => $this->name,
34 1
            'path'          => $this->path,
35 1
            'host'          => $this->domain,
36 1
            'schemes'       => $this->schemes,
37 1
            'defaults'      => $this->defaults,
38 1
            'patterns'      => $this->patterns,
39 1
            'methods'       => $this->methods,
40 1
            'middlewares'   => $this->middlewares,
41 1
            'arguments'     => $this->arguments,
42 1
            'handler'       => $this->controller instanceof Closure ? [$this, 'getController'] : $this->controller,
43
        ];
44
    }
45
46
    /**
47
     * @internal
48
     *
49
     * @param array<string,mixed> $data
50
     */
51 1
    public function __unserialize(array $data): void
52
    {
53 1
        $this->name          = $data['name'];
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
54 1
        $this->path          = $data['path'];
0 ignored issues
show
Bug Best Practice introduced by
The property path does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
55 1
        $this->domain        = $data['host'];
0 ignored issues
show
Bug Best Practice introduced by
The property domain does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
56 1
        $this->defaults      = $data['defaults'];
0 ignored issues
show
Bug Best Practice introduced by
The property defaults does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
57 1
        $this->schemes       = $data['schemes'];
0 ignored issues
show
Bug Best Practice introduced by
The property schemes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
58 1
        $this->patterns      = $data['patterns'];
0 ignored issues
show
Bug Best Practice introduced by
The property patterns does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
59 1
        $this->methods       = $data['methods'];
0 ignored issues
show
Bug Best Practice introduced by
The property methods does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
60 1
        $this->controller    = $data['handler'];
0 ignored issues
show
Bug Best Practice introduced by
The property controller does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
61 1
        $this->middlewares   = $data['middlewares'];
0 ignored issues
show
Bug Best Practice introduced by
The property middlewares does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
62 1
        $this->arguments     = $data['arguments'];
0 ignored issues
show
Bug Best Practice introduced by
The property arguments does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
63 1
    }
64
65
    /**
66
     * {@inheritdoc}
67
     *
68
     * @internal
69
     */
70 1
    final public function serialize(): string
71
    {
72 1
        return \serialize($this->__serialize());
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     *
78
     * @internal
79
     */
80 1
    final public function unserialize($serialized): void
81
    {
82 1
        $this->__unserialize(\unserialize($serialized));
83 1
    }
84
85
    /**
86
     * Locates appropriate route by name. Support dynamic route allocation using following pattern:
87
     * Pattern route:   `pattern/*<controller@action>`
88
     * Default route: `*<controller@action>`
89
     * Only action:   `pattern/*<action>`.
90
     *
91
     * @param string $route
92
     *
93
     * @throws InvalidControllerException
94
     *
95
     * @return string
96
     */
97 151
    private function castRoute(string $route): string
98
    {
99
        // Match domain + scheme from pattern...
100 151
        if (false !== \preg_match($regex = '@^(?:(https?):)?(//[^/]+)@i', $route)) {
101 151
            $route = $this->castDomain($route, $regex);
102
        }
103
104 151
        if (false !== \strpbrk($route, '*') && false !== \preg_match(self::RCA_PATTERN, $route, $matches)) {
0 ignored issues
show
Bug introduced by
The constant Flight\Routing\Traits\CastingTrait::RCA_PATTERN was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
105 2
            if (!isset($matches['route']) || empty($matches['route'])) {
106 1
                throw new InvalidControllerException("Unable to locate route candidate on `{$route}`");
107
            }
108
109 1
            if (isset($matches['controller'], $matches['action'])) {
110 1
                $this->controller = [$matches['controller'] ?: $this->controller, $matches['action']];
0 ignored issues
show
Bug Best Practice introduced by
The property controller does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
111
            }
112
113 1
            $route = $matches['route'];
114
        }
115
116 150
        return (empty($route) || '/' === $route) ? '/' : $route;
117
    }
118
119
    /**
120
     * Match scheme and domain from route patterned path
121
     *
122
     * @param string $route
123
     * @param string $regex
124
     *
125
     * @return string
126
     */
127 151
    private function castDomain(string $route, string $regex): string
128
    {
129 151
        return (string) \preg_replace_callback($regex, function (array $matches): string {
130 9
            $this->setDomain(isset($matches[1]) ? $matches[0] : $matches[2]);
0 ignored issues
show
Bug introduced by
It seems like setDomain() 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

130
            $this->/** @scrutinizer ignore-call */ 
131
                   setDomain(isset($matches[1]) ? $matches[0] : $matches[2]);
Loading history...
131
132 9
            return '';
133 151
        }, $route);
134
    }
135
136
    /**
137
     * Ensures that the right-most slash is trimmed for prefixes of more than
138
     * one character, and that the prefix begins with a slash.
139
     *
140
     * @param string $uri
141
     * @param string $prefix
142
     *
143
     * @return string
144
     */
145 16
    private function castPrefix(string $uri, string $prefix): string
146
    {
147
        // Allow homepage uri on prefix just like python django url style.
148 16
        if (\in_array($uri, ['', '/'], true)) {
149 7
            return \rtrim($prefix, '/') . $uri;
150
        }
151
152 16
        if (1 === \preg_match('/^([^\|\/|&|-|_|~|@]+)(&|-|_|~|@)/i', $prefix, $matches)) {
153 1
            $newPattern = \rtrim($prefix, $matches[2]) . $matches[2] . $uri;
154
        }
155
156 16
        return !empty($newPattern) ? $newPattern : \rtrim($prefix, '/') . '/' . \ltrim($uri, '/');
157
    }
158
}
159