create-prisma-php-app 2.3.30 → 2.3.31
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/src/Lib/Validator.php +41 -1
- package/package.json +1 -1
|
@@ -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
|
}
|