-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToken.php
More file actions
135 lines (121 loc) · 3.16 KB
/
Token.php
File metadata and controls
135 lines (121 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
* Class Token
*
* Represents the query token.
*/
class Token
{
const TOKEN_SELECT = 'SELECT';
const TOKEN_WHERE = 'WHERE';
const TOKEN_FROM = 'FROM';
const TOKEN_NOT = 'NOT';
const TOKEN_LIMIT = 'LIMIT';
const TOKEN_ROOT = 'ROOT';
const TOKEN_CONTAINS = 'CONTAINS';
const TOKEN_ORDER_BY = 'ORDER_BY';
const TOKEN_DESC = 'DESC';
const TOKEN_ASC = 'ASC';
const TOKEN_SPACE = 'SPACE';
const TOKEN_OPERATOR_MORE = 'OPERATOR_MORE';
const TOKEN_OPERATOR_LESS = 'OPERATOR_LESS';
const TOKEN_OPERATOR_EQUALS = 'OPERATOR_EQUALS';
const TOKEN_ATTRIBUTE = 'ATTRIBUTE';
const TOKEN_ELEMENT_WITH_ATTRIBUTE = 'ELEMENT_WITH_ATTRIBUTE';
const TOKEN_ELEMENT = 'ELEMENT';
const TOKEN_INTEGER = 'INTEGER';
const TOKEN_STRING = 'STRING';
const ALL_TOKENS = [
self::TOKEN_SELECT, self::TOKEN_WHERE, self::TOKEN_FROM, self::TOKEN_NOT, self::TOKEN_LIMIT, self::TOKEN_ROOT,
self::TOKEN_CONTAINS, self::TOKEN_ORDER_BY, self::TOKEN_DESC, self::TOKEN_ASC, self::TOKEN_SPACE,
self::TOKEN_OPERATOR_MORE, self::TOKEN_OPERATOR_LESS, self::TOKEN_OPERATOR_EQUALS,self::TOKEN_ATTRIBUTE,
self::TOKEN_ELEMENT_WITH_ATTRIBUTE, self::TOKEN_ELEMENT, self::TOKEN_INTEGER, self::TOKEN_STRING
];
/**
* @var string
*/
protected $type;
/**
* @var mixed
*/
protected $value;
/**
* Token constructor.
*
* @param string $type
* @param mixed $value
*/
public function __construct($type, $value)
{
$this->type = $type;
$this->value = $value;
}
/**
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* @param string $type
*
* @throws \Exception
*/
public function setType($type)
{
if (!in_array($type, self::ALL_TOKENS, true)) {
throw new \Exception(
'Invalid token type ' . $type . 'The type must be one of: ' . implode(',', self::ALL_TOKENS)
);
}
$this->type = $type;
}
/**
* @return mixed
*/
public function getValue()
{
return $this->value;
}
/**
* @param mixed $value
*/
public function setValue($value)
{
$this->value = $value;
}
/**
* @return bool
*/
public function isElementOrAttribute()
{
return $this->type === self::TOKEN_ELEMENT ||
$this->type === self::TOKEN_ELEMENT_WITH_ATTRIBUTE ||
$this->type === self::TOKEN_ATTRIBUTE;
}
/**
* @return bool
*/
public function isConditionOperator()
{
return $this->isArithmeticalOperator() ||
$this->type === self::TOKEN_CONTAINS;
}
/**
* @return bool
*/
public function isArithmeticalOperator()
{
return $this->type === self::TOKEN_OPERATOR_EQUALS ||
$this->type === self::TOKEN_OPERATOR_LESS ||
$this->type === self::TOKEN_OPERATOR_MORE;
}
/**
* @return bool
*/
public function isLiteral()
{
return $this->type === self::TOKEN_STRING || $this->type === self::TOKEN_INTEGER;
}
}