Completed
Push — master ( 01675b...2dfe17 )
by Alex
04:02
created

DefaultTypes::stringHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
namespace Mezon\Router;
3
4
/**
5
 * Default types for router - integer, string, command, list if ids
6
 *
7
 * @author gdever
8
 */
9
trait DefaultTypes
10
{
11
12
    /**
13
     * Supported types of URL parameters
14
     *
15
     * @var array
16
     */
17
    private $types = [];
18
19
    /**
20
     * Method handles integer type
21
     *
22
     * @param string $value
23
     *            value to be parsed
24
     * @return bool was the value parsed
25
     */
26
    public static function intHandler(string &$value): bool
27
    {
28
        if (is_numeric($value)) {
29
            $value = $value + 0;
30
            return true;
31
        }
32
33
        return false;
34
    }
35
36
    /**
37
     * Method handles command type
38
     *
39
     * @param string $value
40
     *            value to be parsed
41
     * @return bool was the value parsed
42
     */
43
    public static function commandHandler(string &$value): bool
44
    {
45
        if (preg_match('/^([a-z0-9A-Z_\/\-\.\@]+)$/', $value)) {
46
            return true;
47
        }
48
49
        return false;
50
    }
51
52
    /**
53
     * Method handles list of integers type
54
     *
55
     * @param string $value
56
     *            value to be parsed
57
     * @return bool was the value parsed
58
     */
59
    public static function intListHandler(string &$value): bool
60
    {
61
        if (preg_match('/^([0-9,]+)$/', $value)) {
62
            return true;
63
        }
64
65
        return false;
66
    }
67
68
    /**
69
     * Method handles string type
70
     *
71
     * @param string $value
72
     *            value to be parsed
73
     * @return bool was the value parsed
74
     */
75
    public static function stringHandler(string &$value): bool
76
    {
77
        $value = htmlspecialchars($value, ENT_QUOTES);
78
79
        return true;
80
    }
81
82
    /**
83
     * Init types
84
     */
85
    protected function initDefaultTypes()
86
    {
87
        $this->types['i'] = '\Mezon\Router\DefaultTypes::intHandler';
88
        $this->types['a'] = '\Mezon\Router\DefaultTypes::commandHandler';
89
        $this->types['il'] = '\Mezon\Router\DefaultTypes::intListHandler';
90
        $this->types['s'] = '\Mezon\Router\DefaultTypes::stringHandler';
91
    }
92
}
93