Test Failed
Push — master ( d750d9...412c5f )
by Divine Niiquaye
03:10
created

Route::__set_state()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 13
ccs 0
cts 0
cp 0
crap 2
rs 9.9666
c 2
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;
19
20
use Flight\Routing\Interfaces\RouteInterface;
21
22
/**
23
 * Value object representing a single route.
24
 *
25
 * Routes are a combination of path, middleware, and HTTP methods; two routes
26
 * representing the same path and overlapping HTTP methods are not allowed,
27
 * while two routes representing the same path and non-overlapping HTTP methods
28
 * can be used (and should typically resolve to different middleware).
29
 *
30
 * Internally, only those three properties are required. However, underlying
31
 * router implementations may allow or require additional information, such as
32
 * information defining how to generate a URL from the given route, qualifiers
33
 * for how segments of a route match, or even default values to use. These may
34
 * be provided after instantiation via the "defaults" property and related
35
 * addDefaults() method.
36
 *
37
 * @author Divine Niiquaye Ibok <[email protected]>
38
 */
39
class Route implements RouteInterface
40
{
41
    use Traits\RouteTrait;
42
    use Traits\CastingTrait;
43
44
    /**
45
     * A Pattern to Locates appropriate route by name, support dynamic route allocation using following pattern:
46
     * Pattern route:   `pattern/*<controller@action>`
47
     * Default route: `*<controller@action>`
48
     * Only action:   `pattern/*<action>`.
49
     *
50
     * @var string
51
     */
52
    public const RCA_PATTERN = '/^(?P<route>[^.*]+?)?(?P<handler>\*<(?:(?<c>[a-zA-Z0-9\\\\]+?@))?(?<a>[a-zA-Z0-9_\-]+)?\>)?$/mi';
53
54
    /**
55
     * A Pattern to match protocol, host and port from a url
56
     *
57
     * Examples of urls that can be matched:
58
     * http://en.example.domain
59
     * //example.domain
60
     * //example.com
61
     * https://example.com:34
62 138
     * //example.com
63
     * example.com
64 138
     * localhost:8000
65 138
     * {foo}.domain.com
66 138
     *
67 138
     * @var string
68 137
     */
69
    public const URL_PATTERN = '/^(?:(?P<scheme>https?):)?(?P<domain>(?:\/\/)?([^\/\*]+)?(:\d+)?)\/*?$/m';
70
71
    /**
72
     * Create a new Route constructor.
73
     *
74
     * @param string                                   $name    The route name
75
     * @param string[]                                 $methods The route HTTP methods
76
     * @param string                                   $pattern The route pattern
77
     * @param null|array<mixed,string>|callable|string $handler The route callable
78
     */
79
    public function __construct(string $name, array $methods, string $pattern, $handler)
80
    {
81
        $this->name       = $name;
82
        $this->controller = null === $handler ? '' : $handler;
83
        $this->methods    = \array_map('strtoupper', $methods);
84
        $this->path       = $this->castRoute($pattern);
85
    }
86
87
    /**
88
     * @internal This is handled different by router
89
     *
90
     * @param array $properties
91
     */
92
    public static function __set_state(array $properties)
93
    {
94
        $controller = $properties[5];
95
96
        $recovered = new self($properties[0], $properties[1], $properties[2], $controller);
97
        $recovered->setScheme(...$properties[3]);
98
        $recovered->addMiddleware(...$properties[6]);
99
        $recovered->setPatterns($properties[7]);
100
        $recovered->setDefaults($properties[8]);
101
        $recovered->setArguments($properties[9]);
102
        $recovered->domain = $properties[4];
103
104
        return $recovered;
105
    }
106
}
107