Passed
Pull Request — master (#3852)
by Benjamin
62:40
created

ExpressionBuilder::and()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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