Completed
Push — master ( 465cbd...e0d13e )
by Marco
06:44 queued 11s
created

Route::setParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php namespace Comodojo\Dispatcher\Router;
2
3
use \Comodojo\Exception\DispatcherException;
4
use \Serializable;
5
use \InvalidArgumentException;
6
use \Exception;
7
8
/**
9
 * @package     Comodojo Dispatcher
10
 * @author      Marco Giovinazzi <[email protected]>
11
 * @author      Marco Castiello <[email protected]>
12
 * @license     MIT
13
 *
14
 * LICENSE:
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
class Route implements Serializable {
26
27
    /**
28
     * @const string
29
     */
30
    const REDIRECT_REFRESH = 'REFRESH';
31
32
    /**
33
     * @const string
34
     */
35
    const REDIRECT_LOCATION = 'LOCATION';
36
37
    /**
38
     * @var string
39
     */
40
    protected $type;
41
42
    /**
43
     * @var string
44
     */
45
    protected $classname;
46
47
    /**
48
     * @var int
49
     */
50
    protected $redirect_code;
51
52
    /**
53
     * @var string
54
     */
55
    protected $redirect_location;
56
57
    /**
58
     * @var string
59
     */
60
    protected $redirect_message;
61
62
    /**
63
     * @var string
64
     */
65
    protected $redirect_type = self::REDIRECT_LOCATION;
66
67
    /**
68
     * @var int
69
     */
70
    protected $error_code;
71
72
    /**
73
     * @var string
74
     */
75
    protected $error_message;
76
77
    /**
78
    * @var array
79
    */
80
    protected $parameters = [];
81
82
    /**
83
     * @var array
84
     */
85
    protected $service = [];
86
87
    /**
88
     * @var array
89
     */
90
    protected $request = [];
91
92
    /**
93
     * @var array
94
     */
95
    protected $query = [];
96
97 2
    public function getType() {
98
99 2
        return $this->type;
100
101
    }
102
103 2
    public function setType($type) {
104
105 2
        $this->type = $type;
106
107 2
        return $this;
108
109
    }
110
111 2
    public function getClassName() {
112
113 2
        return $this->classname;
114
115
    }
116
117 2
    public function setClassName($class) {
118
119 2
        $this->classname = $class;
120
121 2
        return $this;
122
123
    }
124
125
    public function getRedirectCode() {
126
127
        return $this->redirect_code;
128
129
    }
130
131
    public function setRedirectCode($code) {
132
133
        if ( $code < 300 || $code >= 400 ) {
134
            throw new InvalidArgumentException("Invalid redirection code $code");
135
        }
136
137
        $this->redirect_code = $code;
138
139
        return $this;
140
141
    }
142
143
    public function getRedirectLocation() {
144
145
        return $this->redirect_location;
146
147
    }
148
149
    public function setRedirectLocation($location) {
150
151
        $this->redirect_location = $location;
152
153
        return $this;
154
155
    }
156
157
    public function getRedirectMessage() {
158
159
        return $this->redirect_message;
160
161
    }
162
163
    public function setRedirectMessage($message) {
164
165
        $this->redirect_message = $message;
166
167
        return $this;
168
169
    }
170
171
    public function getRedirectType() {
172
173
        return $this->redirect_type;
174
175
    }
176
177
    public function setRedirectType($type) {
178
179
        if ( !in_array($type, [self::REDIRECT_REFRESH, self::REDIRECT_LOCATION]) ) {
180
            throw new InvalidArgumentException("Invalid redirection type $type");
181
        }
182
183
        $this->redirect_type = $type;
184
185
        return $this;
186
187
    }
188
189
    public function getErrorCode() {
190
191
        return $this->error_code;
192
193
    }
194
195
    public function setErrorCode($code) {
196
197
        if ( $code < 400 || $code >= 600 ) {
198
            throw new InvalidArgumentException("Invalid error code $code");
199
        }
200
201
        $this->error_code = $code;
202
203
        return $this;
204
205
    }
206
207
    public function getErrorMessage() {
208
209
        return $this->error_message;
210
211
    }
212
213
    public function setErrorMessage($message) {
214
215
        $this->error_message = $message;
216
217
        return $this;
218
219
    }
220
221
    public function getParameter($key) {
222
223
        $parameters = $this->parameters;
224
225
        return isset($parameters[$key]) ? $parameters[$key] : null;
226
227
    }
