|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LaravelFreelancerNL\FluentAQL; |
|
6
|
|
|
|
|
7
|
|
|
use LaravelFreelancerNL\FluentAQL\Traits\ValidatesExpressions; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Provides AQL syntax functions |
|
11
|
|
|
*/ |
|
12
|
|
|
class Grammar |
|
13
|
|
|
{ |
|
14
|
|
|
use ValidatesExpressions; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Available predicate operators. |
|
18
|
|
|
* |
|
19
|
|
|
* @var array<string, int> |
|
20
|
|
|
*/ |
|
21
|
|
|
protected array $comparisonOperators = [ |
|
22
|
|
|
'==' => 1, |
|
23
|
|
|
'!=' => 1, |
|
24
|
|
|
'<' => 1, |
|
25
|
|
|
'>' => 1, |
|
26
|
|
|
'<=' => 1, |
|
27
|
|
|
'>=' => 1, |
|
28
|
|
|
'IN' => 1, |
|
29
|
|
|
'NOT IN' => 1, |
|
30
|
|
|
'LIKE' => 1, |
|
31
|
|
|
'~' => 1, |
|
32
|
|
|
'!~' => 1, |
|
33
|
|
|
'ALL ==' => 1, |
|
34
|
|
|
'ALL !=' => 1, |
|
35
|
|
|
'ALL <' => 1, |
|
36
|
|
|
'ALL >' => 1, |
|
37
|
|
|
'ALL <=' => 1, |
|
38
|
|
|
'ALL >=' => 1, |
|
39
|
|
|
'ALL IN' => 1, |
|
40
|
|
|
'ANY ==' => 1, |
|
41
|
|
|
'ANY !=' => 1, |
|
42
|
|
|
'ANY <' => 1, |
|
43
|
|
|
'ANY >' => 1, |
|
44
|
|
|
'ANY <=' => 1, |
|
45
|
|
|
'ANY >=' => 1, |
|
46
|
|
|
'ANY IN' => 1, |
|
47
|
|
|
'NONE ==' => 1, |
|
48
|
|
|
'NONE !=' => 1, |
|
49
|
|
|
'NONE <' => 1, |
|
50
|
|
|
'NONE >' => 1, |
|
51
|
|
|
'NONE <=' => 1, |
|
52
|
|
|
'NONE >=' => 1, |
|
53
|
|
|
'NONE IN' => 1, |
|
54
|
|
|
]; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var array|int[] |
|
58
|
|
|
*/ |
|
59
|
|
|
protected array $arithmeticOperators = [ |
|
60
|
|
|
'+' => 1, |
|
61
|
|
|
'-' => 1, |
|
62
|
|
|
'*' => 1, |
|
63
|
|
|
'/' => 1, |
|
64
|
|
|
'%' => 1, |
|
65
|
|
|
]; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var array|int[] |
|
69
|
|
|
*/ |
|
70
|
|
|
protected array $logicalOperators = [ |
|
71
|
|
|
'AND' => 1, |
|
72
|
|
|
'&&' => 1, |
|
73
|
|
|
'OR' => 1, |
|
74
|
|
|
'||' => 1, |
|
75
|
|
|
'NOT' => 1, |
|
76
|
|
|
'!' => 1, |
|
77
|
|
|
]; |
|
78
|
|
|
|
|
79
|
|
|
protected string $rangeOperator = '..'; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @var string[] |
|
83
|
|
|
*/ |
|
84
|
|
|
protected array $keywords = [ |
|
85
|
|
|
'FOR', 'FILTER', 'SEARCH', 'SORT', 'ASC', 'DESC', 'LIMIT', 'COLLECT', 'INTO', 'AGGREGATE', 'RETURN', 'DISTINCT', |
|
86
|
|
|
'WITH', 'GRAPH', 'INBOUND', 'OUTBOUND', 'ANY', 'SHORTEST_PATH', 'K_SHORTEST_PATH', 'PRUNE', |
|
87
|
|
|
'LET', 'INSERT', 'UPDATE', 'REPLACE', 'UPSERT', 'REMOVE', |
|
88
|
|
|
'ALL', 'NONE', 'AND', 'OR', 'NOT', 'LIKE', 'IN', |
|
89
|
|
|
'FALSE', 'TRUE', 'NULL', |
|
90
|
|
|
]; |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* List of recognizable data and the accompanying Expression type it will be mapped too. |
|
94
|
|
|
* Strings of an unrecognized nature are always bound. |
|
95
|
|
|
* |
|
96
|
|
|
* @var string[] |
|
97
|
|
|
*/ |
|
98
|
|
|
protected array $argumentTypeExpressionMap = [ |
|
99
|
|
|
'AssociativeArray' => 'Object', |
|
100
|
|
|
'Attribute' => 'Literal', |
|
101
|
|
|
'Bind' => 'Bind', |
|
102
|
|
|
'CollectionBind' => 'CollectionBind', |
|
103
|
|
|
'Boolean' => 'Boolean', |
|
104
|
|
|
'Collection' => 'Literal', |
|
105
|
|
|
'Constant' => 'Constant', |
|
106
|
|
|
'GraphDirection' => 'Constant', |
|
107
|
|
|
'Document' => 'Object', |
|
108
|
|
|
'Function' => 'Function', |
|
109
|
|
|
'Graph' => 'String', |
|
110
|
|
|
'Id' => 'String', |
|
111
|
|
|
'IndexedArray' => 'List', |
|
112
|
|
|
'Key' => 'String', |
|
113
|
|
|
'List' => 'List', |
|
114
|
|
|
'Name' => 'String', |
|
115
|
|
|
'Number' => 'Literal', |
|
116
|
|
|
'Null' => 'Literal', |
|
117
|
|
|
'RegisteredVariable' => 'Literal', |
|
118
|
|
|
'Variable' => 'Literal', |
|
119
|
|
|
'Reference' => 'Literal', |
|
120
|
|
|
'Object' => 'Object', |
|
121
|
|
|
'Range' => 'Literal', |
|
122
|
|
|
'String' => 'Bind', |
|
123
|
|
|
]; |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* List of default allowed Data Types |
|
127
|
|
|
* The order matters in the compilation of the data |
|
128
|
|
|
* |
|
129
|
|
|
* @var array|string[] |
|
130
|
|
|
*/ |
|
131
|
|
|
protected array $defaultAllowedExpressionTypes = [ |
|
132
|
|
|
'Number' => 'Number', |
|
133
|
|
|
'Boolean' => 'Boolean', |
|
134
|
|
|
'Null' => 'Null', |
|
135
|
|
|
'Reference' => 'Reference', |
|
136
|
|
|
'Id' => 'Id', |
|
137
|
|
|
'Key' => 'Key', |
|
138
|
|
|
'Bind' => 'Bind', |
|
139
|
|
|
]; |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Get the format for database stored dates. |
|
143
|
|
|
* ArangoDB's date functions support ISO8601 strings up to milliseconds. We default to Zulu time (UTC) |
|
144
|
|
|
* |
|
145
|
|
|
* @return string |
|
146
|
1 |
|
*/ |
|
147
|
|
|
public function getDateFormat() |
|
148
|
1 |
|
{ |
|
149
|
|
|
return 'Y-m-d\TH:i:s.u\Z'; |
|
150
|
|
|
} |
|
151
|
1 |
|
|
|
152
|
|
|
public function wrap( |
|
153
|
|
|
string $value |
|
154
|
1 |
|
): string { |
|
155
|
|
|
return '`' . addcslashes($value, '`') . '`'; |
|
156
|
|
|
} |
|
157
|
5 |
|
|
|
158
|
|
|
public function mapArgumentTypeToExpressionType( |
|
159
|
|
|
string $argumentType |
|
160
|
5 |
|
): string { |
|
161
|
|
|
return $this->argumentTypeExpressionMap[$argumentType]; |
|
162
|
|
|
} |
|
163
|
2 |
|
|
|
164
|
|
|
public function formatBind( |
|
165
|
|
|
string $bindVariableName, |
|
166
|
|
|
bool $collection = null |
|
167
|
2 |
|
): string { |
|
168
|
1 |
|
if (stripos($bindVariableName, '@') === 0) { |
|
169
|
|
|
$bindVariableName = $this->wrap($bindVariableName); |
|
170
|
|
|
} |
|
171
|
2 |
|
|
|
172
|
2 |
|
$prefix = '@'; |
|
173
|
1 |
|
if ($collection) { |
|
174
|
|
|
$prefix = '@@'; |
|
175
|
|
|
} |
|
176
|
2 |
|
|
|
177
|
|
|
return $prefix . $bindVariableName; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @return array|string[] |
|
182
|
6 |
|
*/ |
|
183
|
|
|
public function getAllowedExpressionTypes(): array |
|
184
|
6 |
|
{ |
|
185
|
|
|
return $this->defaultAllowedExpressionTypes; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @return array<string, int> |
|
190
|
|
|
*/ |
|
191
|
|
|
public function getComparisonOperators(): array |
|
192
|
|
|
{ |
|
193
|
|
|
return $this->comparisonOperators; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|