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

Route::addPattern()   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 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
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;
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 1
    use Traits\RouteTrait;
42 1
    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<controller>[^@]+)@+)?(?P<action>[a-z_\-]+)\>$/i';
53
54
    /**
55
     * Create a new Route constructor.
56
     *
57
     * @param string                                   $name    The route name
58
     * @param string[]                                 $methods The route HTTP methods
59
     * @param string                                   $pattern The route pattern
60
     * @param null|array<mixed,string>|callable|string $handler The route callable
61
     */
62 152
    public function __construct(string $name, array $methods, string $pattern, $handler)
63
    {
64 152
        $this->name       = $name;
65 152
        $this->controller = null === $handler ? '' : $handler;
66 152
        $this->methods    = \array_map('strtoupper', $methods);
67 152
        $this->path       = $this->castRoute($pattern);
68 151
    }
69
}
70