-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathCondition.php
More file actions
226 lines (191 loc) · 6.2 KB
/
Copy pathCondition.php
File metadata and controls
226 lines (191 loc) · 6.2 KB
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
<?php
/**
* Copyright 2018 Google Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 2 as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
namespace Drupal\apigee_edge\Entity\Query;
use Drupal\Core\Entity\Query\ConditionBase;
use Drupal\Core\Entity\Query\ConditionInterface;
use Drupal\Core\Entity\Query\QueryException;
/**
* Defines the condition class for the Apigee Edge entity query.
*/
class Condition extends ConditionBase implements ConditionInterface {
/**
* {@inheritdoc}
*/
public function exists($field, $langcode = NULL) {
return $this->condition($field, NULL, 'IS NOT NULL', $langcode);
}
/**
* {@inheritdoc}
*/
public function notExists($field, $langcode = NULL) {
return $this->condition($field, NULL, 'IS NULL', $langcode);
}
/**
* {@inheritdoc}
*/
public function compile($query) {
if (empty($this->conditions)) {
return function () {
return TRUE;
};
}
// This closure will fold the conditions into a single closure if there are
// more than one, depending on the conjunction.
$fold = strtoupper($this->conjunction) === 'AND' ?
function (array $filters) : callable {
return function ($item) use ($filters) : bool {
foreach ($filters as $filter) {
if (!$filter($item)) {
return FALSE;
}
}
return TRUE;
};
} :
function (array $filters) : callable {
return function ($item) use ($filters) : bool {
foreach ($filters as $filter) {
if ($filter($item)) {
return TRUE;
}
}
return FALSE;
};
};
$filters = [];
foreach ($this->conditions as $condition) {
// If the field is a condition object, compile it and add it to the
// filters.
if ($condition['field'] instanceof ConditionInterface) {
$filters[] = $condition['field']->compile($query);
}
else {
// Set the default operator if it is not set.
if (!isset($condition['operator'])) {
$condition['operator'] = is_array($condition['value']) ? 'IN' : '=';
}
// Normalize the value to lower case.
if (is_array($condition['value'])) {
$condition['value'] = array_map('mb_strtolower', $condition['value']);
}
elseif (is_string($condition['value'])) {
$condition['value'] = mb_strtolower($condition['value']);
}
$filters[] = static::matchProperty($condition);
}
}
// Only fold in case of multiple filters.
return count($filters) > 1 ? $fold($filters) : reset($filters);
}
/**
* Creates a filter closure that matches a property.
*
* @param array $condition
* Condition structure.
*
* @return callable
* Filter function.
*/
protected static function matchProperty(array $condition): callable {
return function ($item) use ($condition) : bool {
$value = static::getProperty($item, $condition['field']);
// Ignore object property values.
if (is_object($value)) {
return FALSE;
}
// Exit early in case of IS NULL or IS NOT NULL, because they can also
// deal with array values.
if (in_array($condition['operator'], ['IS NULL', 'IS NOT NULL'], TRUE)) {
$should_be_set = $condition['operator'] === 'IS NOT NULL';
return $should_be_set === isset($value);
}
if (isset($value)) {
if (!is_bool($value)) {
$value = mb_strtolower($value);
}
switch ($condition['operator']) {
case '=':
return $value == $condition['value'];
case '>':
return $value > $condition['value'];
case '<':
return $value < $condition['value'];
case '>=':
return $value >= $condition['value'];
case '<=':
return $value <= $condition['value'];
case '<>':
return $value != $condition['value'];
case 'IN':
return in_array($value, $condition['value'], TRUE);
case 'NOT IN':
return !in_array($value, $condition['value'], TRUE);
case 'STARTS_WITH':
return mb_strpos($value, $condition['value']) === 0;
case 'CONTAINS':
return mb_strpos($value, $condition['value']) !== FALSE;
case 'ENDS_WITH':
return mb_substr($value, -mb_strlen($condition['value'])) === (string) $condition['value'];
default:
throw new QueryException('Invalid condition operator.');
}
}
return FALSE;
};
}
/**
* Gets a property from an object.
*
* To do this, the function tries to guess the name of the getter.
*
* @param object $item
* Source object.
* @param string $property
* Property name.
*
* @return mixed|null
* Property value or NULL if not found.
*/
public static function getProperty($item, string $property) {
if (str_starts_with($property, 'AttributeValue.')) {
[$getter, $attribute_name] = explode('.', $property);
$getter_candidates[] = 'get' . $getter;
unset($getter);
$args = [];
$args[] = $attribute_name;
}
else {
$normalized = ucfirst(implode('', array_map('ucfirst', explode('_', $property))));
$args = NULL;
$getter_candidates = [
"is{$normalized}",
"get{$normalized}",
$normalized,
];
}
foreach ($getter_candidates as $getter) {
if (method_exists($item, $getter)) {
if ($args === NULL) {
return $item->$getter();
}
return $item->$getter(...$args);
}
}
return NULL;
}
}