|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LaravelFreelancerNL\FluentAQL\Expressions; |
|
4
|
|
|
|
|
5
|
|
|
use LaravelFreelancerNL\FluentAQL\Exceptions\ExpressionTypeException; |
|
6
|
|
|
use LaravelFreelancerNL\FluentAQL\QueryBuilder; |
|
7
|
|
|
|
|
8
|
|
|
class ArithmeticExpression extends PredicateExpression implements ExpressionInterface |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
protected $calculation = []; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Create predicate expression. |
|
15
|
|
|
* |
|
16
|
|
|
* @param string $leftOperand |
|
17
|
|
|
* @param string $rightOperand |
|
18
|
|
|
* @param string $operator |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct($leftOperand, $operator, $rightOperand) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->calculation = [$leftOperand, $operator, $rightOperand]; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Compile calculation. |
|
27
|
|
|
* |
|
28
|
|
|
* @param QueryBuilder|null $queryBuilder |
|
29
|
|
|
* @return string |
|
30
|
|
|
* @throws \Exception |
|
31
|
|
|
*/ |
|
32
|
|
|
public function compile(QueryBuilder $queryBuilder = null): string |
|
33
|
|
|
{ |
|
34
|
|
|
$normalizedCalculation = $this->normalizeCalculation($queryBuilder, $this->calculation); |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
$leftOperand = $normalizedCalculation['leftOperand']->compile($queryBuilder); |
|
37
|
|
|
if ($normalizedCalculation['leftOperand'] instanceof ArithmeticExpression) { |
|
|
|
|
|
|
38
|
|
|
$leftOperand = '(' . $leftOperand . ')'; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$rightOperand = $normalizedCalculation['rightOperand']->compile($queryBuilder); |
|
42
|
|
|
if ($normalizedCalculation['rightOperand'] instanceof ArithmeticExpression) { |
|
|
|
|
|
|
43
|
|
|
$rightOperand = '(' . $rightOperand . ')'; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $leftOperand . ' ' . $normalizedCalculation['arithmeticOperator'] . ' ' . $rightOperand; |
|
47
|
|
|
return $leftOperand . ' ' . $normalizedCalculation['arithmeticOperator'] . ' ' . $rightOperand; |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param QueryBuilder $queryBuilder |
|
52
|
|
|
* @param array $calculation |
|
53
|
|
|
* @return mixed |
|
54
|
|
|
* @throws ExpressionTypeException |
|
55
|
|
|
*/ |
|
56
|
|
|
public function normalizeCalculation(QueryBuilder $queryBuilder, array $calculation) |
|
57
|
|
|
{ |
|
58
|
|
|
$normalizedCalculation = []; |
|
59
|
|
|
|
|
60
|
|
|
$leftOperand = $queryBuilder->normalizeArgument($calculation[0]); |
|
61
|
|
|
|
|
62
|
|
|
$arithmeticOperator = '+'; |
|
63
|
|
|
if ($queryBuilder->grammar->isArithmeticOperator($calculation[1])) { |
|
64
|
|
|
$arithmeticOperator = $calculation[1]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$rightOperand = $queryBuilder->normalizeArgument($calculation[2]); |
|
68
|
|
|
|
|
69
|
|
|
$normalizedCalculation['leftOperand'] = $leftOperand; |
|
70
|
|
|
$normalizedCalculation['arithmeticOperator'] = $arithmeticOperator; |
|
71
|
|
|
$normalizedCalculation['rightOperand'] = $rightOperand; |
|
72
|
|
|
|
|
73
|
|
|
return $normalizedCalculation; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|