Completed
Push — 4.0 ( 6a2270...a7534e )
by Marco
12:55
created

Route::setParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php namespace Comodojo\Dispatcher\Router;
2
3
use \Comodojo\Dispatcher\Components\Model as DispatcherClassModel;
4
use \Comodojo\Dispatcher\Router\Model as Router;
5
use \Comodojo\Dispatcher\Request\Model as Request;
6
use \Comodojo\Dispatcher\Response\Model as Response;
7
use \Comodojo\Dispatcher\Extra\Model as Extra;
8
use \Comodojo\Dispatcher\Components\Timestamp as TimestampTrait;
9
use \Comodojo\Exception\DispatcherException;
10
use \Exception;
11
12
/**
13
 * @package     Comodojo Dispatcher
14
 * @author      Marco Giovinazzi <[email protected]>
15
 * @author      Marco Castiello <[email protected]>
16
 * @license     GPL-3.0+
17
 *
18
 * LICENSE:
19
 *
20
 * This program is free software: you can redistribute it and/or modify
21
 * it under the terms of the GNU Affero General Public License as
22
 * published by the Free Software Foundation, either version 3 of the
23
 * License, or (at your option) any later version.
24
 *
25
 * This program is distributed in the hope that it will be useful,
26
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
 * GNU Affero General Public License for more details.
29
 *
30
 * You should have received a copy of the GNU Affero General Public License
31
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32
 */
33
34
class Route extends DispatcherClassModel {
35
36
    use TimestampTrait;
37
38
    private $classname = "";
39
40
    private $type = "";
41
42
    private $service = array();
43
44
    private $parameters = array();
45
46
    private $query = array();
47
48
    private $router = null;
49
50
    public function __construct(
51
        Router $router
52
    ) {
53
54
        parent::__construct($router->configuration(), $router->logger());
55
        
56
        $this->router = $router;
57
58
        $this->setTimestamp();
59
60
    }
61
62
    public function getType() {
63
64
        return $this->type;
65
66
    }
67
68
    public function setType($type) {
69
70
        $this->type = $type;
71
        
72
        return $this;
73
74
    }
75
76
    public function getService() {
77
78
        return $this->service;
79
80
    }
81
82
    public function getServiceName() {
83
84
        return (empty($this->service))?"default":implode('.', $this->service);
85
86
    }
87
88
    public function setService($service) {
89
90
        $this->service = $service;
91
        
92
        return $this;
93
94
    }
95
96
    public function addService($service) {
97
98
        array_push($this->service, $service);
99
        
100
        return $this;
101
102
    }
103
104
    public function getParameter($key) {
105
        
106
        return (isset($this->parameters[$key]))?$this->parameters[$key]:null;
107
108
    }
109
110
    public function getParameters() {
111
        
112
        return $this->parameters;
113
114
    }
115
116
    public function setParameter($key, $value) {
117
        
118
        $this->parameters[$key] = $value;
119
        
120
        return $this;
121
122
    }
123
124
    public function setParameters($parameters) {
125
126
        $this->parameters = $parameters;
127
        
128
        return $this;
129
130
    }
131
    
132
    public function setQuery($key, $regex, $required = false) {
133
        
134
        $this->query[$key] = array(
135
            "regex" => $regex,
136
            "required" => $required
137
        );
138
        
139
        return $this;
140
        
141
    }
142
    
143
    public function isQueryRequired($key) {
144
        
145
        return isset($this->query[$key])?$this->query[$key]["required"]:false;
146
        
147
    }
148
    
149
    public function getQueryRegex($key) {
150
        
151
        return isset($this->query[$key])?$this->query[$key]["regex"]:null;
152
        
153
    }
154
    
155
    public function getQueries() {
156
        
157
        return $this->query;
158
        
159
    }
160
    
161
    public function setQueries($query) {
162
        
163
        $this->query = $query;
164
        
165
        return $this;
166
        
167
    }
168
169
    public function getClassName() {
170
171
        return $this->classname;
172
173
    }
174
175
    public function setClassName($class) {
176
177
        $this->classname = $class;
178
        
179
        return $this;
180
181
    }
182
183
    public function getInstance(
184
        Request $request,
185
        Response $response,
186
        Extra $extra
187
    ) {
188
189
        $class = $this->classname;
190
191
        if (class_exists($class)) {
192
            
193
            // All the route parameters are also added to the query parameters
194
            foreach ($this->getParameters() as $parameter => $value) {
195
196
                $request->query()->set($parameter, $value);
197
198
            }
199
200
            return new $class(
201
                $this->configuration,
202
                $this->logger,
203
                $request,
204
                $this->router,
205
                $response,
206
                $extra
207
            );
208
209
        }
210
        else return null;
211
212
    }
213
    
214
    public function path($path) {
215
        
216
        // Because of the nature of the global regular expression, all the bits of the matched route are associated with a parameter key
217
        foreach ($this->query as $key => $value) {
218
219
            if (isset($path[$key])) {
220
                /* if it's available a bit associated with the parameter name, it is compared against
221
                 * it's regular expression in order to extrect backreferences
222
                 */
223
                if (preg_match('/^' . $value['regex'] . '$/', $path[$key], $matches)) {
224
                    
225
                    if (count($matches) == 1) $matches = $matches[0]; // This is the case where no backreferences are present or available.
226
                    
227
                    // The extracted value (with any backreference available) is added to the query parameters.
228
                    $this->setParameter($key, $matches);
229
230
                }
231
232
            } elseif ($value['required']) {
233
234
                throw new DispatcherException(sprintf("Required parameter '%s' not specified.", $key), 1, null, 500);
235
236
            }
237
238
        }
239
        
240
        return $this;
241
        
242
    }
243
    
244
    public function getData() {
245
        
246
        return array(
247
            "type"       => $this->getType(),
248
            "class"      => $this->getClassName(),
249
            "service"    => $this->getService(),
250
            "parameters" => $this->getParameters(),
251
            "query"      => $this->getQueries()
252
        );
253
        
254
    }
255
    
256
    public function setData($data) {
257
        
258
        $this->setType($data['type'])
259
            ->setClassName($data['class'])
260
            ->setService($data['service'])
261
            ->setParameters($data['parameters'])
262
            ->setQueries($data['query']);
263
            
264
        return $this;
265
        
266
    }
267
268
}
269