Skip to content

Commit

Permalink
Ensure EdgeAwareReflectionSerializing traverses SerializablePayloads
Browse files Browse the repository at this point in the history
  • Loading branch information
simensen committed Aug 21, 2023
1 parent 4ead5a7 commit 67c365b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/Serialization/EdgeAwareReflectionSerializing.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Dflydev\EventSauce\Support\Serialization;

use DateTimeInterface;
use EventSauce\EventSourcing\Serialization\SerializablePayload;
use ReflectionClass;
use ReflectionNamedType;
use ReflectionObject;
Expand All @@ -14,7 +15,6 @@ trait EdgeAwareReflectionSerializing
public function toPayload(): array
{
$payload = [];

$object = new ReflectionObject($this);
foreach ($object->getProperties() as $property) {
$key = $property->getName();
Expand All @@ -35,6 +35,11 @@ public function toPayload(): array
/** @var SerializablePayloadEdgeValue $value */
$convertedValue = $value->toPayloadValue();

$value = $convertedValue;
} elseif ($targetClass->implementsInterface(SerializablePayload::class)) {
/** @var SerializablePayload $value */
$convertedValue = $value->toPayload();

$value = $convertedValue;
}

Expand Down Expand Up @@ -91,6 +96,9 @@ public static function fromPayload(array $payload): static
if (!is_null($payload[$key]) && $targetClass->implementsInterface(SerializablePayloadEdgeValue::class)) {
/** @var SerializablePayloadEdgeValue $targetClassName */
$payload[$key] = $targetClassName::fromPayloadValue($payload[$key]);
} elseif (!is_null($payload[$key]) && $targetClass->implementsInterface(SerializablePayload::class)) {
/** @var SerializablePayload $targetClassName */
$payload[$key] = $targetClassName::fromPayload($payload[$key]);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Testing/TestObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* @template T of object
*/
class TestObject
abstract class TestObject
{
/** @var array<string,mixed> */
private array $arguments = [];
Expand Down

0 comments on commit 67c365b

Please sign in to comment.