prisma-client-php 2.0.3 → 2.0.5
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.
- package/dist/index.enc +1 -1
- package/dist/init.js +1 -1
- package/dist/src/Lib/Prisma/Classes/PPHPUtility.php +76 -15
- package/dist/src/Lib/Validator.php +41 -1
- package/package.json +1 -1
package/dist/init.js
CHANGED
|
@@ -202,7 +202,7 @@ const composerPinnedVersions = {
|
|
|
202
202
|
"ezyang/htmlpurifier": "^4.18.0",
|
|
203
203
|
"calicastle/cuid": "^2.0.0",
|
|
204
204
|
"symfony/uid": "^7.2.0",
|
|
205
|
-
"brick/math": "^0.13.
|
|
205
|
+
"brick/math": "^0.13.1",
|
|
206
206
|
};
|
|
207
207
|
function composerPkg(name) {
|
|
208
208
|
return composerPinnedVersions[name]
|
|
@@ -4,14 +4,9 @@ namespace Lib\Prisma\Classes;
|
|
|
4
4
|
|
|
5
5
|
use Lib\Validator;
|
|
6
6
|
use ReflectionClass;
|
|
7
|
-
use InvalidArgumentException;
|
|
8
|
-
use DateTime;
|
|
9
|
-
use Brick\Math\BigDecimal;
|
|
10
|
-
use Brick\Math\BigInteger;
|
|
11
|
-
use ReflectionUnionType;
|
|
12
|
-
use ReflectionNamedType;
|
|
13
7
|
use Exception;
|
|
14
8
|
use PDO;
|
|
9
|
+
use UnitEnum;
|
|
15
10
|
|
|
16
11
|
enum ArrayType: string
|
|
17
12
|
{
|
|
@@ -357,6 +352,28 @@ final class PPHPUtility
|
|
|
357
352
|
|
|
358
353
|
if (is_array($value)) {
|
|
359
354
|
foreach ($value as $condition => $val) {
|
|
355
|
+
|
|
356
|
+
$enumAllowed = ['equals', 'not', 'in', 'notIn'];
|
|
357
|
+
$unsupported = ['contains', 'startsWith', 'endsWith', 'gt', 'gte', 'lt', 'lte'];
|
|
358
|
+
|
|
359
|
+
$castEnum = static function ($v) use ($condition, $key, $enumAllowed, $unsupported) {
|
|
360
|
+
if ($v instanceof UnitEnum) {
|
|
361
|
+
if (in_array($condition, $unsupported, true)) {
|
|
362
|
+
$msg = "Operator '$condition' is not supported for enum field '$key'. ";
|
|
363
|
+
$msg .= 'Allowed operators: ' . implode(', ', $enumAllowed) . '.';
|
|
364
|
+
throw new Exception($msg);
|
|
365
|
+
}
|
|
366
|
+
return $v->value;
|
|
367
|
+
}
|
|
368
|
+
return $v;
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
if (in_array($condition, ['in', 'notIn'], true)) {
|
|
372
|
+
$val = array_map($castEnum, $val);
|
|
373
|
+
} else {
|
|
374
|
+
$val = $castEnum($val);
|
|
375
|
+
}
|
|
376
|
+
|
|
360
377
|
$bindingKey = ":" . $prefix . $key . "_" . $condition . $level;
|
|
361
378
|
switch ($condition) {
|
|
362
379
|
case 'contains':
|
|
@@ -419,6 +436,10 @@ final class PPHPUtility
|
|
|
419
436
|
} elseif ($value === '') {
|
|
420
437
|
$sqlConditions[] = "$qualifiedField = ''";
|
|
421
438
|
} else {
|
|
439
|
+
if ($value instanceof UnitEnum) {
|
|
440
|
+
$value = $value->value;
|
|
441
|
+
}
|
|
442
|
+
|
|
422
443
|
$bindingKey = ":" . $prefix . $key . $level;
|
|
423
444
|
$validatedValue = Validator::string($value, false);
|
|
424
445
|
$sqlConditions[] = "$qualifiedField = $bindingKey";
|
|
@@ -1024,22 +1045,62 @@ final class PPHPUtility
|
|
|
1024
1045
|
$parentId = $operations[0][$childFkFields[0]]
|
|
1025
1046
|
?? throw new Exception("Missing parent id in 'set' for '{$relatedFieldName}'.");
|
|
1026
1047
|
|
|
1027
|
-
$
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1048
|
+
$keepIds = [];
|
|
1049
|
+
foreach ($operations as $opSet) {
|
|
1050
|
+
if (isset($opSet[$relatedClass->_primaryKey])) {
|
|
1051
|
+
$keepIds[] = $opSet[$relatedClass->_primaryKey];
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
$deleteWhere = [
|
|
1056
|
+
$childFkFields[0] => $parentId,
|
|
1057
|
+
];
|
|
1058
|
+
if ($keepIds) {
|
|
1059
|
+
$deleteWhere[$relatedClass->_primaryKey] = ['notIn' => $keepIds];
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
$relatedClass->deleteMany(['where' => $deleteWhere]);
|
|
1031
1063
|
|
|
1032
1064
|
$newIds = [];
|
|
1033
1065
|
foreach ($operations as $opSet) {
|
|
1034
|
-
$where
|
|
1035
|
-
$fkUpdate
|
|
1036
|
-
|
|
1037
|
-
$
|
|
1066
|
+
$where = array_diff_key($opSet, array_flip($childFkFields));
|
|
1067
|
+
$fkUpdate = array_fill_keys($childFkFields, $parentId);
|
|
1068
|
+
|
|
1069
|
+
$row = $relatedClass->upsert([
|
|
1070
|
+
'where' => $where,
|
|
1071
|
+
'update' => $fkUpdate,
|
|
1072
|
+
'create' => array_merge($where, $fkUpdate),
|
|
1073
|
+
]);
|
|
1074
|
+
|
|
1075
|
+
$newIds[] = $row->{$relatedClass->_primaryKey};
|
|
1038
1076
|
}
|
|
1039
1077
|
|
|
1040
1078
|
$relatedResult = $relatedClass->findMany([
|
|
1041
|
-
'where' => [
|
|
1079
|
+
'where' => [
|
|
1080
|
+
$relatedClass->_primaryKey => ['in' => $newIds],
|
|
1081
|
+
],
|
|
1042
1082
|
]);
|
|
1083
|
+
$relatedResult = (array)$relatedResult;
|
|
1084
|
+
|
|
1085
|
+
if (!$requestOption) {
|
|
1086
|
+
return $relatedResult;
|
|
1087
|
+
}
|
|
1088
|
+
if (!$relatedResult) {
|
|
1089
|
+
throw new Exception("Failed to process related record for '{$relatedFieldName}'.");
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
if ($modelRelatedFieldIsList) {
|
|
1093
|
+
return [];
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
$bindings = [];
|
|
1097
|
+
foreach ($modelRelatedFromFields as $i => $fromField) {
|
|
1098
|
+
$toField = $modelRelatedToFields[$i];
|
|
1099
|
+
if (!isset($relatedResult[$toField])) {
|
|
1100
|
+
throw new Exception("The field '{$toField}' is missing …");
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
return $bindings;
|
|
1043
1104
|
} elseif (empty($modelRelatedFromFields) && empty($modelRelatedToFields)) {
|
|
1044
1105
|
$newRelatedIds = [];
|
|
1045
1106
|
$primaryId = null;
|
|
@@ -14,6 +14,8 @@ use Brick\Math\BigDecimal;
|
|
|
14
14
|
use Brick\Math\BigInteger;
|
|
15
15
|
use Brick\Math\Exception\MathException;
|
|
16
16
|
use Brick\Math\RoundingMode;
|
|
17
|
+
use InvalidArgumentException;
|
|
18
|
+
use BackedEnum;
|
|
17
19
|
|
|
18
20
|
final class Validator
|
|
19
21
|
{
|
|
@@ -305,6 +307,44 @@ final class Validator
|
|
|
305
307
|
return in_array($value, $allowedValues, true);
|
|
306
308
|
}
|
|
307
309
|
|
|
310
|
+
/**
|
|
311
|
+
* Validates and casts a value (or array of values) of a native enum.
|
|
312
|
+
*
|
|
313
|
+
* @template T of BackedEnum
|
|
314
|
+
* @param string|int|T|array<string|int|T> $value String, integer, instance, or array.
|
|
315
|
+
* @param class-string<T> $enumClass Enum class name.
|
|
316
|
+
* @return string|int|array<string|int>|null Backed value(s) or null if any is invalid.
|
|
317
|
+
* @throws InvalidArgumentException If the class is not an enum.
|
|
318
|
+
*/
|
|
319
|
+
public static function enumClass(mixed $value, string $enumClass): string|int|array|null
|
|
320
|
+
{
|
|
321
|
+
if (!enum_exists($enumClass)) {
|
|
322
|
+
throw new InvalidArgumentException("Enum '$enumClass' not found.");
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
$cast = static function ($v) use ($enumClass) {
|
|
326
|
+
if (is_object($v) && $v instanceof $enumClass && property_exists($v, 'value')) {
|
|
327
|
+
return $v->value;
|
|
328
|
+
}
|
|
329
|
+
$inst = $enumClass::tryFrom($v);
|
|
330
|
+
return $inst?->value;
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
if (is_array($value)) {
|
|
334
|
+
$out = [];
|
|
335
|
+
foreach ($value as $item) {
|
|
336
|
+
$val = $cast($item);
|
|
337
|
+
if ($val === null) {
|
|
338
|
+
return null;
|
|
339
|
+
}
|
|
340
|
+
$out[] = $val;
|
|
341
|
+
}
|
|
342
|
+
return $out;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
return $cast($value);
|
|
346
|
+
}
|
|
347
|
+
|
|
308
348
|
/**
|
|
309
349
|
* Purify and sanitize HTML content.
|
|
310
350
|
*
|
|
@@ -647,7 +687,7 @@ final class Validator
|
|
|
647
687
|
return true;
|
|
648
688
|
}
|
|
649
689
|
break;
|
|
650
|
-
|
|
690
|
+
// Add additional rules as needed...
|
|
651
691
|
default:
|
|
652
692
|
return true;
|
|
653
693
|
}
|
package/package.json
CHANGED