RequestRoute::getParameter()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * Copyright (c) 2011-2015, Celestino Diaz <[email protected]>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
25
namespace Brickoo\Component\Routing\Route;
26
27
use Brickoo\Component\Routing\Route\Exception\ParameterNotAvailableException;
28
use Brickoo\Component\Common\Assert;
29
30
/**
31
 * RequestRoute
32
 *
33
 * Implementation of an executable route containing the responsible route.
34
 * @author Celestino Diaz <[email protected]>
35
 */
36
class RequestRoute {
37
38
    /** @var \Brickoo\Component\Routing\Route\Route */
39
    private $route;
40
41
    /** @var array */
42
    private $parameters;
43
44
    /**
45
     * Class constructor.
46
     * @param \Brickoo\Component\Routing\Route\Route $route the matching request route
47
     * @param array $parameters the parameters extracted from the request
48
     */
49 1
    public function __construct(Route $route, array $parameters = []) {
50 1
        $this->route = $route;
51 1
        $this->parameters = $parameters;
52 1
    }
53
54
    /**
55
     * Return the matched route.
56
     * @return \Brickoo\Component\Routing\Route\Route
57
     */
58 1
    public function getRoute() {
59 1
        return $this->route;
60
    }
61
62
    /**
63
     * Return the route parameters and values as pairs.
64
     * @return array the route parameters and values
65
     */
66 1
    public function getParameters() {
67 1
        return $this->parameters;
68
    }
69
70
    /**
71
     * Return the value of the parameter.
72
     * @param string $parameter the parameter name
73
     * @throws \Brickoo\Component\Routing\Route\Exception\ParameterNotAvailableException
74
     * @return string the parameter value
75
     */
76 3
    public function getParameter($parameter) {
77 3
        Assert::isString($parameter);
78
79 2
        if (!$this->hasParameter($parameter)) {
80 1
            throw new ParameterNotAvailableException($parameter);
81
        }
82
83 1
        return $this->parameters[$parameter];
84
    }
85
86
    /**
87
     * Check if the parameter is available.
88
     * @param string $parameter the parameter name
89
     * @return boolean check result
90
     */
91 2
    public function hasParameter($parameter) {
92 2
        Assert::isString($parameter);
93 1
        return isset($this->parameters[$parameter]);
94
    }
95
96
}
97