1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of EC-CUBE |
4
|
|
|
* |
5
|
|
|
* Copyright(c) 2000-2017 LOCKON CO.,LTD. All Rights Reserved. |
6
|
|
|
* |
7
|
|
|
* http://www.lockon.co.jp/ |
8
|
|
|
* |
9
|
|
|
* This program is free software; you can redistribute it and/or |
10
|
|
|
* modify it under the terms of the GNU General Public License |
11
|
|
|
* as published by the Free Software Foundation; either version 2 |
12
|
|
|
* of the License, or (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU General Public License |
20
|
|
|
* along with this program; if not, write to the Free Software |
21
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace Eccube\Doctrine\Query; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
use Doctrine\ORM\Query\Expr; |
28
|
|
|
use Doctrine\ORM\QueryBuilder; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* WHERE句を組み立てるクラス。 |
32
|
|
|
* |
33
|
|
|
* @package Eccube\Doctrine\Query |
34
|
|
|
*/ |
35
|
|
|
class WhereClause |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
private $expr; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private $params; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* WhereClause constructor. |
47
|
|
|
* @param $expr |
48
|
|
|
* @param array $params |
49
|
|
|
*/ |
50
|
|
|
private function __construct($expr, $params = null) |
51
|
|
|
{ |
52
|
|
|
$this->expr = $expr; |
53
|
|
|
$this->params = $params; |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private static function newWhereClause($expr, $x, $y) |
57
|
|
|
{ |
58
|
|
|
if ($y) { |
59
|
|
|
if (is_array($y)) { |
60
|
|
|
return new WhereClause($expr, $y); |
61
|
|
|
} else { |
62
|
|
|
return new WhereClause($expr, [$x => $y]); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
return new WhereClause($expr); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
|
|
|
|
69
|
|
|
* =条件式のファクトリメソッド。 |
70
|
|
|
* |
71
|
|
|
* Example: |
72
|
|
|
* WhereClause::eq('name', ':Name', 'hoge') |
73
|
|
|
* WhereClause::eq('name', ':Name', ['Name' => 'hoge']) |
74
|
|
|
* |
75
|
|
|
* @param $x |
|
|
|
|
76
|
|
|
* @param $y |
|
|
|
|
77
|
|
|
* @param $param |
|
|
|
|
78
|
|
|
* @return WhereClause |
79
|
|
|
*/ |
80
|
|
|
public static function eq($x, $y, $param) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
return self::newWhereClause(self::expr()->eq($x, $y), $y, $param); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
|
|
|
|
86
|
|
|
* <>条件式のファクトリメソッド。 |
87
|
|
|
* |
88
|
|
|
* Example: |
89
|
|
|
* WhereClause::neq('name', ':Name', 'hoge') |
90
|
|
|
* WhereClause::neq('name', ':Name', ['Name' => 'hoge']) |
91
|
|
|
* |
92
|
|
|
* @param $x |
|
|
|
|
93
|
|
|
* @param $y |
|
|
|
|
94
|
|
|
* @param $param |
|
|
|
|
95
|
|
|
* @return WhereClause |
96
|
|
|
*/ |
97
|
|
|
public static function neq($x, $y, $param) |
98
|
|
|
{ |
99
|
|
|
return self::newWhereClause(self::expr()->neq($x, $y), $y, $param); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
|
|
|
|
103
|
|
|
* IS NULL条件式のファクトリメソッド。 |
104
|
|
|
* |
105
|
|
|
* Example: |
106
|
|
|
* WhereClause::isNull('name') |
107
|
|
|
* |
108
|
|
|
* @param $x |
|
|
|
|
109
|
|
|
* @return WhereClause |
110
|
|
|
*/ |
111
|
|
|
public static function isNull($x) |
112
|
|
|
{ |
113
|
|
|
return new WhereClause(self::expr()->isNull($x)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
|
|
|
|
117
|
|
|
* IS NOT NULL条件式のファクトリメソッド。 |
118
|
|
|
* |
119
|
|
|
* Example: |
120
|
|
|
* WhereClause::isNotNull('name') |
121
|
|
|
* |
122
|
|
|
* @param $x |
|
|
|
|
123
|
|
|
* @return WhereClause |
124
|
|
|
*/ |
125
|
|
|
public static function isNotNull($x) |
126
|
|
|
{ |
127
|
|
|
return new WhereClause(self::expr()->isNotNull($x)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
|
|
|
|
131
|
|
|
* LIKE演算子のファクトリメソッド。 |
132
|
|
|
* |
133
|
|
|
* Example: |
134
|
|
|
* WhereClause::like('name', ':Name', '%hoge') |
135
|
|
|
* WhereClause::like('name', ':Name', ['Name' => '%hoge']) |
136
|
|
|
* |
137
|
|
|
* @param $x |
|
|
|
|
138
|
|
|
* @param $y |
|
|
|
|
139
|
|
|
* @param $param |
|
|
|
|
140
|
|
|
* @return WhereClause |
141
|
|
|
*/ |
142
|
|
|
public static function like($x, $y, $param) |
143
|
|
|
{ |
144
|
|
|
return self::newWhereClause(self::expr()->like($x, $y), $y, $param); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
|
|
|
|
148
|
|
|
* NOT LIKE演算子のファクトリメソッド。 |
149
|
|
|
* |
150
|
|
|
* Example: |
151
|
|
|
* WhereClause::notLike('name', ':Name', '%hoge') |
152
|
|
|
* WhereClause::notLike('name', ':Name', ['Name' => '%hoge']) |
153
|
|
|
* |
154
|
|
|
* @param $x |
|
|
|
|
155
|
|
|
* @param $y |
|
|
|
|
156
|
|
|
* @param $param |
|
|
|
|
157
|
|
|
* @return WhereClause |
158
|
|
|
*/ |
159
|
|
|
public static function notLike($x, $y, $param) |
160
|
|
|
{ |
161
|
|
|
return self::newWhereClause(self::expr()->notLike($x, $y), $y, $param); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
|
|
|
|
165
|
|
|
* IN演算子のファクトリメソッド。 |
166
|
|
|
* |
167
|
|
|
* Example: |
168
|
|
|
* WhereClause::in('name', ':Names', ['foo', 'bar']) |
169
|
|
|
* WhereClause::in('name', ':Names', ['Names' => ['foo', 'bar']]) |
170
|
|
|
* |
171
|
|
|
* @param $x |
|
|
|
|
172
|
|
|
* @param $y |
|
|
|
|
173
|
|
|
* @param $param |
|
|
|
|
174
|
|
|
* @return WhereClause |
175
|
|
|
*/ |
176
|
|
|
public static function in($x, $y, $param) |
177
|
|
|
{ |
178
|
|
|
return new WhereClause(self::expr()->in($x, $y), self::isMap($param) ? $param : [$y => $param]); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
private static function isMap($arrayOrMap) |
182
|
|
|
{ |
183
|
|
|
return array_values($arrayOrMap) !== $arrayOrMap; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
|
|
|
|
187
|
|
|
* NOT IN演算子のファクトリメソッド。 |
188
|
|
|
* |
189
|
|
|
* Example: |
190
|
|
|
* WhereClause::notIn('name', ':Names', ['foo', 'bar']) |
191
|
|
|
* WhereClause::notIn('name', ':Names', ['Names' => ['foo', 'bar']]) |
192
|
|
|
* |
193
|
|
|
* @param $x |
|
|
|
|
194
|
|
|
* @param $y |
|
|
|
|
195
|
|
|
* @param $param |
|
|
|
|
196
|
|
|
* @return WhereClause |
197
|
|
|
*/ |
198
|
|
|
public static function notIn($x, $y, $param) |
|
|
|
|
199
|
|
|
{ |
200
|
|
|
return new WhereClause(self::expr()->notIn($x, $y), self::isMap($param) ? $param : [$y => $param]); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
|
|
|
|
204
|
|
|
* BETWEEN演算子のファクトリメソッド。 |
205
|
|
|
* |
206
|
|
|
* Example: |
207
|
|
|
* WhereClause::between('price', ':PriceMin', ':PriceMax', [1000, 2000]) |
208
|
|
|
* WhereClause::between('price', ':PriceMin', ':PriceMax', ['PriceMin' => 1000, 'PriceMax' => 2000]) |
209
|
|
|
* |
210
|
|
|
* @param $var |
|
|
|
|
211
|
|
|
* @param $x |
|
|
|
|
212
|
|
|
* @param $y |
|
|
|
|
213
|
|
|
* @param $params |
|
|
|
|
214
|
|
|
* @return WhereClause |
215
|
|
|
*/ |
216
|
|
|
public static function between($var, $x, $y, $params) |
217
|
|
|
{ |
218
|
|
|
return new WhereClause(self::expr()->between($var, $x, $y), self::isMap($params) ? $params : [$x => $params[0], $y => $params[1]]); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
|
|
|
|
222
|
|
|
* >演算子のファクトリメソッド。 |
223
|
|
|
* |
224
|
|
|
* Example: |
225
|
|
|
* WhereClause::gt('price', ':Price', 1000) |
226
|
|
|
* WhereClause::gt('price', ':Price', ['Price' => 1000]) |
227
|
|
|
* |
228
|
|
|
* @param $x |
|
|
|
|
229
|
|
|
* @param $y |
|
|
|
|
230
|
|
|
* @param $param |
|
|
|
|
231
|
|
|
* @return WhereClause |
232
|
|
|
*/ |
233
|
|
|
public static function gt($x, $y, $param) |
234
|
|
|
{ |
235
|
|
|
return self::newWhereClause(self::expr()->gt($x, $y), $y, $param); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
|
|
|
|
239
|
|
|
* >=演算子のファクトリメソッド。 |
240
|
|
|
* |
241
|
|
|
* Example: |
242
|
|
|
* WhereClause::gte('price', ':Price', 1000) |
243
|
|
|
* WhereClause::gte('price', ':Price', ['Price' => 1000]) |
244
|
|
|
* |
245
|
|
|
* @param $x |
|
|
|
|
246
|
|
|
* @param $y |
|
|
|
|
247
|
|
|
* @param $param |
|
|
|
|
248
|
|
|
* @return WhereClause |
249
|
|
|
*/ |
250
|
|
|
public static function gte($x, $y, $param) |
251
|
|
|
{ |
252
|
|
|
return self::newWhereClause(self::expr()->gte($x, $y), $y, $param); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
|
|
|
|
256
|
|
|
* <演算子のファクトリメソッド。 |
257
|
|
|
* |
258
|
|
|
* Example: |
259
|
|
|
* WhereClause::lt('price', ':Price', 1000) |
260
|
|
|
* WhereClause::lt('price', ':Price', ['Price' => 1000]) |
261
|
|
|
* |
262
|
|
|
* @param $x |
|
|
|
|
263
|
|
|
* @param $y |
|
|
|
|
264
|
|
|
* @param $param |
|
|
|
|
265
|
|
|
* @return WhereClause |
266
|
|
|
*/ |
267
|
|
|
public static function lt($x, $y, $param) |
268
|
|
|
{ |
269
|
|
|
return self::newWhereClause(self::expr()->lt($x, $y), $y, $param); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
|
|
|
|
273
|
|
|
* <=演算子のファクトリメソッド。 |
274
|
|
|
* |
275
|
|
|
* Example: |
276
|
|
|
* WhereClause::lte('price', ':Price', 1000) |
277
|
|
|
* WhereClause::lte('price', ':Price', ['Price' => 1000]) |
278
|
|
|
* |
279
|
|
|
* @param $x |
|
|
|
|
280
|
|
|
* @param $y |
|
|
|
|
281
|
|
|
* @param $param |
|
|
|
|
282
|
|
|
* @return WhereClause |
283
|
|
|
*/ |
284
|
|
|
public static function lte($x, $y, $param) |
285
|
|
|
{ |
286
|
|
|
return self::newWhereClause(self::expr()->lte($x, $y), $y, $param); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @return Expr |
291
|
|
|
*/ |
292
|
|
|
private static function expr() |
293
|
|
|
{ |
294
|
|
|
return new Expr(); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
public function build(QueryBuilder $builder) { |
|
|
|
|
298
|
|
|
$builder->andWhere($this->expr); |
299
|
|
|
if ($this->params) { |
|
|
|
|
300
|
|
|
foreach ($this->params as $key=>$param) { |
|
|
|
|
301
|
|
|
$builder->setParameter($key, $param); |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
} |
Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.
To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.
The function can be called with either null or an array for the parameter
$needle
but will only accept an array as$haystack
.