Skip to content

Commit feb1f44

Browse files
author
matheo
committed
rewrite errors and renames variable
1 parent 28e3b39 commit feb1f44

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

src/LiveComponent/src/LiveComponentHydrator.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -325,14 +325,14 @@ private function setWritablePaths(array $writablePaths, string $frontendPropName
325325
return $propertyValue;
326326
}
327327

328-
private function dehydrateValue(mixed $value, LivePropMetadata $propMetadata, object $component): mixed
328+
private function dehydrateValue(mixed $value, LivePropMetadata $propMetadata, object $parentObject): mixed
329329
{
330330
if ($method = $propMetadata->dehydrateMethod()) {
331-
if (!method_exists($component, $method)) {
332-
throw new \LogicException(sprintf('The "%s" component has a dehydrateMethod of "%s" but the method does not exist.', $component::class, $method));
331+
if (!method_exists($parentObject, $method)) {
332+
throw new \LogicException(sprintf('The dehydration failed for class "%s" because the "%s" method does not exist.', $parentObject::class, $method));
333333
}
334334

335-
return $component->$method($value);
335+
return $parentObject->$method($value);
336336
}
337337

338338
if ($propMetadata->useSerializerForHydration()) {
@@ -348,37 +348,37 @@ private function dehydrateValue(mixed $value, LivePropMetadata $propMetadata, ob
348348
$collectionClass = $propMetadata->collectionValueType()->getClassName();
349349
foreach ($value as $key => $objectItem) {
350350
if (!$objectItem instanceof $collectionClass) {
351-
throw new \LogicException(sprintf('The LiveProp "%s" on component "%s" is an array. We determined the array is full of %s objects, but at least on key had a different value of %s', $propMetadata->getName(), $component::class, $collectionClass, get_debug_type($objectItem)));
351+
throw new \LogicException(sprintf('The LiveProp "%s" on component "%s" is an array. We determined the array is full of %s objects, but at least on key had a different value of %s', $propMetadata->getName(), $parentObject::class, $collectionClass, get_debug_type($objectItem)));
352352
}
353353

354-
$value[$key] = $this->dehydrateObjectValue($objectItem, $collectionClass, $propMetadata->getFormat(), $component);
354+
$value[$key] = $this->dehydrateObjectValue($objectItem, $collectionClass, $propMetadata->getFormat(), $parentObject);
355355
}
356356
}
357357

358358
if (!$this->isValueValidDehydratedValue($value)) {
359359
$badKeys = $this->getNonScalarKeys($value, $propMetadata->getName());
360360
$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(), $component::class, $badKeysText));
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));
362362
}
363363

364364
return $value;
365365
}
366366

367367
if (!\is_object($value)) {
368-
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(), $component::class));
368+
throw new \LogicException(sprintf('The "%s" object has a hydrateMethod of "%s" but the method does not exist.', $parentObject::class, $propMetadata->hydrateMethod()));
369369
}
370370

371371
if (!$propMetadata->getType() || $propMetadata->isBuiltIn()) {
372-
throw new \LogicException(sprintf('The "%s" property on component "%s" is missing its property-type. Add the "%s" type so the object can be hydrated later.', $propMetadata->getName(), $component::class, $value::class));
372+
throw new \LogicException(sprintf('The "%s" property on component "%s" is missing its property-type. Add the "%s" type so the object can be hydrated later.', $propMetadata->getName(), $parentObject::class, $value::class));
373373
}
374374

375375
// at this point, we have an object and can assume $propMetadata->getType()
376376
// is set correctly (needed for hydration later)
377377

378-
return $this->dehydrateObjectValue($value, $propMetadata->getType(), $propMetadata->getFormat(), $component);
378+
return $this->dehydrateObjectValue($value, $propMetadata->getType(), $propMetadata->getFormat(), $parentObject);
379379
}
380380

