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
156
157
158
|
<?php
namespace ipl\Sql;
/**
* Implementation for the {@link WhereInterface}
*/
trait Where
{
/** @var array|null Internal representation for the WHERE part of the query */
protected $where;
public function getWhere()
{
return $this->where;
}
public function where($condition, ...$args)
{
list($condition, $operator) = $this->prepareConditionArguments($condition, $args);
$this->mergeCondition($this->where, $this->buildCondition($condition, $operator), Sql::ALL);
return $this;
}
public function orWhere($condition, ...$args)
{
list($condition, $operator) = $this->prepareConditionArguments($condition, $args);
$this->mergeCondition($this->where, $this->buildCondition($condition, $operator), Sql::ANY);
return $this;
}
public function notWhere($condition, ...$args)
{
list($condition, $operator) = $this->prepareConditionArguments($condition, $args);
$this->mergeCondition($this->where, $this->buildCondition($condition, $operator), Sql::NOT_ALL);
return $this;
}
public function orNotWhere($condition, ...$args)
{
list($condition, $operator) = $this->prepareConditionArguments($condition, $args);
$this->mergeCondition($this->where, $this->buildCondition($condition, $operator), Sql::NOT_ANY);
return $this;
}
public function resetWhere()
{
$this->where = null;
return $this;
}
/**
* Make $condition an array and build an array like this: [$operator, [$condition]]
*
* If $condition is empty, replace it with a boolean constant depending on the operator.
*
* @param string|array $condition
* @param string $operator
*
* @return array
*/
protected function buildCondition($condition, $operator)
{
if (is_array($condition)) {
if (empty($condition)) {
$condition = [$operator === Sql::ALL ? '1' : '0'];
} elseif (in_array(reset($condition), [Sql::ALL, Sql::ANY, Sql::NOT_ALL, Sql::NOT_ANY], true)) {
return $condition;
}
} else {
$condition = [$condition];
}
return [$operator, $condition];
}
/**
* Merge the given condition with ours via the given operator
*
* @param mixed $base Our condition
* @param array $condition As returned by {@link buildCondition()}
* @param string $operator
*/
protected function mergeCondition(&$base, array $condition, $operator)
{
if ($base === null) {
$base = [$operator, [$condition]];
} else {
if ($base[0] === $operator) {
$base[1][] = $condition;
} elseif ($operator === Sql::NOT_ALL) {
$base = [Sql::ALL, [$base, [$operator, [$condition]]]];
} elseif ($operator === Sql::NOT_ANY) {
$base = [Sql::ANY, [$base, [$operator, [$condition]]]];
} else {
$base = [$operator, [$base, $condition]];
}
}
}
/**
* Prepare condition arguments from the different supported where styles
*
* @param mixed $condition
* @param array $args
*
* @return array
*/
protected function prepareConditionArguments($condition, array $args)
{
// Default operator
$operator = Sql::ALL;
if (! is_array($condition) && ! empty($args)) {
// Variadic
$condition = [(string) $condition => $args];
} else {
// Array or string format
$operator = array_shift($args) ?: $operator;
}
return [$condition, $operator];
}
/**
* Clone the properties provided by this trait
*
* Shall be called by using classes in their __clone()
*/
protected function cloneWhere()
{
if ($this->where !== null) {
$this->cloneCondition($this->where);
}
}
/**
* Clone a condition in-place
*
* @param array $condition As returned by {@link buildCondition()}
*/
protected function cloneCondition(array &$condition)
{
foreach ($condition as &$subCondition) {
if (is_array($subCondition)) {
$this->cloneCondition($subCondition);
} elseif ($subCondition instanceof ExpressionInterface || $subCondition instanceof Select) {
$subCondition = clone $subCondition;
}
}
unset($subCondition);
}
}
|