Passed
Pull Request — master (#225)
by Rustam
11:11 queued 08:15
created

CurrentRoute::getArgument()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router;
6
7
use LogicException;
8
use Psr\Http\Message\UriInterface;
9
10
/**
11
 * Holds information about current route e.g. matched last.
12
 */
13
final class CurrentRoute
14
{
15
    /**
16
     * Current Route.
17
     */
18
    private ?Route $route = null;
19
20
    /**
21
     * Current URI.
22
     */
23
    private ?UriInterface $uri = null;
24
25
    /**
26
     * Current Route arguments.
27
     *
28
     * @var array<string, string>
29
     */
30
    private array $arguments = [];
31
32
    /**
33
     * Returns the current route name.
34
     *
35
     * @return string|null The current route name.
36
     */
37 3
    public function getName(): ?string
38
    {
39 3
        return $this->route?->getName();
40
    }
41
42
    /**
43
     * Returns the current route hosts.
44
     *
45
     * @return array|null The current route hosts.
46
     */
47 1
    public function getHosts(): ?array
48
    {
49 1
        return $this->route?->getHosts();
50
    }
51
52
    /**
53
     * Returns the current route pattern.
54
     *
55
     * @return string|null The current route pattern.
56
     */
57 1
    public function getPattern(): ?string
58
    {
59 1
        return $this->route?->getPattern();
60
    }
61
62
    /**
63
     * Returns the current route methods.
64
     *
65
     * @return string[]|null The current route methods.
66
     */
67 1
    public function getMethods(): ?array
68
    {
69 1
        return $this->route?->getMethods();
70
    }
71
72
    /**
73
     * Returns the current URI.
74
     *
75
     * @return UriInterface|null The current URI.
76
     */
77 5
    public function getUri(): ?UriInterface
78
    {
79 5
        return $this->uri;
80
    }
81
82
    /**
83
     * @param array<string,string> $arguments
84
     *
85
     * @internal
86
     */
87 23
    public function setRouteWithArguments(Route $route, array $arguments): void
88
    {
89 23
        if ($this->route === null && $this->arguments === []) {
90 23
            $this->route = $route;
91 23
            $this->arguments = $arguments;
92 23
            return;
93
        }
94 2
        throw new LogicException('Can not set route/arguments since it was already set.');
95
    }
96
97
    /**
98
     * @internal
99
     */
100 17
    public function setUri(UriInterface $uri): void
101
    {
102 17
        if ($this->uri === null) {
103 17
            $this->uri = $uri;
104 17
            return;
105
        }
106 1
        throw new LogicException('Can not set URI since it was already set.');
107
    }
108
109
    /**
110
     * @return array<string, string> Arguments.
111
     */
112 5
    public function getArguments(): array
113
    {
114 5
        return $this->arguments;
115
    }
116
117 3
    public function getArgument(string $name, ?string $default = null): ?string
118
    {
119 3
        return $this->arguments[$name] ?? $default;
120
    }
121
}
122