Skip to content

Commit 8a44220

Browse files
committed
bug #1295 [Live] Fix date object hydration for custom format (1ed)
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 ------- a86b51b6 [Live] Fix date object hydration for custom format
2 parents 4e87314 + cfa962a commit 8a44220

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/LiveComponentHydrator.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ private function hydrateValue(mixed $value, LivePropMetadata $propMetadata, obje
439439
if ($propMetadata->collectionValueType() && Type::BUILTIN_TYPE_OBJECT === $propMetadata->collectionValueType()->getBuiltinType()) {
440440
$collectionClass = $propMetadata->collectionValueType()->getClassName();
441441
foreach ($value as $key => $objectItem) {
442-
$value[$key] = $this->hydrateObjectValue($objectItem, $collectionClass, true, $parentObject::class, sprintf('%s.%s', $propMetadata->getName(), $key), $parentObject);
442+
$value[$key] = $this->hydrateObjectValue($objectItem, $collectionClass, true, $propMetadata->getFormat(), $parentObject::class, sprintf('%s.%s', $propMetadata->getName(), $key), $parentObject);
443443
}
444444
}
445445

@@ -461,10 +461,10 @@ private function hydrateValue(mixed $value, LivePropMetadata $propMetadata, obje
461461
return $value;
462462
}
463463

464-
return $this->hydrateObjectValue($value, $propMetadata->getType(), $propMetadata->allowsNull(), $parentObject::class, $propMetadata->getName(), $parentObject);
464+
return $this->hydrateObjectValue($value, $propMetadata->getType(), $propMetadata->allowsNull(), $propMetadata->getFormat(), $parentObject::class, $propMetadata->getName(), $parentObject);
465465
}
466466

467-
private function hydrateObjectValue(mixed $value, string $className, bool $allowsNull, string $componentClassForError, string $propertyPathForError, object $component): ?object
467+
private function hydrateObjectValue(mixed $value, string $className, bool $allowsNull, ?string $dateFormat, string $componentClassForError, string $propertyPathForError, object $component): ?object
468468
{
469469
// enum
470470
if (is_a($className, \BackedEnum::class, true)) {
@@ -485,6 +485,10 @@ private function hydrateObjectValue(mixed $value, string $className, bool $allow
485485
throw new BadRequestHttpException(sprintf('The model path "%s" was sent an invalid data type "%s" for a date.', $propertyPathForError, get_debug_type($value)));
486486
}
487487

488+
if (null !== $dateFormat) {
489+
return $className::createFromFormat($dateFormat, $value);
490+
}
491+
488492
return new $className($value);
489493
}
490494

tests/Integration/LiveComponentHydratorTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,7 +1144,7 @@ public function hydrateDate($data)
11441144

11451145
yield 'Use the format option to control the date format' => [function () {
11461146
return HydrationTest::create(new class() {
1147-
#[LiveProp(writable: true, format: 'Y-m-d')]
1147+
#[LiveProp(writable: true, format: 'Y. m. d.')]
11481148
public \DateTime $createdAt;
11491149

11501150
public function __construct()
@@ -1156,13 +1156,13 @@ public function __construct()
11561156
'createdAt' => new \DateTime('2023-03-05 9:23', new \DateTimeZone('America/New_York')),
11571157
])
11581158
->assertDehydratesTo([
1159-
'createdAt' => '2023-03-05',
1159+
'createdAt' => '2023. 03. 05.',
11601160
])
11611161
->userUpdatesProps([
1162-
'createdAt' => '2024-04-06',
1162+
'createdAt' => '2024. 04. 06.',
11631163
])
11641164
->assertObjectAfterHydration(function (object $object) {
1165-
self::assertSame('2024-04-06', $object->createdAt->format('Y-m-d'));
1165+
self::assertSame('2024. 04. 06.', $object->createdAt->format('Y. m. d.'));
11661166
})
11671167
;
11681168
}];

0 commit comments

Comments
 (0)