create-prisma-php-app 1.7.2 → 1.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -162,4 +162,116 @@ abstract class Utility
|
|
|
162
162
|
}
|
|
163
163
|
}
|
|
164
164
|
}
|
|
165
|
+
|
|
166
|
+
public static function processConditions(array $conditions, &$sqlConditions, &$bindings, $dbType, $prefix = '', $level = 0)
|
|
167
|
+
{
|
|
168
|
+
foreach ($conditions as $key => $value) {
|
|
169
|
+
if (in_array($key, ['AND', 'OR', 'NOT'])) {
|
|
170
|
+
$groupedConditions = [];
|
|
171
|
+
if ($key === 'NOT') {
|
|
172
|
+
self::processNotCondition($value, $groupedConditions, $bindings, $dbType, $prefix . $key . '_', $level);
|
|
173
|
+
} else {
|
|
174
|
+
foreach ($value as $conditionIndex => $subCondition) {
|
|
175
|
+
if ($key === 'OR' && is_array($subCondition)) {
|
|
176
|
+
if (!is_numeric($conditionIndex)) {
|
|
177
|
+
throw new \Exception("The '$key' condition must be an indexed array.");
|
|
178
|
+
}
|
|
179
|
+
foreach ($subCondition as $subKey => $subValue) {
|
|
180
|
+
self::processSingleCondition($subKey, $subValue, $groupedConditions, $bindings, $dbType, $prefix . $key . $conditionIndex . '_', $level + 1);
|
|
181
|
+
}
|
|
182
|
+
} else {
|
|
183
|
+
if (is_numeric($conditionIndex)) {
|
|
184
|
+
self::processConditions($subCondition, $groupedConditions, $bindings, $dbType, $prefix . $key . $conditionIndex . '_', $level + 1);
|
|
185
|
+
} else {
|
|
186
|
+
self::processSingleCondition($conditionIndex, $subCondition, $groupedConditions, $bindings, $dbType, $prefix . $key . $conditionIndex . '_', $level + 1);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
if (!empty($groupedConditions)) {
|
|
192
|
+
$conditionGroup = '(' . implode(" $key ", $groupedConditions) . ')';
|
|
193
|
+
if ($key === 'NOT') {
|
|
194
|
+
$conditionGroup = 'NOT ' . $conditionGroup;
|
|
195
|
+
}
|
|
196
|
+
$sqlConditions[] = $conditionGroup;
|
|
197
|
+
}
|
|
198
|
+
} else {
|
|
199
|
+
self::processSingleCondition($key, $value, $sqlConditions, $bindings, $dbType, $prefix, $level);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
private static function processSingleCondition($key, $value, &$sqlConditions, &$bindings, $dbType, $prefix, $level)
|
|
205
|
+
{
|
|
206
|
+
$fieldQuoted = $dbType == 'pgsql' ? "\"$key\"" : "`$key`";
|
|
207
|
+
if (is_array($value)) {
|
|
208
|
+
foreach ($value as $condition => $val) {
|
|
209
|
+
$bindingKey = ":" . $prefix . $key . "_" . $condition . $level;
|
|
210
|
+
switch ($condition) {
|
|
211
|
+
case 'contains':
|
|
212
|
+
case 'startsWith':
|
|
213
|
+
case 'endsWith':
|
|
214
|
+
case 'equals':
|
|
215
|
+
case 'not':
|
|
216
|
+
$validatedValue = Validator::validateString($val);
|
|
217
|
+
$likeOperator = $condition === 'contains' ? ($dbType == 'pgsql' ? 'ILIKE' : 'LIKE') : '=';
|
|
218
|
+
if ($condition === 'startsWith') $validatedValue .= '%';
|
|
219
|
+
if ($condition === 'endsWith') $validatedValue = '%' . $validatedValue;
|
|
220
|
+
if ($condition === 'contains') $validatedValue = '%' . $validatedValue . '%';
|
|
221
|
+
$sqlConditions[] = $condition === 'not' ? "$fieldQuoted != $bindingKey" : "$fieldQuoted $likeOperator $bindingKey";
|
|
222
|
+
$bindings[$bindingKey] = $validatedValue;
|
|
223
|
+
break;
|
|
224
|
+
case 'gt':
|
|
225
|
+
case 'gte':
|
|
226
|
+
case 'lt':
|
|
227
|
+
case 'lte':
|
|
228
|
+
$validatedValue = is_float($val) ? Validator::validateFloat($val) : Validator::validateInt($val);
|
|
229
|
+
$operator = $condition === 'gt' ? '>' : ($condition === 'gte' ? '>=' : ($condition === 'lt' ? '<' : '<='));
|
|
230
|
+
$sqlConditions[] = "$fieldQuoted $operator $bindingKey";
|
|
231
|
+
$bindings[$bindingKey] = $validatedValue;
|
|
232
|
+
break;
|
|
233
|
+
case 'in':
|
|
234
|
+
case 'notIn':
|
|
235
|
+
$inPlaceholders = [];
|
|
236
|
+
foreach ($val as $i => $inVal) {
|
|
237
|
+
$inKey = $bindingKey . "_" . $i;
|
|
238
|
+
// Assuming the values are strings; adjust validation as necessary.
|
|
239
|
+
$validatedValue = Validator::validateString($inVal);
|
|
240
|
+
$inPlaceholders[] = $inKey;
|
|
241
|
+
$bindings[$inKey] = $validatedValue;
|
|
242
|
+
}
|
|
243
|
+
$inClause = implode(', ', $inPlaceholders);
|
|
244
|
+
$sqlConditions[] = "$fieldQuoted " . ($condition === 'notIn' ? 'NOT IN' : 'IN') . " ($inClause)";
|
|
245
|
+
break;
|
|
246
|
+
default:
|
|
247
|
+
// Handle other conditions or log an error/warning for unsupported conditions
|
|
248
|
+
throw new \Exception("Unsupported condition: $condition");
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
} else {
|
|
253
|
+
// For scalar non-array values; direct equals condition assumed
|
|
254
|
+
$bindingKey = ":" . $prefix . $key . $level;
|
|
255
|
+
// Use appropriate validation based on expected type; assuming string for simplicity
|
|
256
|
+
$validatedValue = Validator::validateString($value);
|
|
257
|
+
$sqlConditions[] = "$fieldQuoted = $bindingKey";
|
|
258
|
+
$bindings[$bindingKey] = $validatedValue;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
private static function processNotCondition($conditions, &$sqlConditions, &$bindings, $dbType, $prefix, $level = 0)
|
|
263
|
+
{
|
|
264
|
+
foreach ($conditions as $key => $value) {
|
|
265
|
+
self::processSingleCondition($key, $value, $sqlConditions, $bindings, $dbType, $prefix . 'NOT_', $level);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
public static function checkForInvalidKeys(array $data, array $fields, string $modelName)
|
|
270
|
+
{
|
|
271
|
+
foreach ($data as $key => $value) {
|
|
272
|
+
if (!empty($key) && !in_array($key, $fields)) {
|
|
273
|
+
throw new \Exception("The field '$key' does not exist in the $modelName model.");
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
165
277
|
}
|