Route::getPath()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Simply\Router;
4
5
/**
6
 * Represents a matched request route.
7
 * @author Riikka Kalliomäki <[email protected]>
8
 * @copyright Copyright (c) 2018-2019 Riikka Kalliomäki
9
 * @license http://opensource.org/licenses/mit-license.php MIT License
10
 */
11
class Route
12
{
13
    /** @var RouteDefinition Definition of the matched route */
14
    private $definition;
15
16
    /** @var string Requested HTTP method */
17
    private $method;
18
19
    /** @var string[] Segments of the requested path */
20
    private $segments;
21
22
    /** @var string[] Values for the parameters in the requested route */
23
    private $values;
24
25
    /**
26
     * Route constructor.
27
     * @param RouteDefinition $definition The definition of the matched route
28
     * @param string $method The requested HTTP method
29
     * @param string[] $segments The segments of the requested path
30
     * @param string[] $values The values for the parameters in the requested route
31
     */
32 17
    public function __construct(RouteDefinition $definition, string $method, array $segments, array $values)
33
    {
34 17
        $this->definition = $definition;
35 17
        $this->method = $method;
36 17
        $this->segments = $segments;
37 17
        $this->values = $values;
38 17
    }
39
40
    /**
41
     * Returns the requested HTTP method.
42
     * @return string The requested HTTP method
43
     */
44 14
    public function getMethod(): string
45
    {
46 14
        return $this->method;
47
    }
48
49
    /**
50
     * Returns the handler for the requested route.
51
     * @return mixed The handler for the requested route
52
     */
53 15
    public function getHandler()
54
    {
55 15
        return $this->definition->getHandler();
56
    }
57
58
    /**
59
     * Returns the unencoded canonical path for the requested route.
60
     * @return string The canonical path for the requested route
61
     */
62 13
    public function getPath(): string
63
    {
64 13
        if (\count($this->segments) === 0) {
65 2
            return '/';
66
        }
67
68 11
        $path = sprintf('/%s/', implode('/', $this->segments));
69
70 11
        if (!$this->definition->hasSlash()) {
71 3
            $path = substr($path, 0, -1);
72
        }
73
74 11
        return $path;
75
    }
76
77
    /**
78
     * Returns the encoded URL for the requested route.
79
     * @return string The encoded URL for the requested route
80
     */
81 13
    public function getUrl(): string
82
    {
83 13
        return $this->definition->formatUrl($this->values);
84
    }
85
86
    /**
87
     * Returns the value for the given parameter from the requested route.
88
     * @param string $name The name of the parameter
89
     * @return string The value for the parameter
90
     */
91 8
    public function getParameter(string $name): string
92
    {
93 8
        if (!isset($this->values[$name])) {
94 1
            throw new \InvalidArgumentException("Invalid route parameter '$name'");
95
        }
96
97 8
        return $this->values[$name];
98
    }
99
100
    /**
101
     * Returns all parameters from the requested route as an associative array.
102
     * @return string[] All parameters from the requested route
103
     */
104 13
    public function getParameters(): array
105
    {
106 13
        return $this->values;
107
    }
108
}
109