Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Live] Fix date object hydration for custom format #1295

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/LiveComponent/src/LiveComponentHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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)) {
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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.'));
})
;
}];
Expand Down