Completed
Push — master ( 82eb41...4f6b0a )
by Alexander
03:49
created

Token::getTypeName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2.0625
1
<?php
2
namespace Xiag\Rql\Parser;
3
4
use Xiag\Rql\Parser\Exception\UnknownTokenException;
5
6
class Token
7
{
8
    const T_END                 = -1;
9
10
    const T_INTEGER             = 1;
11
    const T_FLOAT               = 2;
12
    const T_STRING              = 3;
13
    const T_DATE                = 4;
14
    const T_GLOB                = 5;
15
16
    const T_CLOSE_PARENTHESIS   = 11;
17
    const T_OPEN_PARENTHESIS    = 12;
18
    const T_COMMA               = 13;
19
    const T_AMPERSAND           = 14;
20
    const T_VERTICAL_BAR        = 15;
21
    const T_PLUS                = 16;
22
    const T_MINUS               = 17;
23
    const T_COLON               = 18;
24
25
    const T_TYPE                = 31;
26
27
    const T_OPERATOR            = 41;
28
29
    const T_NULL                = 51;
30
    const T_EMPTY               = 52;
31
    const T_TRUE                = 53;
32
    const T_FALSE               = 54;
33
34
    /**
35
     * @var array Type<->Name mapping
36
     */
37
    public static $typeNameMap = [
38
        self::T_END               => 'T_END',
39
40
        self::T_INTEGER           => 'T_INTEGER',
41
        self::T_FLOAT             => 'T_FLOAT',
42
        self::T_STRING            => 'T_STRING',
43
        self::T_DATE              => 'T_DATE',
44
        self::T_GLOB              => 'T_GLOB',
45
46
        self::T_CLOSE_PARENTHESIS => 'T_CLOSE_PARENTHESIS',
47
        self::T_OPEN_PARENTHESIS  => 'T_OPEN_PARENTHESIS',
48
        self::T_COMMA             => 'T_COMMA',
49
        self::T_AMPERSAND         => 'T_AMPERSAND',
50
        self::T_VERTICAL_BAR      => 'T_VERTICAL_BAR',
51
        self::T_PLUS              => 'T_PLUS',
52
        self::T_MINUS             => 'T_MINUS',
53
        self::T_COLON             => 'T_COLON',
54
55
        self::T_TYPE              => 'T_TYPE',
56
57
        self::T_OPERATOR          => 'T_OPERATOR',
58
59
        self::T_NULL              => 'T_NULL',
60
        self::T_EMPTY             => 'T_EMPTY',
61
        self::T_TRUE              => 'T_TRUE',
62
        self::T_FALSE             => 'T_FALSE',
63
    ];
64
65
    /**
66
     * @var string
67
     */
68
    private $value;
69
    /**
70
     * @var int
71
     */
72
    private $type;
73
    /**
74
     * @var int
75
     */
76
    private $start;
77
    /**
78
     * @var int
79
     */
80
    private $end;
81
82
    /**
83
     * @param int $type
84
     * @param string $value
85
     * @param int $start
86
     * @param int $end
87
     */
88 85
    public function __construct($type, $value, $start, $end)
89
    {
90 85
        $this->type  = $type;
91 85
        $this->value = $value;
92 85
        $this->start = $start;
93 85
        $this->end   = $end;
94 85
    }
95
96
    /**
97
     * @return string
98
     */
99
    public function __toString()
100
    {
101
        try {
102
            return sprintf('%s(%s)', $this->getName(), $this->value);
103
        } catch (\Exception $e) {
104
            return sprintf('%s(%s)', 'UNKNOWN', $this->value);
105
        }
106
    }
107
108
    /**
109
     * @param int|int[] $type
110
     * @param string|string[] $value
111
     * @return bool
112
     */
113 64
    public function test($type, $value = null)
114
    {
115 64
        if (!in_array($this->type, (array)$type, true)) {
116 64
            return false;
117 63
        } elseif ($value !== null) {
118 63
            return in_array($this->value, (array)$value, true);
119
        } else {
120 63
            return true;
121
        }
122
    }
123
124
    /**
125
     * @return int
126
     */
127 81
    public function getType()
128
    {
129 81
        return $this->type;
130
    }
131
132
    /**
133
     * @return string
134
     */
135 81
    public function getValue()
136
    {
137 81
        return $this->value;
138
    }
139
140
    /**
141
     * @return int
142
     */
143 1
    public function getStart()
144
    {
145 1
        return $this->start;
146
    }
147
148
    /**
149
     * @return int
150
     */
151 85
    public function getEnd()
152
    {
153 85
        return $this->end;
154
    }
155
156
    /**
157
     * @return string
158
     */
159 11
    public function getName()
160
    {
161 11
        return self::getTypeName($this->type);
162
    }
163
164
    /**
165
     * @param int $type
166
     * @return string
167
     * @throws UnknownTokenException
168
     */
169 28
    public static function getTypeName($type)
170
    {
171 28
        if (!isset(static::$typeNameMap[$type])) {
172
            throw new UnknownTokenException(sprintf('Token of type "%s" does not exist', $type));
173
        }
174
175 28
        return static::$typeNameMap[$type];
176
    }
177
}
178