-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWhereTrait.php
More file actions
167 lines (145 loc) · 4.79 KB
/
WhereTrait.php
File metadata and controls
167 lines (145 loc) · 4.79 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
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace ModernPDO\Traits;
use ModernPDO\Conditions\Condition;
use ModernPDO\Escaper;
use ModernPDO\Functions\Scalar\ScalarFunction;
/**
* Trait for working with 'where'.
*/
trait WhereTrait
{
/**
* @var list<array{type: string, name: string, sign: string, value: scalar|ScalarFunction|Condition|null}> where conditions
*/
protected array $where = [];
/**
* Returns set query.
*/
protected function whereQuery(Escaper $escaper): string
{
$query = '';
foreach ($this->where as [
'type' => $type,
'name' => $name,
'sign' => $sign,
'value' => $value,
]) {
if ($value instanceof ScalarFunction) {
$value = $value->buildQuery();
} elseif ($value instanceof Condition) {
$sign = ' ' . $value->buildSign() . ' ';
$value = $value->buildQuery();
} elseif (\is_bool($value)) {
$value = $escaper->boolValue($value);
} else {
$value = '?';
}
$query .= $type . ' ' . $escaper->column($name) . $sign . $value . ' ';
}
return trim($query);
}
/**
* Returns set placeholders.
*
* @return list<mixed>
*/
protected function wherePlaceholders(): array
{
$placeholders = [];
foreach ($this->where as [
'value' => $value,
]) {
if ($value instanceof ScalarFunction) {
$placeholders = array_merge($placeholders, $value->buildParams());
} elseif ($value instanceof Condition) {
$placeholders = array_merge($placeholders, $value->buildParams());
} elseif (!\is_bool($value)) {
$placeholders[] = $value;
}
}
return $placeholders;
}
/**
* Adds condition to list.
*
* @param string $type condition type
* @param string $name column name
* @param string $sign condition sign
* @param scalar|ScalarFunction|Condition|null $value condition value
*/
protected function addCondition(string $type, string $name, string $sign, string|int|float|bool|null|ScalarFunction|Condition $value): void
{
$this->where[] = [
'type' => $type,
'name' => $name,
'sign' => $sign,
'value' => $value,
];
}
/**
* Set first condition.
*
* @param string $name column name
* @param scalar|ScalarFunction|Condition|null $value condition value
* @param string $sign condition sign
*
* $value can be subclass of Condition (In, Beetween, etc.)
* If $value is subclass of Condition $sign will be ignored.
*
* @return $this
*/
public function where(string $name, string|int|float|bool|null|ScalarFunction|Condition $value, string $sign = '='): object
{
if (empty($name)) {
return $this;
}
$this->addCondition('WHERE', $name, $sign, $value);
return $this;
}
/**
* Adds 'and' condition.
*
* @param string $name column name
* @param scalar|ScalarFunction|Condition|null $value condition value
* @param string $sign condition sign
*
* $value can be subclass of Condition (In, Beetween, etc.)
* If $value is subclass of Condition $sign will be ignored.
*
* @return $this
*/
public function and(string $name, string|int|float|bool|null|ScalarFunction|Condition $value, string $sign = '='): object
{
if (empty($name)) {
return $this;
}
if (empty($this->where)) {
return $this->where($name, $value, $sign);
}
$this->addCondition('AND', $name, $sign, $value);
return $this;
}
/**
* Adds 'or' condition.
*
* @param string $name column name
* @param scalar|ScalarFunction|Condition|null $value condition value
* @param string $sign condition sign
*
* $value can be subclass of Condition (In, Beetween, etc.)
* If $value is subclass of Condition $sign will be ignored.
*
* @return $this
*/
public function or(string $name, string|int|float|bool|null|ScalarFunction|Condition $value, string $sign = '='): object
{
if (empty($name)) {
return $this;
}
if (empty($this->where)) {
return $this->where($name, $value, $sign);
}
$this->addCondition('OR', $name, $sign, $value);
return $this;
}
}