Repository navigation
Expand file tree
/
Copy pathOperator.php
More file actions
155 lines (123 loc) · 3.92 KB
/
Copy pathOperator.php
File metadata and controls
155 lines (123 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
declare(strict_types=1);
namespace PhpDb\Sql\Predicate;
use Override;
use PhpDb\Sql\AbstractExpression;
use PhpDb\Sql\Argument\Identifier;
use PhpDb\Sql\Argument\Select;
use PhpDb\Sql\Argument\Value;
use PhpDb\Sql\ArgumentInterface;
use PhpDb\Sql\Exception\InvalidArgumentException;
use PhpDb\Sql\ExpressionInterface;
use PhpDb\Sql\SqlInterface;
class Operator extends AbstractExpression implements PredicateInterface
{
final public const OPERATOR_EQUAL_TO = '=';
final public const OP_EQ = '=';
final public const OPERATOR_NOT_EQUAL_TO = '!=';
final public const OP_NE = '!=';
final public const OPERATOR_LESS_THAN = '<';
final public const OP_LT = '<';
final public const OPERATOR_LESS_THAN_OR_EQUAL_TO = '<=';
final public const OP_LTE = '<=';
final public const OPERATOR_GREATER_THAN = '>';
final public const OP_GT = '>';
final public const OPERATOR_GREATER_THAN_OR_EQUAL_TO = '>=';
final public const OP_GTE = '>=';
protected ?ArgumentInterface $left = null;
protected ?ArgumentInterface $right = null;
protected string $operator = self::OPERATOR_EQUAL_TO;
/**
* Constructor
*/
public function __construct(
null|string|ArgumentInterface|ExpressionInterface|SqlInterface $left = null,
string $operator = self::OPERATOR_EQUAL_TO,
null|bool|string|int|float|ArgumentInterface|ExpressionInterface|SqlInterface $right = null
) {
if ($left !== null) {
$this->setLeft($left);
}
if ($operator !== self::OPERATOR_EQUAL_TO) {
$this->setOperator($operator);
}
if ($right !== null) {
$this->setRight($right);
}
}
/**
* Get left side of operator
*/
public function getLeft(): ?ArgumentInterface
{
return $this->left;
}
/**
* Set left side of operator
*/
public function setLeft(string|ArgumentInterface|ExpressionInterface|SqlInterface $left): static
{
if ($left instanceof ArgumentInterface) {
$this->left = $left;
} elseif ($left instanceof ExpressionInterface || $left instanceof SqlInterface) {
$this->left = new Select($left);
} else {
$this->left = new Identifier($left);
}
return $this;
}
/**
* Get operator string
*/
public function getOperator(): string
{
return $this->operator;
}
/**
* Set operator string
*/
public function setOperator(string $operator): static
{
$this->operator = $operator;
return $this;
}
/**
* Get right side of operator
*/
public function getRight(): ?ArgumentInterface
{
return $this->right;
}
/**
* Set right side of operator
*/
public function setRight(
null|bool|string|int|float|ArgumentInterface|ExpressionInterface|SqlInterface $right
): static {
if ($right instanceof ArgumentInterface) {
$this->right = $right;
} elseif ($right instanceof ExpressionInterface || $right instanceof SqlInterface) {
$this->right = new Select($right);
} else {
$this->right = new Value($right);
}
return $this;
}
/** @inheritDoc */
#[Override]
public function getExpressionData(): array
{
if (! $this->left instanceof ArgumentInterface) {
throw new InvalidArgumentException('Left expression must be specified');
}
if (! $this->right instanceof ArgumentInterface) {
throw new InvalidArgumentException('Right expression must be specified');
}
$leftSpec = $this->left->getSpecification();
$rightSpec = $this->right->getSpecification();
return [
'spec' => $this->specification ?? "{$leftSpec} {$this->operator} {$rightSpec}",
'values' => [$this->left, $this->right],
];
}
}