GenericRoute::getRule()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
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\Common\Assert;
28
use UnexpectedValueException;
29
30
/**
31
 * GenericRoute
32
 *
33
 * Implements a generic route.
34
 * @author Celestino Diaz <[email protected]>
35
 */
36
class GenericRoute implements Route {
37
38
    /** @var string */
39
    protected $name;
40
41
    /** @var string */
42
    protected $path;
43
44
    /** @var string */
45
    protected $controller;
46
47
    /** @var array */
48
    protected $defaultValues;
49
50
    /** @var array */
51
    protected $rules;
52
53
    /**
54
     * Class constructor.
55
     * @param string $name
56
     * @param string $path
57
     * @param string $controller
58
     * @throws \InvalidArgumentException
59
     */
60 2
    public function __construct($name, $path, $controller) {
61 2
        Assert::isString($name);
62 2
        Assert::isString($path);
63 2
        Assert::isString($controller);
64
65 2
        $this->name = $name;
66 2
        $this->path = $path;
67 2
        $this->controller = $controller;
68 2
        $this->rules = [];
69 2
        $this->defaultValues = [];
70 2
    }
71
72
    /** {@inheritDoc} */
73 1
    public function getName() {
74 1
        return $this->name;
75
    }
76
77
    /** {@inheritDoc} */
78 1
    public function getPath() {
79 1
        return $this->path;
80
    }
81
82
    /** {@inheritDoc} */
83 1
    public function getController() {
84 1
        return $this->controller;
85
    }
86
87
    /** {@inheritDoc} */
88 1
    public function getRules() {
89 1
        return $this->rules;
90
    }
91
92
    /** {@inheritDoc} */
93 3
    public function getRule($parameter) {
94 3
        Assert::isString($parameter);
95
96 2
        if (!$this->hasRule($parameter)) {
97 1
            throw new UnexpectedValueException(
98 1
                sprintf("The rule for `%s` does not exist.", $parameter)
99 1
            );
100
        }
101
102 1
        return $this->rules[$parameter];
103
    }
104
105
    /** {@inheritDoc} */
106 1
    public function hasRules() {
107 1
        return (!empty($this->rules));
108
    }
109
110
    /** {@inheritDoc} */
111 2
    public function hasRule($parameter) {
112 2
        Assert::isString($parameter);
113 1
        return array_key_exists($parameter, $this->rules);
114
    }
115
116
    /** {@inheritDoc} */
117 1
    public function setRules(array $rules) {
118 1
        $this->rules = $rules;
119 1
        return $this;
120
    }
121
122
    /** {@inheritDoc} */
123 1
    public function getDefaultValues() {
124 1
        return $this->defaultValues;
125
    }
126
127
    /** {@inheritDoc} */
128 3
    public function getDefaultValue($parameter) {
129 3
        Assert::isString($parameter);
130
131 2
        if (!$this->hasDefaultValue($parameter)) {
132 1
            throw new UnexpectedValueException(
133 1
                sprintf("The default value for the parameter `%s` does not exist.", $parameter)
134 1
            );
135
        }
136 1
        return $this->defaultValues[$parameter];
137
    }
138
139
    /** {@inheritDoc} */
140 2
    public function hasDefaultValue($parameter) {
141 2
        Assert::isString($parameter);
142 1
        return array_key_exists($parameter, $this->defaultValues);
143
    }
144
145
    /** {@inheritDoc} */
146 1
    public function setDefaultValues(array $defaultValues) {
147 1
        $this->defaultValues = $defaultValues;
148 1
        return $this;
149
    }
150
151
}
152