PredicateExpression::compile()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 3
b 0
f 0
nc 2
nop 1
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 3
rs 10
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 PredicateExpression extends Expression implements ExpressionInterface
11
{
12
    /**
13
     * @var object|array<mixed>|string|int|float|bool|null
14
     */
15
    protected object|array|string|int|float|bool|null $leftOperand;
16
17
    protected string|null $comparisonOperator;
18
19
    /**
20
     * @var object|array<mixed>|string|int|float|bool|null
21
     */
22
    protected object|array|string|int|float|bool|null $rightOperand;
23
24
    public string $logicalOperator;
25
26
    /**
27
     * Create predicate expression.
28
     *
29
     * @param object|array<mixed>|string|int|float|bool|null $leftOperand
30
     * @param ?string $comparisonOperator
31
     * @param object|array<mixed>|string|int|float|bool|null $rightOperand
32
     * @param string $logicalOperator
33
     */
34 10
    public function __construct(
35
        object|array|string|int|float|bool|null $leftOperand,
36
        ?string $comparisonOperator = null,
37
        object|array|string|int|float|bool|null $rightOperand = null,
38
        string $logicalOperator = 'AND'
39
    ) {
40 10
        $this->leftOperand = $leftOperand;
41 10
        $this->comparisonOperator = strtoupper((string) $comparisonOperator);
42 10
        $this->rightOperand = $rightOperand;
43 10
        $this->logicalOperator = strtoupper($logicalOperator);
44
    }
45
46
    /**
47
     * Compile predicate string.
48
     *
49
     * @throws ExpressionTypeException
50
     */
51 10
    public function compile(QueryBuilder $queryBuilder): string
52
    {
53 10
        $leftOperand = $queryBuilder->normalizeArgument($this->leftOperand);
54
55 10
        $compiledPredicate = $leftOperand->compile($queryBuilder);
56 10
        if (isset($this->comparisonOperator) && $this->comparisonOperator !== '') {
57 9
            $compiledPredicate .= ' ' . $this->comparisonOperator;
58
59 9
            $rightOperand = $queryBuilder->normalizeArgument($this->rightOperand);
60
61 9
            $compiledPredicate .= ' ' . $rightOperand->compile($queryBuilder);
62
        }
63 10
        return $compiledPredicate;
64
    }
65
}
66