Completed
Push — 4.0 ( bf6233...a1234e )
by Marco
13:59
created

Route::getInstance()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3.0032

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 28
ccs 13
cts 14
cp 0.9286
rs 8.8571
cc 3
eloc 16
nc 3
nop 3
crap 3.0032
1
<?php namespace Comodojo\Dispatcher\Router;
2
3
use \Comodojo\Dispatcher\Components\DataAccess as DataAccessTrait;
4
use \Comodojo\Dispatcher\Components\DataSerialization as DataSerializationTrait;
5
use \Comodojo\Exception\DispatcherException;
6
use \Serializable;
7
use \Exception;
8
9
/**
10
 * @package     Comodojo Dispatcher
11
 * @author      Marco Giovinazzi <[email protected]>
12
 * @author      Marco Castiello <[email protected]>
13
 * @license     GPL-3.0+
14
 *
15
 * LICENSE:
16
 *
17
 * This program is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License as
19
 * published by the Free Software Foundation, either version 3 of the
20
 * License, or (at your option) any later version.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 * GNU Affero General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU Affero General Public License
28
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29
 */
30
31
class Route implements Serializable {
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: serialize, unserialize
Loading history...
32
    
33
    use DataAccessTrait;
34
    
35
    use DataSerializationTrait;
36
37
    public function __construct() {
38
39
        $this->classname = "";
40
41
        $this->type = "";
42
43
        $this->service = array();
44
45
        $this->parameters = array();
46
47
        $this->query = array();
48
49
    }
50 2
51
    public function getType() {
52
53
        return $this->type;
54 2
55
    }
56 2
57
    public function setType($type) {
58 2
59
        $this->type = $type;
60 2
61
        return $this;
62 2
63
    }
64 2
65
    public function getService() {
66
67
        return $this->service;
68 2
69
    }
70 2
71
    public function getServiceName() {
72 2
73
        return (empty($this->service))?"default":implode('.', $this->service);
74
75
    }
76
77
    public function setService($service) {
78
79
        $this->service = $service;
80
81
        return $this;
82 1
83
    }
84 1
85
    public function addService($service) {
86
87
        array_push($this->service, $service);
88
89
        return $this;
90
91
    }
92
93
    public function getParameter($key) {
94
95
        return (isset($this->parameters[$key]))?$this->parameters[$key]:null;
96 1
97
    }
98 1
99
    public function getParameters() {
100 1
101
        return $this->parameters;
102
103
    }
104 1
105
    public function setParameter($key, $value) {
106 1
107
        $this->parameters[$key] = $value;
108
109
        return $this;
110 2
111
    }
112 2
113
    public function setParameters($parameters) {
114
115
        $this->parameters = $parameters;
116 1
117
        return $this;
118 1
119
    }
120 1
121
    public function setQuery($key, $regex, $required = false) {
122
123
        $this->query[$key] = array(
124 1
            "regex" => $regex,
125
            "required" => $required
126 1
        );
127
128 1
        return $this;
129
130
    }
131
132 1
    public function isQueryRequired($key) {
133
134 1
        return isset($this->query[$key])?$this->query[$key]["required"]:false;
135 1
136
    }
137 1
138
    public function getQueryRegex($key) {
139 1
140
        return isset($this->query[$key])?$this->query[$key]["regex"]:null;
141
142
    }
143 1
144
    public function getQueries() {
145 1
146
        return $this->query;
147
148
    }
149 1
150
    public function setQueries($query) {
151 1
152
        $this->query = $query;
153
154
        return $this;
155
156
    }
157
158
    public function getClassName() {
159
160
        return $this->classname;
161
162
    }
163
164
    public function setClassName($class) {
165
166
        $this->classname = $class;
167
168
        return $this;
169 2
170
    }
171 2
172
    public function path($path) {
173
174
        // Because of the nature of the global regular expression, all the bits of the matched route are associated with a parameter key
175 2
        foreach ($this->query as $key => $value) {
176
177 2
            if (isset($path[$key])) {
178
                /* if it's available a bit associated with the parameter name, it is compared against
179 2
                 * it's regular expression in order to extrect backreferences
180
                 */
181
                if (preg_match('/^' . $value['regex'] . '$/', $path[$key], $matches)) {
182
183 2
                    if (count($matches) == 1) $matches = $matches[0]; // This is the case where no backreferences are present or available.
184
185
                    // The extracted value (with any backreference available) is added to the query parameters.
186
                    $this->setParameter($key, $matches);
187
188
                }
189 2
190
            } elseif ($value['required']) {
191 2
192
                throw new DispatcherException(sprintf("Required parameter '%s' not specified.", $key), 1, null, 500);
193
194 2
            }
195 1
196 2
        }
197
198 2
        return $this;
199 2
200 2
    }
201 2
202
}
203