Test Failed
Pull Request — master (#16)
by Divine Niiquaye
02:47
created

CompiledRoute   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 18
c 2
b 0
f 0
dl 0
loc 74
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A serialize() 0 6 1
A getPathRegex() 0 3 1
A getVariables() 0 3 1
A unserialize() 0 7 1
A getHostsRegex() 0 3 1
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[]|null */
31
    private $hostRegexps;
32
33
    /** @var array */
34
    private $variables;
35
36
    /**
37
     * @param string          $pathRegex   The regular expression to use to match this route
38
     * @param string|string[] $hostRegexps A list of Host regexps else a combined single regex of hosts
39
     * @param array           $variables   An array of variables (variables defined in the path and in the host patterns)
40
     */
41
    public function __construct(string $pathRegex, $hostRegexps, array $variables)
42
    {
43
        $this->pathRegex = $pathRegex;
44
        $this->hostRegexps = $hostRegexps;
45
        $this->variables = $variables;
46
    }
47
48
    /**
49
     * @internal
50
     */
51
    final public function serialize(): string
52
    {
53
        return \serialize([
54
            'vars' => $this->variables,
55
            'path_regex' => $this->pathRegex,
56
            'host_regexps' => $this->hostRegexps,
57
        ]);
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->hostRegexps = $data['host_regexps'];
69
        $this->pathRegex = $data['path_regex'];
70
    }
71
72
    /**
73
     * Returns the path regex with modifies.
74
     * Static path should begin wih a "/" while dynamic route begins with "\\/".
75
     */
76
    public function getPathRegex(): string
77
    {
78
        return $this->pathRegex;
79
    }
80
81
    /**
82
     * Returns the hosts regex with modifies.
83
     *
84
     * @return string[] The hosts regex
85
     */
86
    public function getHostsRegex(): array
87
    {
88
        return $this->hostRegexps;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->hostRegexps could return the type null|string which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
89
    }
90
91
    /**
92
     * Returns the compiled variables.
93
     *
94
     * @return array<string,string|null>
95
     */
96
    public function getVariables(): array
97
    {
98
        return $this->variables;
99
    }
100
}
101