Skip to content

Commit

Permalink
bug #1295 [Live] Fix date object hydration for custom format (1ed)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.x branch.

Discussion
----------

[Live] Fix date object hydration for custom format

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| Issues        | -
| License       | MIT

Commits
-------

a86b51b [Live] Fix date object hydration for custom format
  • Loading branch information
weaverryan committed Dec 1, 2023
2 parents 311eb12 + a86b51b commit 1ef2337
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
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

0 comments on commit 1ef2337

Please sign in to comment.