Skip to content

Commit ba53343

Browse files
author
matheo
committed
fix exeception and use PropertyAccessor to read the value
1 parent 6e4854d commit ba53343

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/LiveComponent/src/LiveComponentHydrator.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
1717
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
1818
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
19+
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
20+
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
1921
use Symfony\Component\PropertyInfo\Type;
2022
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
2123
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
@@ -358,14 +360,14 @@ private function dehydrateValue(mixed $value, LivePropMetadata $propMetadata, ob
358360
if (!$this->isValueValidDehydratedValue($value)) {
359361
$badKeys = $this->getNonScalarKeys($value, $propMetadata->getName());
360362
$badKeysText = implode(', ', array_map(fn ($key) => sprintf('%s: %s', $key, $badKeys[$key]), array_keys($badKeys)));
361-
throw new \LogicException(sprintf('The LiveProp "%s" on component "%s" is an array, but it contains one or more keys that are not scalars: %s', $propMetadata->getName(), $parentObject::class, $badKeysText));
363+
throw new \LogicException(throw new \LogicException(sprintf('Unable to dehydrate value of type "%s" for property "%s" on component "%s". Change this to a simpler type of an object that can be dehydrated. Or set the hydrateWith/dehydrateWith options in LiveProp or set "useSerializerForHydration: true" on the LiveProp to use the serializer.', get_debug_type($value), $propMetadata->getName(), $parentObject::class)));
362364
}
363365

364366
return $value;
365367
}
366368

367369
if (!\is_object($value)) {
368-
throw new \LogicException(sprintf('The "%s" object has a hydrateMethod of "%s" but the method does not exist.', $parentObject::class, $propMetadata->hydrateMethod()));
370+
throw new \LogicException(sprintf('Unable to dehydrate value of type "%s" for property "%s" on component "%s". Change this to a simpler type of an object that can be dehydrated. Or set the hydrateWith/dehydrateWith options in LiveProp or set "useSerializerForHydration: true" on the LiveProp to use the serializer.', get_debug_type($value), $propMetadata->getName(), $parentObject::class));
369371
}
370372

371373
if (!$propMetadata->getType() || $propMetadata->isBuiltIn()) {
@@ -395,10 +397,10 @@ private function dehydrateObjectValue(object $value, string $classType, ?string
395397
}
396398

397399
$dehydratedObjectValues = [];
398-
foreach ((new \ReflectionClass($classType))->getProperties() as $property) {
399-
$propertyValue = $this->propertyAccessor->getValue($value, $property->getName());
400-
$propMetadata = $this->liveComponentMetadataFactory->createLivePropMetadata($classType, $property->getName(), $property, new LiveProp());
401-
$dehydratedObjectValues[$property->getName()] = $this->dehydrateValue($propertyValue, $propMetadata, $parentObject);
400+
foreach ((new PropertyInfoExtractor([new ReflectionExtractor()]))->getProperties($classType) as $property) {
401+
$propertyValue = $this->propertyAccessor->getValue($value, $property);
402+
$propMetadata = $this->liveComponentMetadataFactory->createLivePropMetadata($classType, $property, new \ReflectionProperty($classType, $property), new LiveProp());
403+
$dehydratedObjectValues[$property] = $this->dehydrateValue($propertyValue, $propMetadata, $parentObject);
402404
}
403405

404406
return $dehydratedObjectValues;
@@ -479,8 +481,8 @@ private function hydrateObjectValue(mixed $value, string $className, bool $allow
479481
if (\is_array($value)) {
480482
$object = new $className();
481483
foreach ($value as $propertyName => $propertyValue) {
482-
$reflexionClass = new \ReflectionClass($className);
483-
$property = $reflexionClass->getProperty($propertyName);
484+
$reflectionClass = new \ReflectionClass($className);
485+
$property = $reflectionClass->getProperty($propertyName);
484486
$propMetadata = $this->liveComponentMetadataFactory->createLivePropMetadata($className, $propertyName, $property, new LiveProp());
485487
$this->propertyAccessor->setValue($object, $propertyName, $this->hydrateValue($propertyValue, $propMetadata, $component));
486488
}

0 commit comments

Comments
 (0)