Failed Conditions
Push — 2.11.x ( c2b8e6...130e15 )
by Sergei
13:12 queued 15s
created

ExpressionBuilder::or()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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