Completed
Push — 4.0 ( 87d096...bcc1be )
by Kiyotaka
05:44 queued 11s
created

src/Eccube/Doctrine/Query/WhereClause.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Doctrine\Query;
15
16
use Doctrine\ORM\QueryBuilder;
17
use Doctrine\ORM\Query\Expr;
18
19
/**
20
 * WHERE句を組み立てるクラス。
21
 */
22
class WhereClause
0 ignored issues
show
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
23
{
24
    private $expr;
25
26
    /**
27
     * @var array
28
     */
29
    private $params;
30
31
    /**
32
     * WhereClause constructor.
33
     *
34
     * @param $expr
35
     * @param array $params
36
     */
37 24
    private function __construct($expr, $params = null)
38
    {
39 24
        $this->expr = $expr;
40 24
        $this->params = $params;
41
    }
42
43
    /**
44
     * @param Expr\Comparison $expr
45
     */
46 16
    private static function newWhereClause($expr, $x, $y)
47
    {
48 16
        if ($y) {
49 16
            if (is_array($y)) {
50 5
                return new WhereClause($expr, $y);
51
            } else {
52 11
                return new WhereClause($expr, [$x => $y]);
53
            }
54
        }
55
56 1
        return new WhereClause($expr);
57
    }
58
59
    /**
60
     * =条件式のファクトリメソッド。
61
     *
62
     * Example:
63
     *      WhereClause::eq('name', ':Name', 'hoge')
64
     *      WhereClause::eq('name', ':Name', ['Name' => 'hoge'])
65
     *
66
     * @param $x
67
     * @param $y
68
     * @param $param
69
     *
70
     * @return WhereClause
71
     */
72 5
    public static function eq($x, $y, $param)
73
    {
74 5
        return self::newWhereClause(self::expr()->eq($x, $y), $y, $param);
75
    }
76
77
    /**
78
     * <>条件式のファクトリメソッド。
79
     *
80
     * Example:
81
     *      WhereClause::neq('name', ':Name', 'hoge')
82
     *      WhereClause::neq('name', ':Name', ['Name' => 'hoge'])
83
     *
84
     * @param $x
85
     * @param $y
86
     * @param $param
87
     *
88
     * @return WhereClause
89
     */
90 1
    public static function neq($x, $y, $param)
91
    {
92 1
        return self::newWhereClause(self::expr()->neq($x, $y), $y, $param);
93
    }
94
95
    /**
96
     * IS NULL条件式のファクトリメソッド。
97
     *
98
     * Example:
99
     *      WhereClause::isNull('name')
100
     *
101
     * @param $x
102
     *
103
     * @return WhereClause
104
     */
105 1
    public static function isNull($x)
106
    {
107 1
        return new WhereClause(self::expr()->isNull($x));
108
    }
109
110
    /**
111
     * IS NOT NULL条件式のファクトリメソッド。
112
     *
113
     * Example:
114
     *      WhereClause::isNotNull('name')
115
     *
116
     * @param $x
117
     *
118
     * @return WhereClause
119
     */
120 1
    public static function isNotNull($x)
121
    {
122 1
        return new WhereClause(self::expr()->isNotNull($x));
123
    }
124
125
    /**
126
     * LIKE演算子のファクトリメソッド。
127
     *
128
     * Example:
129
     *      WhereClause::like('name', ':Name', '%hoge')
130
     *      WhereClause::like('name', ':Name', ['Name' => '%hoge'])
131
     *
132
     * @param $x
133
     * @param $y
134
     * @param $param
135
     *
136
     * @return WhereClause
137
     */
138 1
    public static function like($x, $y, $param)
139
    {
140 1
        return self::newWhereClause(self::expr()->like($x, $y), $y, $param);
141
    }
142
143
    /**
144
     * NOT LIKE演算子のファクトリメソッド。
145
     *
146
     * Example:
147
     *      WhereClause::notLike('name', ':Name', '%hoge')
148
     *      WhereClause::notLike('name', ':Name', ['Name' => '%hoge'])
149
     *
150
     * @param $x
151
     * @param $y
152
     * @param $param
153
     *
154
     * @return WhereClause
155
     */
156 1
    public static function notLike($x, $y, $param)
157
    {
158 1
        return self::newWhereClause(self::expr()->notLike($x, $y), $y, $param);
159
    }
160
161
    /**
162
     * IN演算子のファクトリメソッド。
163
     *
164
     * Example:
165
     *      WhereClause::in('name', ':Names', ['foo', 'bar'])
166
     *      WhereClause::in('name', ':Names', ['Names' => ['foo', 'bar']])
167
     *
168
     * @param $x
169
     * @param $y
170
     * @param $param
171
     *
172
     * @return WhereClause
173
     */
174 2
    public static function in($x, $y, $param)
175
    {
176 2
        return new WhereClause(self::expr()->in($x, $y), self::isMap($param) ? $param : [$y => $param]);
177
    }
178
179 6
    private static function isMap($arrayOrMap)
180
    {
181 6
        return array_values($arrayOrMap) !== $arrayOrMap;
182
    }
183
184
    /**
185
     * NOT IN演算子のファクトリメソッド。
186
     *
187
     * Example:
188
     *      WhereClause::notIn('name', ':Names', ['foo', 'bar'])
189
     *      WhereClause::notIn('name', ':Names', ['Names' => ['foo', 'bar']])
190
     *
191
     * @param $x
192
     * @param $y
193
     * @param $param
194
     *
195
     * @return WhereClause
196
     */
197 2
    public static function notIn($x, $y, $param)
198
    {
199 2
        return new WhereClause(self::expr()->notIn($x, $y), self::isMap($param) ? $param : [$y => $param]);
200
    }
201
202
    /**
203
     * BETWEEN演算子のファクトリメソッド。
204
     *
205
     * Example:
206
     *      WhereClause::between('price', ':PriceMin', ':PriceMax', [1000, 2000])
207
     *      WhereClause::between('price', ':PriceMin', ':PriceMax', ['PriceMin' => 1000, 'PriceMax' => 2000])
208
     *
209
     * @param $var
210
     * @param $x
211
     * @param $y
212
     * @param $params
213
     *
214
     * @return WhereClause
215
     */
216 2
    public static function between($var, $x, $y, $params)
217
    {
218 2
        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
     *
232
     * @return WhereClause
233
     */
234 2
    public static function gt($x, $y, $param)
235
    {
236 2
        return self::newWhereClause(self::expr()->gt($x, $y), $y, $param);
237
    }
238
239
    /**
240
     * >=演算子のファクトリメソッド。
241
     *
242
     * Example:
243
     *      WhereClause::gte('price', ':Price', 1000)
244
     *      WhereClause::gte('price', ':Price', ['Price' => 1000])
245
     *
246
     * @param $x
247
     * @param $y
248
     * @param $param
249
     *
250
     * @return WhereClause
251
     */
252 2
    public static function gte($x, $y, $param)
253
    {
254 2
        return self::newWhereClause(self::expr()->gte($x, $y), $y, $param);
255
    }
256
257
    /**
258
     * <演算子のファクトリメソッド。
259
     *
260
     * Example:
261
     *      WhereClause::lt('price', ':Price', 1000)
262
     *      WhereClause::lt('price', ':Price', ['Price' => 1000])
263
     *
264
     * @param $x
265
     * @param $y
266
     * @param $param
267
     *
268
     * @return WhereClause
269
     */
270 2
    public static function lt($x, $y, $param)
271
    {
272 2
        return self::newWhereClause(self::expr()->lt($x, $y), $y, $param);
273
    }
274
275
    /**
276
     * <=演算子のファクトリメソッド。
277
     *
278
     * Example:
279
     *      WhereClause::lte('price', ':Price', 1000)
280
     *      WhereClause::lte('price', ':Price', ['Price' => 1000])
281
     *
282
     * @param $x
283
     * @param $y
284
     * @param $param
285
     *
286
     * @return WhereClause
287
     */
288 2
    public static function lte($x, $y, $param)
289
    {
290 2
        return self::newWhereClause(self::expr()->lte($x, $y), $y, $param);
291
    }
292
293
    /**
294
     * @return Expr
295
     */
296 24
    private static function expr()
297
    {
298 24
        return new Expr();
299
    }
300
301 24
    public function build(QueryBuilder $builder)
302
    {
303 24
        $builder->andWhere($this->expr);
304 24
        if ($this->params) {
305 22
            foreach ($this->params as $key => $param) {
306 22
                $builder->setParameter($key, $param);
307
            }
308
        }
309
    }
310
}
311