Completed
Push — master ( 77087e...43819a )
by Sergei
22s queued 15s
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
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Query\Expression;
6
7
use Doctrine\DBAL\Connection;
8
use function array_merge;
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 5571
     */
36
    public function __construct(Connection $connection)
37 5571
    {
38 5571
        $this->connection = $connection;
39
    }
40
41
    /**
42
     * Creates a conjunction of the given expressions.
43
     *
44
     * @param string|CompositeExpression $expression
45
     * @param string|CompositeExpression ...$expressions
46
     */
47
    public function and($expression, ...$expressions) : CompositeExpression
48
    {
49
        return new CompositeExpression(CompositeExpression::TYPE_AND, array_merge([$expression], $expressions));
50
    }
51 189
52
    /**
53 189
     * Creates a disjunction of the given expressions.
54
     *
55
     * @param string|CompositeExpression $expression
56
     * @param string|CompositeExpression ...$expressions
57
     */
58
    public function or($expression, ...$expressions) : CompositeExpression
59
    {
60
        return new CompositeExpression(CompositeExpression::TYPE_OR, array_merge([$expression], $expressions));
61
    }
62
63
    /**
64
     * @deprecated Use `and()` instead.
65
     *
66
     * @param string|CompositeExpression ...$expressions Requires at least one defined when converting to string.
67 162
     */
68
    public function andX(...$expressions) : CompositeExpression
69 162
    {
70
        return new CompositeExpression(CompositeExpression::TYPE_AND, $expressions);
71
    }
72
73
    /**
74
     * @deprecated Use `or()` instead.
75
     *
76
     * @param string|CompositeExpression ...$expressions Requires at least one defined when converting to string.
77
     */
78
    public function orX(...$expressions) : CompositeExpression
79 756
    {
80
        return new CompositeExpression(CompositeExpression::TYPE_OR, $expressions);
81 756
    }
82
83
    /**
84
     * Creates a comparison expression.
85
     *
86
     * @param string $x        The left expression.
87
     * @param string $operator The comparison operator.
88
     * @param string $y        The right expression.
89
     */
90
    public function comparison(string $x, string $operator, string $y) : string
91
    {
92
        return $x . ' ' . $operator . ' ' . $y;
93
    }
94
95
    /**
96
     * Creates an equality comparison expression with the given arguments.
97 243
     *
98
     * First argument is considered the left expression and the second is the right expression.
99 243
     * 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 string $x The left expression.
106
     * @param string $y The right expression.
107
     */
108
    public function eq(string $x, string $y) : string
109
    {
110
        return $this->comparison($x, self::EQ, $y);
111
    }
112
113
    /**
114 27
     * Creates a non equality comparison expression with the given arguments.
115
     * First argument is considered the left expression and the second is the right expression.
116 27
     * When converted to string, it will generated a <left expr> <> <right expr>. Example:
117
     *
118
     *     [php]
119
     *     // u.id <> 1
120
     *     $q->where($q->expr()->neq('u.id', '1'));
121
     *
122
     * @param string $x The left expression.
123
     * @param string $y The right expression.
124
     */
125
    public function neq(string $x, string $y) : string
126
    {
127
        return $this->comparison($x, self::NEQ, $y);
128
    }
129
130
    /**
131 27
     * Creates a lower-than comparison expression with the given arguments.
132
     * First argument is considered the left expression and the second is the right expression.
133 27
     * When converted to string, it will generated a <left expr> < <right expr>. Example:
134
     *
135
     *     [php]
136
     *     // u.id < ?
137
     *     $q->where($q->expr()->lt('u.id', '?'));
138
     *
139
     * @param string $x The left expression.
140
     * @param string $y The right expression.
141
     */
142
    public function lt(string $x, string $y) : string
143
    {
144
        return $this->comparison($x, self::LT, $y);
145
    }
146
147
    /**
148 27
     * Creates a lower-than-equal comparison expression with the given arguments.
149
     * First argument is considered the left expression and the second is the right expression.
150 27
     * When converted to string, it will generated a <left expr> <= <right expr>. Example:
151
     *
152
     *     [php]
153
     *     // u.id <= ?
154
     *     $q->where($q->expr()->lte('u.id', '?'));
155
     *
156
     * @param string $x The left expression.
157
     * @param string $y The right expression.
158
     */
