Test Failed
Pull Request — master (#16)
by Divine Niiquaye
03:21
created

CompiledRoute::getHostsRegex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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.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
/**
21
 * CompiledRoutes are returned by the RouteCompilerInterface instance.
22
 *
23
 * @author Divine Niiquaye Ibok <[email protected]>
24
 */
25
class CompiledRoute implements \Serializable
26
{
27
    /** @var string */
28
    private $pathRegex;
29
30
    /** @var string|string[] */
31
    private $hostRegexs;
32
33
    /** @var array */
34
    private $variables;
35
36
    /** @var string|null */
37
    private $staticRoute;
38
39
    /**
40
     * @param string   $pathRegex  The regular expression to use to match this route
41
     * @param string|string[] $hostRegexs A list of Host regexs else a combined single regex of hosts
42
     * @param array    $variables  An array of variables (variables defined in the path and in the host patterns)
43
     */
44
    public function __construct(string $pathRegex, $hostRegexs, array $variables, string $static = null)
45
    {
46
        $this->pathRegex = $pathRegex;
47
        $this->hostRegexs = $hostRegexs;
48
        $this->variables = $variables;
49
        $this->staticRoute = $static;
50
    }
51
52
    /**
53
     * @internal
54
     */
55
    final public function serialize(): string
56
    {
57
        return \serialize(['vars' => $this->variables, 'path_regex' => $this->pathRegex, 'host_regexs' => $this->hostRegexs]);
58
    }
59
60
    /**
61
     * @internal
62
     */
63
    final public function unserialize($data): void
64
    {
65
        $data = \unserialize($data, ['allowed_classes' => false]);
66
67
        $this->variables = $data['vars'];
68
        $this->hostRegexs = $data['host_regexs'];
69
        $this->pathRegex = $data['path_regex'];
70
    }
71
72
    /**
73
     * Returns the path regex.
74
     */
75
    public function getRegex(): string
76
    {
77
        return $this->pathRegex;
78
    }
79
80
    /**
81
     * Returns the hosts regex.
82
     *
83
     * @return string|string[] The hosts regex
84
     */
85
    public function getHostsRegex()
86
    {
87
        return $this->hostRegexs;
88
    }
89
90
    /**
91
     * Returns the compiled variables.
92
     *
93
     * @return array<string,string|null>
94
     */
95
    public function getVariables(): array
96
    {
97
        return $this->variables;
98
    }
99
100
    /**
101
     * If the path is static, return it else null.
102
     */
103
    public function getStatic(): ?string
104
    {
105
        return $this->staticRoute;
106
    }
107
}
108