1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFreelancerNL\FluentAQL\Traits; |
4
|
|
|
|
5
|
|
|
use LaravelFreelancerNL\FluentAQL\Expressions\PredicateExpression; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Trait hasFunctions. |
9
|
|
|
* |
10
|
|
|
* AQL Function API calls. |
11
|
|
|
*/ |
12
|
|
|
trait CompilesPredicates |
13
|
|
|
{ |
14
|
13 |
|
public function compilePredicates(PredicateExpression|array $predicates): string |
15
|
|
|
{ |
16
|
13 |
|
if (! is_array($predicates)) { |
|
|
|
|
17
|
5 |
|
$predicates = [$predicates]; |
18
|
|
|
} |
19
|
|
|
|
20
|
13 |
|
$compiledPredicates = []; |
21
|
13 |
|
for ($i = 0; $i < count($predicates); $i++) { |
|
|
|
|
22
|
13 |
|
if ($predicates[$i] instanceof PredicateExpression) { |
23
|
13 |
|
$compiledPredicates[] = $this->compilePredicate($predicates[$i], $i); |
24
|
|
|
} |
25
|
|
|
|
26
|
13 |
|
if (is_array($predicates[$i])) { |
27
|
2 |
|
$compiledPredicates[] = $this->compilePredicateGroup($predicates[$i], $i); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
13 |
|
return implode(' ', $compiledPredicates); |
32
|
|
|
} |
33
|
|
|
|
34
|
13 |
|
protected function compilePredicate(PredicateExpression $predicate, int $position = 0): string |
35
|
|
|
{ |
36
|
13 |
|
$compiledPredicate = ''; |
37
|
13 |
|
if ($position > 0) { |
38
|
7 |
|
$compiledPredicate = $predicate->logicalOperator . ' ' ; |
39
|
|
|
} |
40
|
13 |
|
return $compiledPredicate . $predicate->compile($this); |
41
|
|
|
} |
42
|
|
|
|
43
|
2 |
|
protected function compilePredicateGroup(array $predicates, $position = 0): string |
44
|
|
|
{ |
45
|
2 |
|
$compiledPredicates = []; |
46
|
2 |
|
$logicalOperator = ''; |
47
|
2 |
|
if ($predicates[0] instanceof PredicateExpression) { |
48
|
2 |
|
$logicalOperator = $predicates[0]->logicalOperator; |
49
|
|
|
} |
50
|
2 |
|
for ($i = 0; $i < count($predicates); $i++) { |
|
|
|
|
51
|
2 |
|
$compiledPredicates[] = $this->compilePredicate($predicates[$i], $i); |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
$groupCompilation = ''; |
55
|
2 |
|
if ($position > 0) { |
56
|
1 |
|
$groupCompilation = $logicalOperator . ' '; |
57
|
|
|
} |
58
|
2 |
|
return $groupCompilation . '(' . implode(' ', $compiledPredicates) . ')'; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|