Passed
Push — master ( 61ffee...578120 )
by Aleksandr
13:14
created

Expression   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Test Coverage

Coverage 98.39%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 56
dl 0
loc 126
ccs 61
cts 62
cp 0.9839
rs 10
c 1
b 0
f 0
wmc 29

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setField() 0 3 1
A setOperator() 0 3 1
A setParam1() 0 3 1
A setParam2() 0 3 1
A getParam2() 0 3 1
A getField() 0 3 1
A getOperator() 0 3 1
A getParam1() 0 3 1
D execute() 0 78 21
1
<?php
2
/**
3
 * This file is part of the eav package.
4
 * @author    Aleksandr Drobotik <[email protected]>
5
 * @copyright 2023 Aleksandr Drobotik
6
 * @license   https://opensource.org/license/mit  The MIT License
7
 */
8
declare(strict_types=1);
9
10
namespace Drobotik\Eav\QueryBuilder;
11
12
use Doctrine\DBAL\Query\Expression\CompositeExpression;
13
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
14
use Drobotik\Eav\Database\Connection;
15
use Drobotik\Eav\Enum\QB_OPERATOR;
16
17
class Expression
18
{
19
    private QB_OPERATOR $operator;
20
    private string      $field;
21
    private string      $param1;
22
    private string $param2;
23
24 1
    public function setOperator(QB_OPERATOR $operator): void
25
    {
26 1
        $this->operator = $operator;
27
    }
28
29 1
    public function getOperator() : QB_OPERATOR
30
    {
31 1
        return $this->operator;
32
    }
33
34 1
    public function setField(string $name): void
35
    {
36 1
        $this->field = $name;
37
    }
38
39 1
    public function getField() : string
40
    {
41 1
        return $this->field;
42
    }
43
44 1
    public function setParam1(string $name): void
45
    {
46 1
        $this->param1 = $name;
47
    }
48
49 1
    public function getParam1() : string
50
    {
51 1
        return ':'.$this->param1;
52
    }
53
54 1
    public function setParam2(string $name): void
55
    {
56 1
        $this->param2 = $name;
57
    }
58
59 1
    public function getParam2() : string
60
    {
61 1
        return ':'.$this->param2;
62
    }
63
64
65 1
    public function execute() : string|CompositeExpression
66
    {
67 1
        $expr = new ExpressionBuilder(Connection::get());
68
69 1
        switch ($this->getOperator()) {
70 1
            case QB_OPERATOR::EQUAL:
71
72 1
                return $expr->eq($this->getField(), $this->getParam1());
73
74 1
            case QB_OPERATOR::NOT_EQUAL:
75
76 1
                return $expr->neq($this->getField(), $this->getParam1());
77
78 1
            case QB_OPERATOR::IN:
79
80 1
                return $expr->in($this->getField(), $this->getParam1());
81
82 1
            case QB_OPERATOR::NOT_IN:
83
84 1
                return $expr->notIn($this->getField(), $this->getParam1());
85
86 1
            case QB_OPERATOR::LESS:
87
88 1
                return $expr->lt($this->getField(), $this->getParam1());
89
90 1
            case QB_OPERATOR::LESS_OR_EQUAL:
91
92 1
                return $expr->lte($this->getField(), $this->getParam1());
93
94 1
            case QB_OPERATOR::GREATER:
95
96 1
                return $expr->gt($this->getField(), $this->getParam1());
97
98 1
            case QB_OPERATOR::GREATER_OR_EQUAL:
99
100 1
                return $expr->gte($this->getField(), $this->getParam1());
101
102 1
            case QB_OPERATOR::BETWEEN:
103
104 1
                $field = $this->getField();
105 1
                return new CompositeExpression(CompositeExpression::TYPE_AND, [
106 1
                    $expr->gte($field, $this->getParam1()),
107 1
                    $expr->lte($field, $this->getParam2())
108 1
                ]);
109
110 1
            case QB_OPERATOR::NOT_BETWEEN:
111
112 1
                $field = $this->getField();
113 1
                return new CompositeExpression(CompositeExpression::TYPE_OR, [
114 1
                    $expr->lt($field, $this->getParam1()),
115 1
                    $expr->gt($field, $this->getParam2())
116 1
                ]);
117
118 1
            case QB_OPERATOR::CONTAINS:
119 1
            case QB_OPERATOR::ENDS_WITH:
120 1
            case QB_OPERATOR::BEGINS_WITH:
121
122 1
                return $expr->like($this->getField(), $this->getParam1());
123
124 1
            case QB_OPERATOR::NOT_CONTAINS:
125 1
            case QB_OPERATOR::NOT_ENDS_WITH:
126 1
            case QB_OPERATOR::NOT_BEGINS_WITH:
127
128 1
                return $expr->notLike($this->getField(), $this->getParam1());
129
130 1
            case QB_OPERATOR::IS_NULL:
131 1
            case QB_OPERATOR::IS_EMPTY:
132
133 1
                return $expr->isNull($this->getField());
134
135 1
            case QB_OPERATOR::IS_NOT_NULL:
136 1
            case QB_OPERATOR::IS_NOT_EMPTY:
137
138 1
                return $expr->isNotNull($this->getField());
139
140
        }
141
142
        return '';
143
    }
144
}