diff --git a/src/LiveComponent/src/LiveComponentHydrator.php b/src/LiveComponent/src/LiveComponentHydrator.php index 16078cffc05..9d439ca6e63 100644 --- a/src/LiveComponent/src/LiveComponentHydrator.php +++ b/src/LiveComponent/src/LiveComponentHydrator.php @@ -439,7 +439,7 @@ private function hydrateValue(mixed $value, LivePropMetadata $propMetadata, obje if ($propMetadata->collectionValueType() && Type::BUILTIN_TYPE_OBJECT === $propMetadata->collectionValueType()->getBuiltinType()) { $collectionClass = $propMetadata->collectionValueType()->getClassName(); foreach ($value as $key => $objectItem) { - $value[$key] = $this->hydrateObjectValue($objectItem, $collectionClass, true, $parentObject::class, sprintf('%s.%s', $propMetadata->getName(), $key), $parentObject); + $value[$key] = $this->hydrateObjectValue($objectItem, $collectionClass, true, $propMetadata->getFormat(), $parentObject::class, sprintf('%s.%s', $propMetadata->getName(), $key), $parentObject); } } @@ -461,10 +461,10 @@ private function hydrateValue(mixed $value, LivePropMetadata $propMetadata, obje return $value; } - return $this->hydrateObjectValue($value, $propMetadata->getType(), $propMetadata->allowsNull(), $parentObject::class, $propMetadata->getName(), $parentObject); + return $this->hydrateObjectValue($value, $propMetadata->getType(), $propMetadata->allowsNull(), $propMetadata->getFormat(), $parentObject::class, $propMetadata->getName(), $parentObject); } - private function hydrateObjectValue(mixed $value, string $className, bool $allowsNull, string $componentClassForError, string $propertyPathForError, object $component): ?object + private function hydrateObjectValue(mixed $value, string $className, bool $allowsNull, ?string $dateFormat, string $componentClassForError, string $propertyPathForError, object $component): ?object { // enum if (is_a($className, \BackedEnum::class, true)) { @@ -485,6 +485,10 @@ private function hydrateObjectValue(mixed $value, string $className, bool $allow throw new BadRequestHttpException(sprintf('The model path "%s" was sent an invalid data type "%s" for a date.', $propertyPathForError, get_debug_type($value))); } + if (null !== $dateFormat) { + return $className::createFromFormat($dateFormat, $value); + } + return new $className($value); } diff --git a/src/LiveComponent/tests/Integration/LiveComponentHydratorTest.php b/src/LiveComponent/tests/Integration/LiveComponentHydratorTest.php index 75b8529adca..a922b855450 100644 --- a/src/LiveComponent/tests/Integration/LiveComponentHydratorTest.php +++ b/src/LiveComponent/tests/Integration/LiveComponentHydratorTest.php @@ -1144,7 +1144,7 @@ public function hydrateDate($data) yield 'Use the format option to control the date format' => [function () { return HydrationTest::create(new class() { - #[LiveProp(writable: true, format: 'Y-m-d')] + #[LiveProp(writable: true, format: 'Y. m. d.')] public \DateTime $createdAt; public function __construct() @@ -1156,13 +1156,13 @@ public function __construct() 'createdAt' => new \DateTime('2023-03-05 9:23', new \DateTimeZone('America/New_York')), ]) ->assertDehydratesTo([ - 'createdAt' => '2023-03-05', + 'createdAt' => '2023. 03. 05.', ]) ->userUpdatesProps([ - 'createdAt' => '2024-04-06', + 'createdAt' => '2024. 04. 06.', ]) ->assertObjectAfterHydration(function (object $object) { - self::assertSame('2024-04-06', $object->createdAt->format('Y-m-d')); + self::assertSame('2024. 04. 06.', $object->createdAt->format('Y. m. d.')); }) ; }];