create-prisma-php-app 3.0.2 → 3.0.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.
@@ -17,6 +17,8 @@ use LibXMLError;
17
17
  use DOMXPath;
18
18
  use ReflectionClass;
19
19
  use ReflectionProperty;
20
+ use ReflectionType;
21
+ use ReflectionNamedType;
20
22
 
21
23
  class TemplateCompiler
22
24
  {
@@ -543,9 +545,12 @@ class TemplateCompiler
543
545
 
544
546
  foreach (self::$publicProperties[$className] as $prop) {
545
547
  $name = $prop->getName();
546
- if (array_key_exists($name, $attributes)) {
547
- $inst->$name = $attributes[$name];
548
+
549
+ if (!array_key_exists($name, $attributes)) {
550
+ continue;
548
551
  }
552
+ $value = self::coerce($attributes[$name], $prop->getType());
553
+ $prop->setValue($inst, $value);
549
554
  }
550
555
 
551
556
  if ($ctor) {
@@ -555,6 +560,52 @@ class TemplateCompiler
555
560
  return $inst;
556
561
  }
557
562
 
563
+ private static function coerce(mixed $value, ?ReflectionType $type): mixed
564
+ {
565
+ if (!$type instanceof ReflectionNamedType || $type->isBuiltin() === false) {
566
+ return $value;
567
+ }
568
+
569
+ $name = $type->getName();
570
+ if ($value === '' && $name === 'bool') return true;
571
+
572
+ return match ($name) {
573
+ 'bool' => filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? false,
574
+ 'int' => (int) $value,
575
+ 'float' => (float) $value,
576
+ 'array' => self::toArray($value),
577
+
578
+ default => $value,
579
+ };
580
+ }
581
+
582
+ private static function toArray(mixed $v): array
583
+ {
584
+ if (is_array($v)) {
585
+ return $v;
586
+ }
587
+ if (is_string($v)) {
588
+ $decoded = json_decode($v, true);
589
+ if (is_array($decoded)) {
590
+ return $decoded;
591
+ }
592
+ if (str_contains($v, ',')) {
593
+ return array_map('trim', explode(',', $v));
594
+ }
595
+ return [$decoded ?? self::coerceScalarString($v)];
596
+ }
597
+ return [$v];
598
+ }
599
+
600
+ private static function coerceScalarString(string $s): mixed
601
+ {
602
+ return match (strtolower($s)) {
603
+ 'true' => true,
604
+ 'false' => false,
605
+ default => $s,
606
+ };
607
+ }
608
+
558
609
  protected static function initializeClassMappings(): void
559
610
  {
560
611
  foreach (PrismaPHPSettings::$classLogFiles as $tag => $cls) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-prisma-php-app",
3
- "version": "3.0.2",
3
+ "version": "3.0.3",
4
4
  "description": "Prisma-PHP: A Revolutionary Library Bridging PHP with Prisma ORM",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",