159
    public function lte(string $x, string $y) : string
160
    {
161
        return $this->comparison($x, self::LTE, $y);
162
    }
163
164
    /**
165 27
     * Creates a greater-than comparison expression with the given arguments.
166
     * First argument is considered the left expression and the second is the right expression.
167 27
     * When converted to string, it will generated a <left expr> > <right expr>. Example:
168
     *
169
     *     [php]
170
     *     // u.id > ?
171
     *     $q->where($q->expr()->gt('u.id', '?'));
172
     *
173
     * @param string $x The left expression.
174
     * @param string $y The right expression.
175
     */
176
    public function gt(string $x, string $y) : string
177
    {
178
        return $this->comparison($x, self::GT, $y);
179
    }
180
181
    /**
182 27
     * Creates a greater-than-equal 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()->gte('u.id', '?'));
189
     *
190
     * @param string $x The left expression.
191
     * @param string $y The right expression.
192 27
     */
193
    public function gte(string $x, string $y) : string
194 27
    {
195
        return $this->comparison($x, self::GTE, $y);
196
    }
197
198
    /**
199
     * Creates an IS NULL expression with the given arguments.
200
     *
201
     * @param string $x The field in string format to be restricted by IS NULL.
202 27
     */
203
    public function isNull(string $x) : string
204 27
    {
205
        return $x . ' IS NULL';
206
    }
207
208
    /**
209
     * Creates an IS NOT NULL expression with the given arguments.
210
     *
211
     * @param string $x The field in string format to be restricted by IS NOT NULL.
212
     */
213 54
    public function isNotNull(string $x) : string
214
    {
215 54
        return $x . ' IS NOT NULL';
216 54
    }
217
218
    /**
219
     * Creates a LIKE comparison expression.
220
     *
221
     * @param string $expression The expression to be inspected by the LIKE comparison
222
     * @param string $pattern    The pattern to compare against
223
     */
224
    public function like(string $expression, string $pattern, ?string $escapeChar = null) : string
225 54
    {
226
        return $this->comparison($expression, 'LIKE', $pattern) .
227 54
            ($escapeChar !== null ? sprintf(' ESCAPE %s', $escapeChar) : '');
228 54
    }
229
230
    /**
231
     * Creates a NOT LIKE comparison expression
232
     *
233
     * @param string $expression The expression to be inspected by the NOT LIKE comparison
234
     * @param string $pattern    The pattern to compare against
235
     */
236
    public function notLike(string $expression, string $pattern, ?string $escapeChar = null) : string
237 54
    {
238
        return $this->comparison($expression, 'NOT LIKE', $pattern) .
239 54
            ($escapeChar !== null ? sprintf(' ESCAPE %s', $escapeChar) : '');
240
    }
241
242
    /**
243
     * Creates a IN () comparison expression with the given arguments.
244
     *
245
     * @param string          $x The field in string format to be inspected by IN() comparison.
246
     * @param string|string[] $y The placeholder or the array of values to be used by IN() comparison.
247
     */
248 54
    public function in(string $x, $y) : string
249
    {
250 54
        return $this->comparison($x, 'IN', '(' . implode(', ', (array) $y) . ')');
251
    }
252
253
    /**
254
     * Creates a NOT IN () comparison expression with the given arguments.
255
     *
256
     * @param string          $x The field in string format to be inspected by NOT IN() comparison.
257
     * @param string|string[] $y The placeholder or the array of values to be used by NOT IN() comparison.
258
     */
259
    public function notIn(string $x, $y) : string
260
    {
261
        return $this->comparison($x, 'NOT IN', '(' . implode(', ', (array) $y) . ')');
262
    }
263
264
    /**
265
     * Creates an SQL literal expression from the string.
266
     */
267
    public function literal(string $input) : string
268
    {
269
        return $this->connection->quote($input);
270
    }
271
}
272