forked from chrmorandi/yii2-ldap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FilterBuilder.php
329 lines (293 loc) · 11.5 KB
/
FilterBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
/**
* @link https://github.com/chrmorandi/yii2-ldap for the canonical source repository
* @package yii2-ldap
* @author Christopher Mota <[email protected]>
* @license MIT License - view the LICENSE file that was distributed with this source code.
*/
namespace chrmorandi\ldap;
use Traversable;
use yii\base\InvalidParamException;
use yii\base\Object;
use yii\helpers\ArrayHelper;
/**
* FilterBuilder builds a Filter for search in LDAP.
*
* FilterBuilder is also used by [[Query]] to build Filters.
*
* @author Christopher Mota <[email protected]>
* @since 1.0.0
*/
class FilterBuilder extends Object
{
/**
* @var string the separator between different fragments of a SQL statement.
* Defaults to an empty space. This is mainly used by [[build()]] when generating a SQL statement.
*/
public $separator = ' ';
/**
* @var array map of query condition to builder methods.
* These methods are used by [[buildCondition]] to build SQL conditions from array syntax.
*/
protected $conditionBuilders = [
'NOT' => 'buildNotCondition',
'AND' => 'buildAndCondition',
'OR' => 'buildAndCondition',
'IN' => 'buildInCondition',
'NOT IN' => 'buildInCondition',
'LIKE' => 'buildLikeCondition',
'NOT LIKE' => 'buildLikeCondition',
'OR LIKE' => 'buildLikeCondition',
'OR NOT LIKE' => 'buildLikeCondition',
];
/**
* @var array map of operator for builder methods.
*/
protected $operator = [
'NOT' => '!',
'AND' => '&',
'OR' => '|',
'LIKE' => '~=',
];
/**
* Parses the condition specification and generates the corresponding filters.
* @param string|array $condition the condition specification. Please refer to [[Query::where()]]
* on how to specify a condition.
* @return string the generated
*/
public function build($condition)
{
if (!is_array($condition)) {
return (string) $condition;
} elseif (empty($condition)) {
return '';
}
if (isset($condition[0])) { // operator format: operator, operand 1, operand 2, ...
$operator = strtoupper($condition[0]);
if (isset($this->conditionBuilders[$operator])) {
$method = $this->conditionBuilders[$operator];
} else {
$method = 'buildSimpleCondition';
}
array_shift($condition);
return $this->$method($operator, $condition);
} else { // hash format: 'column1' => 'value1', 'column2' => 'value2', ...
return $this->buildHashCondition($condition);
}
}
/**
* Creates a condition based on column-value pairs.
* @param array $condition the condition specification.
* @return string the generated
*/
public function buildHashCondition($condition)
{
$parts = [];
foreach ($condition as $column => $value) {
if (ArrayHelper::isTraversable($value) || $value instanceof Query) {
// IN condition
$parts[] = $this->buildInCondition('IN', [$column, $value]);
} else {
if ($value === null) {
$parts[] = "$column IS NULL";
} else if ($column === 'dn') {
$parts[] = LdapUtils::getRdnFromDn($value);
} else {
$parts[] = "$column=$value";
}
}
}
return count($parts) === 1 ? '('.$parts[0].')' : '$('.implode(') (', $parts).')';
}
/**
* Connects two or more Filters expressions with the `AND`(&) or `OR`(|) operator.
* @param string $operator the operator to use for connecting the given operands
* @param array $operands the Filter expressions to connect.
* @return string the generated
*/
public function buildAndCondition($operator, $operands)
{
$parts = [];
$other = [];
foreach ($operands as $key => $operand) {
if (is_array($operand)) {
$operand = $this->build($operand);
}
if ($operand !== '' && !is_numeric($key)) {
$parts[] = $key.'='.$operand;
} elseif ($operand !== '') {
$other[] = $operand;
}
}
if (!empty($parts)) {
return '('.$this->operator[$operator].'('.implode(") (", $parts).')'.implode($other).')';
} else if (!empty($other)) {
return '('.$this->operator[$operator].implode($other).')';
}else {
return '';
}
}
/**
* Returns a query string for does not equal.
* Produces: (!(field=value))
* @param string $operator the operator to use for connecting the given operands
* @param array $operands the filter expressions to connect.
* @return string the generated filter expression
* @throws InvalidParamException if wrong number of operands have been given.
*/
public function buildNotCondition($operator, $operands)
{
if (count($operands) !== 1) {
throw new InvalidParamException("Operator '$operator' requires exactly one operand.");
}
$operand = reset($operands);
if (is_array($operand)) {
$operand = $this->build($operand);
}
if ($operand === '') {
return '';
}
return '('.$this->operator['NOT'].'('.key($operands).'='.$operand.'))';
}
/**
* Creates an filter expressions with the `IN` operator.
* @param string $operator the operator to use (e.g. `IN` or `NOT IN`)
* @param array $operands the first operand is the column name. If it is an array
* a composite IN condition will be generated.
* The second operand is an array of values that column value should be among.
* If it is an empty array the generated expression will be a `false` value if
* operator is `IN` and empty if operator is `NOT IN`.
* @return string the generated SQL expression
* @throws Exception if wrong number of operands have been given.
*/
public function buildInCondition($operator, $operands)
{
if (!isset($operands[0], $operands[1])) {
throw new Exception("Operator '$operator' requires two operands.");
}
list($column, $values) = $operands;
if ($column === []) {
return $operator === 'IN' ? '0=1' : '';
}
if (!is_array($values) && !$values instanceof \Traversable) {
// ensure values is an array
$values = (array) $values;
}
if ($column instanceof \Traversable || count($column) > 1) {
return $this->buildCompositeInCondition($operator, $column, $values);
} elseif (is_array($column)) {
$column = reset($column);
}
$sqlValues = [];
foreach ($values as $i => $value) {
if (is_array($value) || $value instanceof \ArrayAccess) {
$value = isset($value[$column]) ? $value[$column] : null;
}
if ($value === null) {
$sqlValues[$i] = 'NULL';
} else {
$sqlValues[$i] = $value;
}
}
if (empty($sqlValues)) {
return $operator === 'IN' ? '0=1' : '';
}
if (count($sqlValues) > 1) {
return "&($column=".implode(")($column=", $sqlValues).')';
} else {
$operator = $operator === 'IN' ? '=' : '<>';
return $column.$operator.reset($sqlValues);
}
}
/**
* Builds SQL for IN condition
*
* @param string $operator
* @param array|Traversable $columns
* @param array|Traversable $values
* @return string SQL
*/
protected function buildCompositeInCondition($operator, $columns, $values)
{
$vss = [];
foreach ($values as $value) {
$vs = [];
foreach ($columns as $column) {
if (isset($value[$column])) {
$vs[] = "($column=$value[$column])";
}
}
$vss[] = implode('', $vs);
}
if (empty($vss)) {
return $operator === 'IN' ? '0=1' : '';
}
return '(&'.implode('', $vss).')';
}
/**
* Creates an SQL expressions with the `LIKE` operator.
* @param string $operator the operator to use (e.g. `LIKE`, `NOT LIKE`, `OR LIKE` or `OR NOT LIKE`)
* @param array $operands an array of two or three operands
*
* - The first operand is the column name.
* - The second operand is a single value or an array of values that column value
* should be compared with. If it is an empty array the generated expression will
* be a `false` value if operator is `LIKE` or `OR LIKE`, and empty if operator
* is `NOT LIKE` or `OR NOT LIKE`.
* - An optional third operand can also be provided to specify how to escape special characters
* in the value(s). The operand should be an array of mappings from the special characters to their
* escaped counterparts. If this operand is not provided, a default escape mapping will be used.
* You may use `false` or an empty array to indicate the values are already escaped and no escape
* should be applied. Note that when using an escape mapping (or the third operand is not provided),
* the values will be automatically enclosed within a pair of percentage characters.
* @return string the generated SQL expression
* @throws InvalidParamException if wrong number of operands have been given.
*/
public function buildLikeCondition($operator, $operands)
{
if (!isset($operands[0], $operands[1])) {
throw new InvalidParamException("Operator '$operator' requires two operands.");
}
$escape = isset($operands[2]) ? $operands[2] : ['*' => '\*', '_' => '\_', '\\' => '\\\\'];
unset($operands[2]);
if (!preg_match('/^(AND |OR |)(((NOT |))I?LIKE)/', $operator, $matches)) {
throw new InvalidParamException("Invalid operator '$operator'.");
}
$andor = (!empty($matches[1]) ? $matches[1] : 'AND ');
$not = !empty($matches[3]);
$operator = $matches[2];
list($column, $values) = $operands;
if (!is_array($values)) {
$values = [$values];
}
if (empty($values)) {
return $not ? '' : '0=1';
}
$not = ($operator == 'NOT LIKE') ? '('.$this->operator['NOT'] : false;
$parts = [];
foreach ($values as $value) {
$value = empty($escape) ? $value : strtr($value, $escape);
$parts[] = $not.'('.$column.'=*'.$value.'*)'.($not ? ')' : '');
}
return '('.$this->operator[trim($andor)].implode($parts).')';
}
/**
* Creates an SQL expressions like `"column" operator value`.
* @param string $operator the operator to use. Anything could be used e.g. `>`, `<=`, etc.
* @param array $operands contains two column names.
* @return string the generated SQL expression
* @throws InvalidParamException if wrong number of operands have been given.
*/
public function buildSimpleCondition($operator, $operands)
{
if (count($operands) !== 2) {
throw new InvalidParamException("Operator '$operator' requires two operands.");
}
list($column, $value) = $operands;
if ($value === null) {
return "$column $operator NULL";
} else {
return "($column $operator $value)";
}
}
}