Route::serialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 9
c 0
b 0
f 0
ccs 0
cts 8
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
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");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $code instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
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");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $type instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
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");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $code instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
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 2
    public function getParameter($key) {
222
223 2
        $parameters = $this->parameters;
224
225 2
        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 3
    public function setParameters($parameters) {
244
245 3
        $this->parameters = $parameters;
246
247 3
        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);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal default does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
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,
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal regex does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
314
                "required" => $required
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal required does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
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;
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal required does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
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;
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal regex does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
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.
0 ignored issues
show
Coding Style introduced by
Operator == prohibited; use === instead
Loading history...
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