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