Skip to content
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
4 changes: 4 additions & 0 deletions src/LiveComponent/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 2.25.0

- Add support for [Symfony UID](https://symfony.com/doc/current/components/uid.html) hydration/dehydration

## 2.23.0

- Allow configuring the secret used to compute fingerprints and checksums.
Expand Down
1 change: 1 addition & 0 deletions src/LiveComponent/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"symfony/security-bundle": "^5.4|^6.0|^7.0",
"symfony/serializer": "^5.4|^6.0|^7.0",
"symfony/twig-bundle": "^5.4|^6.0|^7.0",
"symfony/uid": "^5.4|^6.0|^7.0",
"symfony/validator": "^5.4|^6.0|^7.0",
"zenstruck/browser": "^1.2.0",
"zenstruck/foundry": "^2.0"
Expand Down
13 changes: 13 additions & 0 deletions src/LiveComponent/src/LiveComponentHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\Serializer\Exception\ExceptionInterface as SerializerExceptionInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Uid\AbstractUid;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\Exception\HydrationException;
Expand Down Expand Up @@ -505,6 +506,10 @@ private function dehydrateObjectValue(object $value, string $classType, ?string
return $value->value;
}

if ($value instanceof AbstractUid) {
return (string) $value;
}

foreach ($this->hydrationExtensions as $extension) {
if ($extension->supports($classType)) {
return $extension->dehydrate($value);
Expand Down Expand Up @@ -553,6 +558,14 @@ private function hydrateObjectValue(mixed $value, string $className, bool $allow
return new $className($value);
}

if (is_a($className, AbstractUid::class, true)) {
if (!\is_string($value)) {
throw new BadRequestHttpException(\sprintf('The model path "%s" was sent an invalid data type "%s" for a uuid.', $propertyPathForError, get_debug_type($value)));
}

return $className::fromString($value);
}

foreach ($this->hydrationExtensions as $extension) {
if ($extension->supports($className)) {
return $extension->hydrate($value, $className);
Expand Down
33 changes: 33 additions & 0 deletions src/LiveComponent/tests/Integration/LiveComponentHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Uid\UuidV4;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\Exception\HydrationException;
use Symfony\UX\LiveComponent\Metadata\LiveComponentMetadata;
Expand Down Expand Up @@ -1372,6 +1375,36 @@ public function modifyDateProp(LiveProp $prop): LiveProp
})
;
}];

yield 'Uuid: (de)hydrates correctly' => [function () {
$uuid = new UuidV4('ffdb229c-13e6-4bc4-939e-c8e73958104c');

return HydrationTest::create(new class {
#[LiveProp]
public Uuid $id;
})
->mountWith(['id' => $uuid])
->assertDehydratesTo(['id' => 'ffdb229c-13e6-4bc4-939e-c8e73958104c'])
->assertObjectAfterHydration(function (object $object) {
self::assertEquals(new UuidV4('ffdb229c-13e6-4bc4-939e-c8e73958104c'), $object->id);
})
;
}];

yield 'Ulid: (de)hydrates correctly' => [function () {
$uuid = new Ulid('01AN4Z07BY79KA1307SR9X4MV3');

return HydrationTest::create(new class {
#[LiveProp]
public Ulid $id;
})
->mountWith(['id' => $uuid])
->assertDehydratesTo(['id' => '01AN4Z07BY79KA1307SR9X4MV3'])
->assertObjectAfterHydration(function (object $object) {
self::assertEquals(new Ulid('01AN4Z07BY79KA1307SR9X4MV3'), $object->id);
})
;
}];
}

public function testHydrationWithInvalidDate(): void
Expand Down
Loading