Passed
Push — master ( 67a6c5...5b4678 )
by Divine Niiquaye
24:01 queued 08:03
created

Route::setData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
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 217
    public function __construct(string $pattern, $methods = self::DEFAULT_METHODS, $handler = null)
67
    {
68 217
        $this->data = ['handler' => $handler];
69
70 217
        foreach ((array) $methods as $method) {
71 215
            $this->data['methods'][\strtoupper($method)] = true;
72
        }
73
74 217
        if (!empty($pattern)) {
75 216
            $this->path($pattern);
76
        }
77
    }
78
79
    /**
80
     * @internal
81
     */
82 12
    public function __serialize(): array
83
    {
84 12
        return $this->data;
85
    }
86
87
    /**
88
     * @internal
89
     *
90
     * @param array<string,mixed> $data
91
     */
92 13
    public function __unserialize(array $data): void
93
    {
94 13
        $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 11
    public static function __set_state(array $properties)
105
    {
106 11
        $route = new static($properties['path'] ?? '', $properties['methods'] ?? [], $properties['handler'] ?? null);
107 10
        $route->data += $properties['data'] ?? \array_diff_key($properties, ['path' => null, 'methods' => [], 'handler' => null]);
108
109 10
        return $route;
110
    }
111
112
    /**
113
     * Create a new Route statically.
114
     *
115
     * @param string          $pattern The route pattern
116
     * @param string|string[] $methods the route HTTP methods
117
     * @param mixed           $handler The PHP class, object or callable that returns the response when matched
118
     *
119
     * @return static
120
     */
121 10
    public static function to(string $pattern, $methods = self::DEFAULT_METHODS, $handler = null)
122
    {
123 10
        return new static($pattern, $methods, $handler);
124
    }
125
126
    /**
127
     * Sets a custom key and value into route
128
     *
129
     * @return $this
130
     */
131 213
    public function setData(string $key, $value)
132
    {
133 213
        $this->data[$key] = $value;
134
135 213
        return $this;
136
    }
137
138
    /**
139
     * Get the route's data.
140
     *
141
     * @return array<string,mixed>
142
     */
143 6
    public function getData(): array
144
    {
145 6
        return $this->data;
146
    }
147
148 23
    public function generateRouteName(string $prefix): string
149
    {
150 23
        $routeName = \implode('_', $this->getMethods()) . '_' . $prefix . $this->data['path'] ?? '';
151 23
        $routeName = \str_replace(['/', ':', '|', '-'], '_', $routeName);
152 23
        $routeName = (string) \preg_replace('/[^a-z0-9A-Z_.]+/', '', $routeName);
153
154 23
        return (string) \preg_replace(['/\_+/', '/\.+/'], ['_', '.'], $routeName);
155
    }
156
}
157