|
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
|
|
|
private ExpressionBuilder $exprBuilder; |
|
25
|
|
|
|
|
26
|
1 |
|
public function __construct() |
|
27
|
|
|
{ |
|
28
|
1 |
|
$this->exprBuilder = new ExpressionBuilder(Connection::get()); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
public function setOperator(QB_OPERATOR $operator): void |
|
32
|
|
|
{ |
|
33
|
1 |
|
$this->operator = $operator; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
1 |
|
public function getOperator() : QB_OPERATOR |
|
37
|
|
|
{ |
|
38
|
1 |
|
return $this->operator; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
public function setField(string $name): void |
|
42
|
|
|
{ |
|
43
|
1 |
|
$this->field = $name; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
public function getField() : string |
|
47
|
|
|
{ |
|
48
|
1 |
|
return $this->field; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
public function setParam1(string $name): void |
|
52
|
|
|
{ |
|
53
|
1 |
|
$this->param1 = $name; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
public function getParam1() : string |
|
57
|
|
|
{ |
|
58
|
1 |
|
return ':'.$this->param1; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
public function isParam1() : bool |
|
62
|
|
|
{ |
|
63
|
1 |
|
return isset($this->param1); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
public function setParam2(string $name): void |
|
67
|
|
|
{ |
|
68
|
1 |
|
$this->param2 = $name; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
public function getParam2() : string |
|
72
|
|
|
{ |
|
73
|
1 |
|
return ':'.$this->param2; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
public function isParam2() : bool |
|
77
|
|
|
{ |
|
78
|
1 |
|
return isset($this->param2); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private function between(): CompositeExpression |
|
82
|
|
|
{ |
|
83
|
|
|
$field = $this->getField(); |
|
84
|
|
|
return new CompositeExpression(CompositeExpression::TYPE_AND, [ |
|
85
|
|
|
$this->exprBuilder->gte($field, $this->getParam1()), |
|
86
|
|
|
$this->exprBuilder->lte($field, $this->getParam2()) |
|
87
|
|
|
]); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
private function notBetween(): CompositeExpression |
|
91
|
|
|
{ |
|
92
|
|
|
$field = $this->getField(); |
|
93
|
|
|
return new CompositeExpression(CompositeExpression::TYPE_OR, [ |
|
94
|
|
|
$this->exprBuilder->lt($field, $this->getParam1()), |
|
95
|
|
|
$this->exprBuilder->gt($field, $this->getParam2()) |
|
96
|
|
|
]); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
public function execute() : string|CompositeExpression |
|
100
|
|
|
{ |
|
101
|
1 |
|
$operator = $this->getOperator(); |
|
102
|
1 |
|
$method = $operator->expr(); |
|
103
|
|
|
// when need to call custom methods |
|
104
|
1 |
|
if($operator->isBetween()) { |
|
105
|
1 |
|
return call_user_func([$this, $method]); |
|
106
|
|
|
} |
|
107
|
|
|
// when need to call basic ExpressionBuilder methods |
|
108
|
1 |
|
$args = [$this->getField()]; |
|
109
|
1 |
|
if(!$operator->isNull()) |
|
110
|
1 |
|
$args[] = $this->getParam1(); |
|
111
|
1 |
|
return call_user_func_array([$this->exprBuilder, $method], $args); |
|
112
|
|
|
} |
|
113
|
|
|
} |