Passed
Push — master ( 049287...68f58a )
by Divine Niiquaye
03:27
created

Route::__serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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.4 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
/**
21
 * Value object representing a single route.
22
 *
23
 * @author Divine Niiquaye Ibok <[email protected]>
24
 */
25
class Route
26
{
27
    use Traits\DataTrait;
28
29
    /**
30
     * A Pattern to Locates appropriate route by name, support dynamic route allocation using following pattern:
31
     * Pattern route:   `pattern/*<controller@action>`
32
     * Default route: `*<controller@action>`
33
     * Only action:   `pattern/*<action>`.
34
     */
35
    public const RCA_PATTERN = '#^(?:([a-z]+)\:)?(?:\/{2}([^\/]+))?([^*]*)(?:\*\<(?:([\w+\\\\]+)\@)?(\w+)\>)?$#u';
36
37
    /**
38
     * A Pattern to match protocol, host and port from a url.
39
     *
40
     * Examples of urls that can be matched: http://en.example.domain, {sub}.example.domain, https://example.com:34, example.com, etc.
41
     */
42
    public const URL_PATTERN = '#^(?:([a-z]+)\:\/{2})?([^\/]+)?$#u';
43
44
    /**
45
     * A Pattern to match the route's priority.
46
     *
47
     * If route path matches, 1 is expected return else 0 should be return as priority index.
48
     */
49
    public const PRIORITY_REGEX = '#^\\/?[\\/\\w+]+\\b#';
50
51
    /**
52
     * Slashes supported on browser when used.
53
     */
54
    public const URL_PREFIX_SLASHES = ['/' => '/', ':' => ':', '-' => '-', '_' => '_', '~' => '~', '@' => '@'];
55
56
    /** @var array<int,string> Default methods for route. */
57
    public const DEFAULT_METHODS = [Router::METHOD_GET, Router::METHOD_HEAD];
58
59
    /**
60
     * Create a new Route constructor.
61
     *
62
     * @param string          $pattern The route pattern
63
     * @param string|string[] $methods the route HTTP methods
64
     * @param mixed           $handler The PHP class, object or callable that returns the response when matched
65
     */
66 214
    public function __construct(string $pattern, $methods = self::DEFAULT_METHODS, $handler = null)
67
    {
68 214
        $this->data = ['handler' => $handler];
69
70 214
        foreach ((array) $methods as $method) {
71 211
            $this->data['methods'][\strtoupper($method)] = true;
72
        }
73
74 214
        if (!empty($pattern)) {
75 212
            $this->path($pattern);
76
        }
77
    }
78
79
    /**
80
     * @internal
81
     */
82 11
    public function __serialize(): array
83
    {
84 11
        return $this->data;
85
    }
86
87
    /**
88
     * @internal
89
     *
90
     * @param array<string,mixed> $data
91
     */
92 11
    public function __unserialize(array $data): void
93
    {
94 11
        $this->data = $data;
95
    }
96
97
    /**
98
     * @internal
99
     *
100
     * @param array<string,mixed> $properties The route data properties
101
     *
102
     * @return static
103
     */
104 13
    public static function __set_state(array $properties)
105
    {
106 13
        if (\array_key_exists('data', $properties)) {
107 2
            $route = new static('', []);
108 2
            $route->data = $properties['data'];
109
        } else {
110 11
            $route = new static($properties['path'] ?? '', $properties['methods'] ?? [], $properties['handler'] ?? null);
111 10
            $route->data += \array_diff_key($properties, ['path' => null, 'methods' => [], 'handler' => null]);
112
        }
113
114 12
        return $route;
115
    }
116
117
    /**
118
     * Create a new Route statically.
119
     *
120
     * @param string          $pattern The route pattern
121
     * @param string|string[] $methods the route HTTP methods
122
     * @param mixed           $handler The PHP class, object or callable that returns the response when matched
123
     *
124
     * @return static
125
     */
126 9
    public static function to(string $pattern, $methods = self::DEFAULT_METHODS, $handler = null)
127
    {
128 9
        return new static($pattern, $methods, $handler);
129
    }
130
131
    /**
132
     * Get the route's data.
133
     *
134
     * @return array<string,mixed>
135
     */
136 6
    public function getData(): array
137
    {
138 6
        return $this->data;
139
    }
140
141 23
    public function generateRouteName(string $prefix): string
142
    {
143 23
        $routeName = \implode('_', $this->getMethods()) . '_' . $prefix . $this->data['path'] ?? '';
144 23
        $routeName = \str_replace(['/', ':', '|', '-'], '_', $routeName);
145 23
        $routeName = (string) \preg_replace('/[^a-z0-9A-Z_.]+/', '', $routeName);
146
147 23
        return (string) \preg_replace(['/\_+/', '/\.+/'], ['_', '.'], $routeName);
148
    }
149
}
150