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