381-
private function dehydrateObjectValue(object $value, string $classType, ?string $dateFormat, object $component): mixed
381+
private function dehydrateObjectValue(object $value, string $classType, ?string $dateFormat, object $parentObject): mixed
382382
{
383383
if ($value instanceof \DateTimeInterface) {
384384
return $value->format($dateFormat ?: \DateTimeInterface::RFC3339);
@@ -398,20 +398,20 @@ private function dehydrateObjectValue(object $value, string $classType, ?string
398398
foreach ((new \ReflectionClass($classType))->getProperties() as $property) {
399399
$propertyValue = $this->propertyAccessor->getValue($value, $property->getName());
400400
$propMetadata = $this->liveComponentMetadataFactory->createLivePropMetadata($classType, $property->getName(), $property, new LiveProp());
401-
$dehydratedObjectValues[$property->getName()] = $this->dehydrateValue($propertyValue, $propMetadata, $component);
401+
$dehydratedObjectValues[$property->getName()] = $this->dehydrateValue($propertyValue, $propMetadata, $parentObject);
402402
}
403403

404404
return $dehydratedObjectValues;
405405
}
406406

407-
private function hydrateValue(mixed $value, LivePropMetadata $propMetadata, object $component): mixed
407+
private function hydrateValue(mixed $value, LivePropMetadata $propMetadata, object $parentObject): mixed
408408
{
409409
if ($propMetadata->hydrateMethod()) {
410-
if (!method_exists($component, $propMetadata->hydrateMethod())) {
411-
throw new \LogicException(sprintf('The "%s" component has a hydrateMethod of "%s" but the method does not exist.', $component::class, $propMetadata->hydrateMethod()));
410+
if (!method_exists($parentObject, $propMetadata->hydrateMethod())) {
411+
throw new \LogicException(sprintf('The "%s" object has a hydrateMethod of "%s" but the method does not exist.', $parentObject::class, $propMetadata->hydrateMethod()));
412412
}
413413

414-
return $component->{$propMetadata->hydrateMethod()}($value);
414+
return $parentObject->{$propMetadata->hydrateMethod()}($value);
415415
}
416416

417417
if ($propMetadata->useSerializerForHydration()) {
@@ -421,7 +421,7 @@ private function hydrateValue(mixed $value, LivePropMetadata $propMetadata, obje
421421
if ($propMetadata->collectionValueType() && Type::BUILTIN_TYPE_OBJECT === $propMetadata->collectionValueType()->getBuiltinType()) {
422422
$collectionClass = $propMetadata->collectionValueType()->getClassName();
423423
foreach ($value as $key => $objectItem) {
424-
$value[$key] = $this->hydrateObjectValue($objectItem, $collectionClass, true, $component::class, sprintf('%s.%s', $propMetadata->getName(), $key), $component);
424+
$value[$key] = $this->hydrateObjectValue($objectItem, $collectionClass, true, $parentObject::class, sprintf('%s.%s', $propMetadata->getName(), $key), $parentObject);
425425
}
426426
}
427427

@@ -443,7 +443,7 @@ private function hydrateValue(mixed $value, LivePropMetadata $propMetadata, obje
443443
return $value;
444444
}
445445

446-
return $this->hydrateObjectValue($value, $propMetadata->getType(), $propMetadata->allowsNull(), $component::class, $propMetadata->getName(), $component);
446+
return $this->hydrateObjectValue($value, $propMetadata->getType(), $propMetadata->allowsNull(), $parentObject::class, $propMetadata->getName(), $parentObject);
447447
}
448448

449449
private function hydrateObjectValue(mixed $value, string $className, bool $allowsNull, string $componentClassForError, string $propertyPathForError, object $component): ?object

src/LiveComponent/tests/Integration/LiveComponentHydratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ public function mount()
836836
'address' => '1 rue du Bac',
837837
'city' => 'Paris',
838838
],
839-
]
839+
],
840840
])
841841
->userUpdatesProps(['customerDetails' => ['lastName' => 'Matheo', 'firstName' => 'Daninos', 'address' => ['address' => '3 rue du Bac', 'city' => 'Paris']]])
842842
->assertObjectAfterHydration(function (object $object) {

0 commit comments

Comments
 (0)