Completed
Pull Request — develop (#3576)
by Jonathan
64:38 queued 61:53
created

ExpressionBuilder::in()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Query\Expression;
6
7
use Doctrine\DBAL\Connection;
8
use function func_get_arg;
9
use function func_get_args;
10
use function func_num_args;
11
use function implode;
12
use function sprintf;
13
14
/**
15
 * ExpressionBuilder class is responsible to dynamically create SQL query parts.
16
 */
17
class ExpressionBuilder
18
{
19
    public const EQ  = '=';
20
    public const NEQ = '<>';
21
    public const LT  = '<';
22
    public const LTE = '<=';
23
    public const GT  = '>';
24
    public const GTE = '>=';
25
26
    /**
27
     * The DBAL Connection.
28
     *
29
     * @var Connection
30
     */
31
    private $connection;
32
33
    /**
34
     * Initializes a new <tt>ExpressionBuilder</tt>.
35
     *
36
     * @param Connection $connection The DBAL Connection.
37
     */
38 4907
    public function __construct(Connection $connection)
39
    {
40 4907
        $this->connection = $connection;
41 4907
    }
42
43
    /**
44
     * Creates a conjunction of the given boolean expressions.
45
     *
46
     * Example:
47
     *
48
     *     [php]
49
     *     // (u.type = ?) AND (u.role = ?)
50
     *     $expr->andX('u.type = ?', 'u.role = ?'));
51
     *
52
     * @param mixed $x Optional clause. Defaults = null, but requires
53
     *                 at least one defined when converting to string.
54
     *
55
     * @return CompositeExpression
56
     */
57 2529
    public function andX($x = null)
58
    {
59 2529
        return new CompositeExpression(CompositeExpression::TYPE_AND, func_get_args());
60
    }
61
62
    /**
63
     * Creates a disjunction of the given boolean expressions.
64
     *
65
     * Example:
66
     *
67
     *     [php]
68
     *     // (u.type = ?) OR (u.role = ?)
69
     *     $qb->where($qb->expr()->orX('u.type = ?', 'u.role = ?'));
70
     *
71
     * @param mixed $x Optional clause. Defaults = null, but requires
72
     *                 at least one defined when converting to string.
73
     *
74
     * @return CompositeExpression
75
     */
76 2398
    public function orX($x = null)
77
    {
78 2398
        return new CompositeExpression(CompositeExpression::TYPE_OR, func_get_args());
79
    }
80
81
    /**
82
     * Creates a comparison expression.
83
     *
84
     * @param mixed  $x        The left expression.
85
     * @param string $operator One of the ExpressionBuilder::* constants.
86
     * @param mixed  $y        The right expression.
87
     *
88
     * @return string
89
     */
90 2290
    public function comparison($x, $operator, $y)
91
    {
92 2290
        return $x . ' ' . $operator . ' ' . $y;
93
    }
94
95
    /**
96
     * Creates an equality comparison expression with the given arguments.
97
     *
98
     * First argument is considered the left expression and the second is the right expression.
99
     * When converted to string, it will generated a <left expr> = <right expr>. Example:
100
     *
101
     *     [php]
102
     *     // u.id = ?
103
     *     $expr->eq('u.id', '?');
104
     *
105
     * @param mixed $x The left expression.
106
     * @param mixed $y The right expression.
107
     *
108
     * @return string
109
     */
110 2245
    public function eq($x, $y)
111
    {
112 2245
        return $this->comparison($x, self::EQ, $y);
113
    }
114
115
    /**
116
     * Creates a non equality comparison expression with the given arguments.
117
     * First argument is considered the left expression and the second is the right expression.
118
     * When converted to string, it will generated a <left expr> <> <right expr>. Example:
119
     *
120
     *     [php]
121
     *     // u.id <> 1
122
     *     $q->where($q->expr()->neq('u.id', '1'));
123
     *
124
     * @param mixed $x The left expression.
125
     * @param mixed $y The right expression.
126
     *
127
     * @return string
128
     */
129 1
    public function neq($x, $y)
130
    {
131 1
        return $this->comparison($x, self::NEQ, $y);
132
    }
133
134
    /**
135
     * Creates a lower-than comparison expression with the given arguments.
136
     * First argument is considered the left expression and the second is the right expression.
137
     * When converted to string, it will generated a <left expr> < <right expr>. Example:
138
     *
139
     *     [php]
140
     *     // u.id < ?
141
     *     $q->where($q->expr()->lt('u.id', '?'));
142
     *
143
     * @param mixed $x The left expression.
144
     * @param mixed $y The right expression.
145
     *
146
     * @return string
147
     */
148 1
    public function lt($x, $y)
149
    {
150 1
        return $this->comparison($x, self::LT, $y);
151
    }
152
153
    /**
154
     * Creates a lower-than-equal comparison expression with the given arguments.
155
     * First argument is considered the left expression and the second is the right expression.
156
     * When converted to string, it will generated a <left expr> <= <right expr>. Example:
157
     *
158
     *     [php]
159
     *     // u.id <= ?
160
     *     $q->where($q->expr()->lte('u.id', '?'));
161
     *
162
     * @param mixed $x The left expression.
163
     * @param mixed $y The right expression.
164
     *
165
     * @return string
166
     */
167 1
    public function lte($x, $y)
168
    {
169 1
        return $this->comparison($x, self::LTE, $y);
170
    }
171
172
    /**
173
     * Creates a greater-than comparison expression with the given arguments.
174
     * First argument is considered the left expression and the second is the right expression.
175
     * When converted to string, it will generated a <left expr> > <right expr>. Example:
176
     *
177
     *     [php]
178
     *     // u.id > ?
179
     *     $q->where($q->expr()->gt('u.id', '?'));
180
     *
181
     * @param mixed $x The left expression.
182
     * @param mixed $y The right expression.
183
     *
184
     * @return string
185
     */
186 1
    public function gt($x, $y)
187
    {
188 1
        return $this->comparison($x, self::GT, $y);
189
    }
190
191
    /**
192
     * Creates a greater-than-equal comparison expression with the given arguments.
193
     * First argument is considered the left expression and the second is the right expression.
194
     * When converted to string, it will generated a <left expr> >= <right expr>. Example:
195
     *
196
     *     [php]
197
     *     // u.id >= ?
198
     *     $q->where($q->expr()->gte('u.id', '?'));
199
     *
200
     * @param mixed $x The left expression.
201
     * @param mixed $y The right expression.
202
     *
203
     * @return string
204
     */
205 1
    public function gte($x, $y)
206
    {
207 1
        return $this->comparison($x, self::GTE, $y);
208
    }
209
210
    /**
211
     * Creates an IS NULL expression with the given arguments.
212
     *
213
     * @param string $x The field in string format to be restricted by IS NULL.
214
     *
215
     * @return string
216
     */
217 2211
    public function isNull($x)
218
    {
219 2211
        return $x . ' IS NULL';
220
    }
221
222
    /**
223
     * Creates an IS NOT NULL expression with the given arguments.
224
     *
225
     * @param string $x The field in string format to be restricted by IS NOT NULL.
226
     *
227
     * @return string
228
     */
229 1
    public function isNotNull($x)
230
    {
231 1
        return $x . ' IS NOT NULL';
232
    }
233
234
    /**
235
     * Creates a LIKE() comparison expression with the given arguments.
236
     *
237
     * @param string $x Field in string format to be inspected by LIKE() comparison.
238
     * @param mixed  $y Argument to be used in LIKE() comparison.
239
     *
240
     * @return string
241
     */
242 2160
    public function like($x, $y/*, ?string $escapeChar = null */)
243
    {
244 2160
        return $this->comparison($x, 'LIKE', $y) .
245 2160
            (func_num_args() >= 3 ? sprintf(' ESCAPE %s', func_get_arg(2)) : '');
246
    }
247
248
    /**
249
     * Creates a NOT LIKE() comparison expression with the given arguments.
250
     *
251
     * @param string $x Field in string format to be inspected by NOT LIKE() comparison.
252
     * @param mixed  $y Argument to be used in NOT LIKE() comparison.
253
     *
254
     * @return string
255
     */
256 2108
    public function notLike($x, $y/*, ?string $escapeChar = null */)
257
    {
258 2108
        return $this->comparison($x, 'NOT LIKE', $y) .
259 2108
            (func_num_args() >= 3 ? sprintf(' ESCAPE %s', func_get_arg(2)) : '');
260
    }
261
262
    /**
263
     * Creates a IN () comparison expression with the given arguments.
264
     *
265
     * @param string          $x The field in string format to be inspected by IN() comparison.
266
     * @param string|string[] $y The placeholder or the array of values to be used by IN() comparison.
267
     *
268
     * @return string
269
     */
270 2186
    public function in($x, $y)
271
    {
272 2186
        return $this->comparison($x, 'IN', '(' . implode(', ', (array) $y) . ')');
273
    }
274
275
    /**
276
     * Creates a NOT IN () comparison expression with the given arguments.
277
     *
278
     * @param string          $x The field in string format to be inspected by NOT IN() comparison.
279
     * @param string|string[] $y The placeholder or the array of values to be used by NOT IN() comparison.
280
     *
281
     * @return string
282
     */
283 2
    public function notIn($x, $y)
284
    {
285 2
        return $this->comparison($x, 'NOT IN', '(' . implode(', ', (array) $y) . ')');
286
    }
287
288
    /**
289
     * Creates an SQL literal expression from the string.
290
     */
291
    public function literal(string $input)
292
    {
293
        return $this->connection->quote($input);
294
    }
295
}
296