1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace LaravelFreelancerNL\FluentAQL\AQL; |
||
6 | |||
7 | use LaravelFreelancerNL\FluentAQL\Clauses\AggregateClause; |
||
8 | use LaravelFreelancerNL\FluentAQL\Clauses\CollectClause; |
||
9 | use LaravelFreelancerNL\FluentAQL\Clauses\FilterClause; |
||
10 | use LaravelFreelancerNL\FluentAQL\Clauses\ForClause; |
||
11 | use LaravelFreelancerNL\FluentAQL\Clauses\IntoClause; |
||
12 | use LaravelFreelancerNL\FluentAQL\Clauses\KeepClause; |
||
13 | use LaravelFreelancerNL\FluentAQL\Clauses\LimitClause; |
||
14 | use LaravelFreelancerNL\FluentAQL\Clauses\OptionsClause; |
||
15 | use LaravelFreelancerNL\FluentAQL\Clauses\ReturnClause; |
||
16 | use LaravelFreelancerNL\FluentAQL\Clauses\SearchClause; |
||
17 | use LaravelFreelancerNL\FluentAQL\Clauses\SortClause; |
||
18 | use LaravelFreelancerNL\FluentAQL\Clauses\WindowClause; |
||
19 | use LaravelFreelancerNL\FluentAQL\Clauses\WithCountClause; |
||
20 | use LaravelFreelancerNL\FluentAQL\Expressions\Expression; |
||
21 | use LaravelFreelancerNL\FluentAQL\QueryBuilder; |
||
22 | |||
23 | /** |
||
24 | * Trait hasQueryClauses |
||
25 | * API calls to add clause commands to the builder. |
||
26 | */ |
||
27 | trait HasQueryClauses |
||
28 | { |
||
29 | abstract public function addCommand($command); |
||
30 | |||
31 | /** |
||
32 | * Create a for clause. |
||
33 | * |
||
34 | * @link https://www.arangodb.com/docs/stable/aql/operations-for.html |
||
35 | * |
||
36 | * @param string|array<string|Expression>|Expression $variableName |
||
37 | * @param string|array<mixed>|QueryBuilder|Expression|null $in |
||
38 | */ |
||
39 | 31 | public function for( |
|
40 | string|array|Expression $variableName, |
||
41 | string|array|QueryBuilder|Expression $in = null |
||
42 | ): self { |
||
43 | 31 | if (!is_array($variableName)) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
44 | 28 | $variableName = [$variableName]; |
|
45 | } |
||
46 | |||
47 | 31 | $this->addCommand(new ForClause($variableName, $in)); |
|
48 | |||
49 | 31 | return $this; |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * Filter results from a for clause. |
||
54 | * |
||
55 | * @link https://www.arangodb.com/docs/stable/aql/operations-filter.html |
||
56 | * |
||
57 | * @param object|array<mixed>|string|int|float|bool|null $leftOperand |
||
58 | * @param object|array<mixed>|string|int|float|bool|null $rightOperand |
||
59 | */ |
||
60 | 14 | public function filter( |
|
61 | object|array|string|int|float|bool|null $leftOperand, |
||
62 | string $comparisonOperator = null, |
||
63 | object|array|string|int|float|bool|null $rightOperand = null, |
||
64 | string $logicalOperator = null |
||
65 | ): self { |
||
66 | 14 | $predicates = $leftOperand; |
|
67 | 14 | if (! is_array($predicates)) { |
|
0 ignored issues
–
show
|
|||
68 | 10 | $predicates = [[$leftOperand, $comparisonOperator, $rightOperand, $logicalOperator]]; |
|
69 | } |
||
70 | |||
71 | 14 | $this->addCommand(new FilterClause($predicates)); |
|
72 | |||
73 | 14 | return $this; |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * Search a view. |
||
78 | * |
||
79 | * @link https://www.arangodb.com/docs/stable/aql/operations-search.html |
||
80 | * |
||
81 | * @param object|array<mixed>|string|int|float|bool|null $leftOperand |
||
82 | * @param object|array<mixed>|string|int|float|bool|null $rightOperand |
||
83 | */ |
||
84 | 1 | public function search( |
|
85 | object|array|string|int|float|bool|null $leftOperand, |
||
86 | string $comparisonOperator = null, |
||
87 | object|array|string|int|float|bool|null $rightOperand = null, |
||
88 | string $logicalOperator = null |
||
89 | ): self { |
||
90 | 1 | $predicates = $leftOperand; |
|
91 | 1 | if (! is_array($predicates)) { |
|
0 ignored issues
–
show
|
|||
92 | 1 | $predicates = [$predicates]; |
|
93 | } |
||
94 | 1 | if (is_string($comparisonOperator)) { |
|
95 | 1 | $predicates = [[$leftOperand, $comparisonOperator, $rightOperand, $logicalOperator]]; |
|
96 | } |
||
97 | |||
98 | 1 | $this->addCommand(new SearchClause($predicates)); |
|
99 | |||
100 | 1 | return $this; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Collect clause. |
||
105 | * |
||
106 | * @link https://www.arangodb.com/docs/stable/aql/operations-collect.html |
||
107 | * |
||
108 | * @param array<array-key, array<array-key, string>>|string|null $variableName |
||
0 ignored issues
–
show
|
|||
109 | */ |
||
110 | 2 | public function collect( |
|
111 | string|array $variableName = null, |
||
112 | string $expression = null |
||
113 | ): self { |
||
114 | 2 | $groups = []; |
|
115 | 2 | if (is_string($variableName)) { |
|
0 ignored issues
–
show
|
|||
116 | 1 | $groups[0][0] = $variableName; |
|
117 | 1 | $groups[0][1] = $expression; |
|
118 | } |
||
119 | 2 | if (is_array($variableName)) { |
|
120 | 1 | $groups = $variableName; |
|
121 | } |
||
122 | |||
123 | 2 | $this->addCommand(new CollectClause($groups)); |
|
124 | |||
125 | 2 | return $this; |
|
126 | } |
||
127 | |||
128 | /** |
||
129 | * Group clause: |
||
130 | * Creates the INTO clause of a collect clause. |
||
131 | * |
||
132 | * @link https://www.arangodb.com/docs/stable/aql/operations-collect.html#grouping-syntaxes |
||
133 | */ |
||
134 | 1 | public function into( |
|
135 | string|QueryBuilder|Expression $groupsVariable, |
||
136 | mixed $projectionExpression = null |
||
137 | ): self { |
||
138 | 1 | $this->addCommand(new IntoClause($groupsVariable, $projectionExpression)); |
|
139 | |||
140 | 1 | return $this; |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * Keep clause |
||
145 | * Limits the attributes of the data that is grouped. |
||
146 | * |
||
147 | * @link https://www.arangodb.com/docs/stable/aql/operations-collect.html#discarding-obsolete-variables |
||
148 | */ |
||
149 | 1 | public function keep( |
|
150 | string|QueryBuilder|Expression $keepVariable |
||
151 | ): self { |
||
152 | 1 | $this->addCommand(new KeepClause($keepVariable)); |
|
153 | |||
154 | 1 | return $this; |
|
155 | } |
||
156 | |||
157 | /** |
||
158 | * withCount clause |
||
159 | * Count the collected and grouped data. |
||
160 | * withCount can only be used after an into clause. |
||
161 | * |
||
162 | * @link https://www.arangodb.com/docs/stable/aql/operations-collect.html#group-length-calculation |
||
163 | */ |
||
164 | 1 | public function withCount( |
|
165 | string|QueryBuilder|Expression $countVariableName |
||
166 | ): self { |
||
167 | 1 | $this->addCommand(new WithCountClause($countVariableName)); |
|
168 | |||
169 | 1 | return $this; |
|
170 | } |
||
171 | |||
172 | /** |
||
173 | * Aggregate clause |
||
174 | * Creates the INTO clause of a collect clause. |
||
175 | * |
||
176 | * @link https://www.arangodb.com/docs/stable/aql/operations-collect.html#aggregation |
||
177 | */ |
||
178 | 1 | public function aggregate( |
|
179 | string|QueryBuilder|Expression $variableName, |
||
180 | string|QueryBuilder|Expression $aggregateExpression |
||
181 | ): self { |
||
182 | 1 | $this->addCommand(new AggregateClause($variableName, $aggregateExpression)); |
|
183 | |||
184 | 1 | return $this; |
|
185 | } |
||
186 | |||
187 | /** |
||
188 | * Sort documents to return. |
||
189 | * |
||
190 | * @link https://www.arangodb.com/docs/stable/aql/operations-sort.html |
||
191 | * |
||
192 | * @param mixed ...$references |
||
193 | */ |
||
194 | 2 | public function sort(...$references): self |
|
195 | { |
||
196 | 2 | $this->addCommand(new SortClause($references)); |
|
197 | |||
198 | 2 | return $this; |
|
199 | } |
||
200 | |||
201 | /** |
||
202 | * Limit results. |
||
203 | * |
||
204 | * @link https://www.arangodb.com/docs/stable/aql/operations-limit.html |
||
205 | */ |
||
206 | 1 | public function limit( |
|
207 | int|QueryBuilder|Expression $offsetOrCount, |
||
208 | int|QueryBuilder|Expression $count = null |
||
209 | ): self { |
||
210 | 1 | $this->addCommand(new LimitClause($offsetOrCount, $count)); |
|
211 | |||
212 | 1 | return $this; |
|
213 | } |
||
214 | |||
215 | /** |
||
216 | * Return data. |
||
217 | * |
||
218 | * @link https://www.arangodb.com/docs/stable/aql/operations-return.html |
||
219 | * |
||
220 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
221 | */ |
||
222 | 13 | public function return( |
|
223 | mixed $expression, |
||
224 | bool $distinct = false |
||
225 | ): self { |
||
226 | 13 | $this->addCommand(new ReturnClause($expression, $distinct)); |
|
227 | |||
228 | 13 | return $this; |
|
229 | } |
||
230 | |||
231 | /** |
||
232 | * @param array<mixed>|object $options |
||
233 | */ |
||
234 | 1 | public function options(array|object $options): self |
|
235 | { |
||
236 | 1 | $this->addCommand(new OptionsClause($options)); |
|
237 | |||
238 | 1 | return $this; |
|
239 | } |
||
240 | |||
241 | /** |
||
242 | * Aggregate adjacent documents or value ranges with a sliding window to calculate |
||
243 | * running totals, rolling averages, and other statistical properties |
||
244 | * |
||
245 | * @link https://www.arangodb.com/docs/stable/aql/operations-window.html |
||
246 | * |
||
247 | * @param array<array-key, string>|object $offsets |
||
0 ignored issues
–
show
|
|||
248 | */ |
||
249 | 4 | public function window( |
|
250 | array|object $offsets, |
||
251 | string|QueryBuilder|Expression $rangeValue = null |
||
252 | ): self { |
||
253 | 4 | $this->addCommand(new WindowClause($offsets, $rangeValue)); |
|
254 | |||
255 | 4 | return $this; |
|
256 | } |
||
257 | } |
||
258 |