1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LaravelFreelancerNL\FluentAQL\AQL; |
6
|
|
|
|
7
|
|
|
use LaravelFreelancerNL\FluentAQL\Expressions\Expression; |
8
|
|
|
use LaravelFreelancerNL\FluentAQL\Expressions\FunctionExpression; |
9
|
|
|
use LaravelFreelancerNL\FluentAQL\QueryBuilder; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Array AQL functions. |
13
|
|
|
* |
14
|
|
|
* @see https://www.arangodb.com/docs/stable/aql/functions-array.html |
15
|
|
|
*/ |
16
|
|
|
trait HasArrayFunctions |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Add all elements of an array to another array. |
20
|
|
|
* |
21
|
|
|
* @link https://www.arangodb.com/docs/stable/aql/functions-array.html#append |
22
|
|
|
* |
23
|
|
|
* @param array<mixed>|object $array |
24
|
|
|
* @param mixed $values |
25
|
|
|
* @param bool|null $unique |
26
|
|
|
* @return FunctionExpression |
27
|
|
|
*/ |
28
|
2 |
|
public function append(mixed $array, mixed $values, bool $unique = null): FunctionExpression |
29
|
|
|
{ |
30
|
2 |
|
$arguments = [ |
31
|
2 |
|
'array' => $array, |
32
|
2 |
|
'values' => $values, |
33
|
|
|
]; |
34
|
2 |
|
if (isset($unique)) { |
35
|
1 |
|
$arguments['unique'] = $unique; |
36
|
|
|
} |
37
|
2 |
|
return new FunctionExpression('APPEND', $arguments); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* This is an alias for LENGTH(). |
42
|
|
|
* |
43
|
|
|
* @link https://www.arangodb.com/docs/stable/aql/functions-array.html#length |
44
|
|
|
* |
45
|
|
|
* @param mixed $value |
46
|
|
|
* @return FunctionExpression |
47
|
|
|
*/ |
48
|
1 |
|
public function count($value): FunctionExpression |
49
|
|
|
{ |
50
|
1 |
|
return $this->length($value); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get the number of unique elements. |
55
|
|
|
* |
56
|
|
|
* @link https://www.arangodb.com/docs/stable/aql/functions-array.html#count_distinct |
57
|
|
|
* |
58
|
|
|
* @param mixed $value |
59
|
|
|
* |
60
|
|
|
* @return FunctionExpression |
61
|
|
|
*/ |
62
|
1 |
|
public function countDistinct($value): FunctionExpression |
63
|
|
|
{ |
64
|
1 |
|
return new FunctionExpression('COUNT_DISTINCT', [$value]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the first element of an array. |
69
|
|
|
* |
70
|
|
|
* @link https://www.arangodb.com/docs/stable/aql/functions-array.html#first |
71
|
|
|
* |
72
|
|
|
* @param mixed $value |
73
|
|
|
* |
74
|
|
|
* @return FunctionExpression |
75
|
|
|
*/ |
76
|
2 |
|
public function first($value): FunctionExpression |
77
|
|
|
{ |
78
|
2 |
|
return new FunctionExpression('FIRST', [$value]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Turn an array of arrays into a flat array. |
83
|
|
|
* |
84
|
|
|
* @link https://www.arangodb.com/docs/stable/aql/functions-array.html#flatten |
85
|
|
|
* |
86
|
|
|
* @param array<mixed>|object $array |
87
|
|
|
* @param int|object $depth |
88
|
|
|
* @return FunctionExpression |
89
|
|
|
*/ |
90
|
1 |
|
public function flatten(mixed $array, mixed $depth = 1): FunctionExpression |
91
|
|
|
{ |
92
|
1 |
|
$arguments = [ |
93
|
1 |
|
'array' => $array, |
94
|
1 |
|
'depth' => $depth, |
95
|
|
|
]; |
96
|
|
|
|
97
|
1 |
|
return new FunctionExpression('FLATTEN', $arguments); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the last element of an array. |
103
|
|
|
* |
104
|
|
|
* @link https://www.arangodb.com/docs/stable/aql/functions-array.html#last |
105
|
|
|
* |
106
|
|
|
* @param mixed $value |
107
|
|
|
* |
108
|
|
|
* @return FunctionExpression |
109
|
|
|
*/ |
110
|
1 |
|
public function last($value): FunctionExpression |
111
|
|
|
{ |
112
|
1 |
|
return new FunctionExpression('LAST', [$value]); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @link https://www.arangodb.com/docs/stable/aql/functions-array.html#length |
117
|
|
|
* |
118
|
|
|
* @param mixed $value |
119
|
|
|
* |
120
|
|
|
* @return FunctionExpression |
121
|
|
|
*/ |
122
|
2 |
|
public function length(mixed $value): FunctionExpression |
123
|
|
|
{ |
124
|
2 |
|
return new FunctionExpression('LENGTH', [$value]); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Remove the first element of an array. |
129
|
|
|
* |
130
|
|
|
* @link https://www.arangodb.com/docs/stable/aql/functions-array.html#shift |
131
|
|
|
* |
132
|
|
|
* @param array<mixed>|object $array |
133
|
|
|
* @return FunctionExpression |
134
|
|
|
*/ |
135
|
1 |
|
public function shift(mixed $array): FunctionExpression |
136
|
|
|
{ |
137
|
1 |
|
return new FunctionExpression('SHIFT', [$array]); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Return the union of all arrays specified. |
142
|
|
|
* |
143
|
|
|
* @link https://www.arangodb.com/docs/stable/aql/functions-array.html#union |
144
|
|
|
* |
145
|
|
|
* @param array<mixed>|Expression|QueryBuilder $arrays |
146
|
|
|
* @return FunctionExpression |
147
|
|
|
*/ |
148
|
1 |
|
public function union( |
149
|
|
|
array|Expression|QueryBuilder ...$arrays |
150
|
|
|
): FunctionExpression { |
151
|
1 |
|
return new FunctionExpression('UNION', [$arrays]); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Return the union of distinct values of all arrays specified. |
156
|
|
|
* |
157
|
|
|
* @link https://www.arangodb.com/docs/stable/aql/functions-array.html#union_distinct |
158
|
|
|
* |
159
|
|
|
* @param array<mixed>|Expression|QueryBuilder $arrays |
160
|
|
|
* @return FunctionExpression |
161
|
|
|
*/ |
162
|
1 |
|
public function unionDistinct( |
163
|
|
|
array|Expression|QueryBuilder ...$arrays |
164
|
|
|
): FunctionExpression { |
165
|
1 |
|
return new FunctionExpression('UNION_DISTINCT', [$arrays]); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Return all unique elements in an Array. |
170
|
|
|
* |
171
|
|
|
* @link https://www.arangodb.com/docs/stable/aql/functions-array.html#unique |
172
|
|
|
* |
173
|
|
|
* @param array<mixed>|object $array |
174
|
|
|
* @return FunctionExpression |
175
|
|
|
*/ |
176
|
1 |
|
public function unique(mixed $array): FunctionExpression |
177
|
|
|
{ |
178
|
1 |
|
return new FunctionExpression('UNIQUE', [$array]); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|