Passed
Push — master ( b4f91d...b5810c )
by Bas
07:20 queued 04:57
created

HasQueryClauses   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 232
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 40
c 0
b 0
f 0
dl 0
loc 232
ccs 50
cts 50
cp 1
rs 10
wmc 17

12 Methods

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