228
229
    public function getParameters() {
230
231
        return $this->parameters;
232
233
    }
234
235
    public function setParameter($key, $value) {
236
237
        $this->parameters = array_merge($this->parameters, array($key => $value));
238
239
        return $this;
240
241
    }
242
243 1
    public function setParameters($parameters) {
244
245 1
        $this->parameters = $parameters;
246
247 1
        return $this;
248
249
    }
250
251 1
    public function getRequestParameter($key) {
252
253 1
        $parameters = $this->request;
254
255 1
        return isset($parameters[$key]) ? $parameters[$key] : null;
256
257
    }
258
259
    public function getService() {
260
261
        return $this->service;
262
263
    }
264
265 1
    public function getServiceName() {
266
267 1
        return empty($this->service) ? "default" : implode('.', $this->service);
268
269
    }
270
271
    public function setService($service) {
272
273
        $this->service = $service;
274
275
        return $this;
276
277
    }
278
279 1
    public function addService($service) {
280
281 1
        $this->service = array_merge($this->service, array($service));
282
283 1
        return $this;
284
285
    }
286
287 1
    public function getRequestParameters() {
288
289 1
        return $this->request;
290
291
    }
292
293 1
    public function setRequestParameter($key, $value) {
294
295 1
        $this->request = array_merge($this->request, array($key => $value));
296
297 1
        return $this;
298
299
    }
300
301
    public function setRequestParameters($parameters) {
302
303
        $this->request = $parameters;
304
305
        return $this;
306
307
    }
308
309 1
    public function setQuery($key, $regex, $required = false) {
310
311 1
        $this->query = array_merge($this->query, [
312
            $key => [
313 1
                "regex" => $regex,
314
                "required" => $required
315 1
            ]
316 1
        ]);
317
318 1
        return $this;
319
320
    }
321
322 1
    public function isQueryRequired($key) {
323
324 1
        $query = $this->query;
325
326 1
        return isset($query[$key]) ? $query[$key]["required"] : false;
327
328
    }
329
330 1
    public function getQueryRegex($key) {
331
332 1
        $query = $this->query;
333
334 1
        return isset($query[$key]) ? $query[$key]["regex"] : null;
335
336
    }
337
338
    public function getQueries() {
339
340
        return $this->query;
341
342
    }
343
344
    public function setQueries($query) {
345
346
        $this->query = $query;
347
348
        return $this;
349
350
    }
351
352 1
    public function path($path) {
353
354
        // Because of the nature of the global regular expression, all the bits of the matched route are associated with a parameter key
355 1
        foreach ($this->query as $key => $value) {
356
357 1
            if ( isset($path[$key]) ) {
358
                /* if it's available a bit associated with the parameter name, it is compared against
359
                 * it's regular expression in order to extrect backreferences
360
                 */
361 1
                if ( preg_match('/^' . $value['regex'] . '$/', $path[$key], $matches) ) {
362
363 1
                    if ( count($matches) == 1 ) $matches = $matches[0]; // This is the case where no backreferences are present or available.
364
365
                    // The extracted value (with any backreference available) is added to the query parameters.
366 1
                    $this->setRequestParameter($key, $matches);
367
368 1
                }
369
370 1
            } elseif ($value['required']) {
371
372
                throw new DispatcherException(sprintf("Required parameter '%s' not specified.", $key), 1, null, 500);
373
374
            }
375
376 1
        }
377
378 1
        return $this;
379
380
    }
381
382
    /**
383
     * Return the serialized data
384
     *
385
     * @return string
386
     */
387
    public function serialize() {
388
389
        return serialize( (object) [
390
            'classname' => $this->classname,
391
            'type' => $this->type,
392
            'service' => $this->service,
393
            'parameters' => $this->parameters,
394
            'request' => $this->request,
395
            'query' => $this->query
396
        ]);
397
398
    }
399
400
    /**
401
     * Return the unserialized object
402
     *
403
     * @param string $data Serialized data
404
     *
405
     */
406
    public function unserialize($data) {
407
408
        $parts = unserialize($data);
409
410
        $this->classname = $parts->classname;
411
        $this->type = $parts->type;
412
        $this->service = $parts->service;
413
        $this->parameters = $parts->parameters;
414
        $this->request = $parts->request;
415
        $this->query = $parts->query;
416
417
    }
418
419
}
420