Skip to content
Open
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
12 changes: 12 additions & 0 deletions src/LiveComponent/src/LiveComponentHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,18 @@ public function hydrate(object $component, array $props, array $updatedProps, Li
$dehydratedUpdatedProps,
);

if (0 < \count($updatedWritablePaths)) {
try {
$propertyValue = $this->hydrateValue(
$propertyValue,
$propMetadata,
$component,
);
} catch (HydrationException $e) {
// swallow this: it's bad data from the user
}
}

try {
$this->propertyAccessor->setValue($component, $propMetadata->getName(), $propertyValue);
} catch (PropertyAccessExceptionInterface $exception) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\UX\LiveComponent\Tests\Fixtures\Component;

use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveProp;

#[AsLiveComponent]
final class ComponentWithHydrateWithProps
{
/**
* @var array<string, int>
*/
#[LiveProp(writable: true, hydrateWith: 'hydrateIntegers')]
public array $integers = [
'one' => 1,
'two' => 2,
'three' => 3,
];

/**
* @param array<string, mixed> $data
*
* @return array<string, int>
*/
public function hydrateIntegers(array $data): array
{
return array_map(intval(...), $data);
}
}
75 changes: 75 additions & 0 deletions src/LiveComponent/tests/Unit/LiveComponentHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\UX\LiveComponent\Tests\Unit;

use PHPUnit\Framework\TestCase;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
Expand All @@ -20,9 +21,13 @@
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\LiveComponentHydrator;
use Symfony\UX\LiveComponent\Metadata\LegacyLivePropMetadata;
use Symfony\UX\LiveComponent\Metadata\LiveComponentMetadata;
use Symfony\UX\LiveComponent\Metadata\LiveComponentMetadataFactory;
use Symfony\UX\LiveComponent\Metadata\LivePropMetadata;
use Symfony\UX\LiveComponent\Tests\Fixtures\Component\ComponentWithHydrateWithProps;
use Symfony\UX\TwigComponent\ComponentMetadata;
use Twig\Environment;
use Twig\Runtime\EscaperRuntime;

final class LiveComponentHydratorTest extends TestCase
{
Expand Down Expand Up @@ -80,6 +85,76 @@ public function testItCanHydrateWithNullValues()
self::assertNull($hydratedValue);
}
}

public function testHydrateWithIsCalledWithNestedArrayAsProps()
{
$twigMock = $this->createMock(Environment::class);
$twigMock->method('getRuntime')->willReturn(new EscaperRuntime());
$hydrator = new LiveComponentHydrator(
[],
PropertyAccess::createPropertyAccessor(),
$this->createMock(LiveComponentMetadataFactory::class),
new Serializer(normalizers: [new ObjectNormalizer()]),
'foo',
$twigMock,
);
$component = new ComponentWithHydrateWithProps();

// BC layer when "symfony/type-info" is not available
if (!class_exists(Type::class)) {
$hydrator->hydrate(
$component,
[
'@checksum' => 'SNKH1tHUgwgWT8V+Z/A7z126JQ2dWGiG6xVmjx7FbeA=',
'integers' => [
'one' => 1,
'two' => 2,
'three' => 3,
],
],
[
'integers.one' => '4',
'integers.two' => '5',
'integers.three' => '6',
],
new LiveComponentMetadata(
new ComponentMetadata([]),
[
new LegacyLivePropMetadata('integers', new LiveProp(writable: true, hydrateWith: 'hydrateIntegers'), typeName: 'array', isBuiltIn: true, allowsNull: false, collectionValueType: new \Symfony\Component\PropertyInfo\Type('int')),
],
),
);
} else {
$hydrator->hydrate(
$component,
[
'@checksum' => 'SNKH1tHUgwgWT8V+Z/A7z126JQ2dWGiG6xVmjx7FbeA=',
'integers' => [
'one' => 1,
'two' => 2,
'three' => 3,
],
],
[
'integers.one' => '4',
'integers.two' => '5',
'integers.three' => '6',
],
new LiveComponentMetadata(
new ComponentMetadata([]),
[
new LivePropMetadata('integers', new LiveProp(writable: true, hydrateWith: 'hydrateIntegers'), Type::array(Type::int(), Type::string())),
],
),
);
}

self::assertSame([
'one' => 4,
'two' => 5,
'three' => 6,
], $component->integers);
}
}

class Foo
Expand Down
Loading