diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon deleted file mode 100644 index 1636201..0000000 --- a/phpstan-baseline.neon +++ /dev/null @@ -1,11 +0,0 @@ -parameters: - ignoreErrors: - - - message: "#^PHPDoc tag @var for variable \\$aggregateRootType contains generic interface Dflydev\\\\EventSauce\\\\Support\\\\AggregateRoot\\\\AggregateRootIdAware but does not specify its types\\: T$#" - count: 1 - path: src/Messages/MessageInflection.php - - - - message: "#^PHPDoc tag @var for variable \\$aggregateRootTypeString contains generic interface Dflydev\\\\EventSauce\\\\Support\\\\AggregateRoot\\\\AggregateRootIdAware but does not specify its types\\: T$#" - count: 1 - path: src/Messages/MessageInflection.php diff --git a/src/Messages/MessageInflection.php b/src/Messages/MessageInflection.php index 2bf01a9..87f92fa 100644 --- a/src/Messages/MessageInflection.php +++ b/src/Messages/MessageInflection.php @@ -9,7 +9,6 @@ use EventSauce\EventSourcing\AggregateRootId; use EventSauce\EventSourcing\ClassNameInflector; use EventSauce\EventSourcing\Message; -use Whrc\Model\Common\Identity\Identity; final readonly class MessageInflection { @@ -53,13 +52,13 @@ public function extractAggregateRootId(Message $message, ?string $identityType = } /** - * @template T of Identity + * @template T of AggregateRootId * * @param class-string $identityType * * @phpstan-return T|AggregateRootId|null $value */ - public function extractOptionalIdentityFromHeader(Message $message, string $identityType, string $headerName): ?AggregateRootId + public function extractOptionalAggregateRootIdFromHeader(Message $message, string $identityType, string $headerName): ?AggregateRootId { return IdentityInflection::toObject( $identityType, @@ -68,15 +67,15 @@ public function extractOptionalIdentityFromHeader(Message $message, string $iden } /** - * @template T of Identity + * @template T of AggregateRootId * * @param class-string $identityType * * @phpstan-return T|AggregateRootId $value */ - public function extractIdentityFromHeader(Message $message, string $identityType, string $headerName): AggregateRootId + public function extractAggregateRootIdFromHeader(Message $message, string $identityType, string $headerName): AggregateRootId { - $value = $this->extractOptionalIdentityFromHeader($message, $identityType, $headerName); + $value = $this->extractOptionalAggregateRootIdFromHeader($message, $identityType, $headerName); if (is_null($value)) { throw new \RuntimeException('Expected $message to have an identity'); diff --git a/src/Serialization/CustomizedSerializablePayloadEdgeValue.php b/src/Serialization/CustomizedSerializablePayloadEdgeValue.php new file mode 100644 index 0000000..0b7d117 --- /dev/null +++ b/src/Serialization/CustomizedSerializablePayloadEdgeValue.php @@ -0,0 +1,27 @@ + $type + * + * @return TType + */ + public static function fromCustomSerializablePayloadEdgeValue(string $type, string $name, string|array|null $value = null): mixed; +} diff --git a/src/Serialization/EdgeAwareReflectionSerializing.php b/src/Serialization/EdgeAwareReflectionSerializing.php index 3564237..b9990af 100644 --- a/src/Serialization/EdgeAwareReflectionSerializing.php +++ b/src/Serialization/EdgeAwareReflectionSerializing.php @@ -31,16 +31,29 @@ public function toPayload(): array $key = $value::getPayloadKey(); } - if ($targetClass->implementsInterface(SerializablePayloadEdgeValue::class)) { + $factoryMethodName = sprintf('%sEdgeValueToPayload', $key); + + if (method_exists($this, $factoryMethodName)) { + $convertedValue = $this->$factoryMethodName($value); + + $value = $convertedValue; + } elseif ($targetClass->implementsInterface(SerializablePayloadEdgeValue::class)) { /** @var SerializablePayloadEdgeValue $value */ $convertedValue = $value->toPayloadValue(); - $value = $convertedValue; + $value = $convertedValue; } elseif ($targetClass->implementsInterface(SerializablePayload::class)) { /** @var SerializablePayload $value */ $convertedValue = $value->toPayload(); $value = $convertedValue; + } elseif ($object->implementsInterface(CustomizedSerializablePayloadEdgeValue::class)) { + /** @var CustomizedSerializablePayloadEdgeValue $this */ + if ($this->hasCustomSerializablePayloadEdgeValue($targetClass->getName(), $key)) { + $convertedValue = $this->toCustomSerializablePayloadEdgeValue($targetClass->getName(), $key, $value); + + $value = $convertedValue; + } } if ($targetClass->implementsInterface(DateTimeInterface::class) && $value) { @@ -93,12 +106,20 @@ public static function fromPayload(array $payload): static $payload[$key] = $targetClass->newInstance($payload[$key]); } - if (!is_null($payload[$key]) && $targetClass->implementsInterface(SerializablePayloadEdgeValue::class)) { + $factoryMethodName = sprintf('%sEdgeValueFromPayload', $key); + + if ($class->hasMethod($factoryMethodName)) { + $payload[$key] = static::$factoryMethodName($payload[$key]); + } elseif (!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]); + } elseif ($class->implementsInterface(CustomizedSerializablePayloadEdgeValue::class)) { + if (static::hasCustomSerializablePayloadEdgeValue($targetClass->getName(), $key)) { + $payload[$key] = static::fromCustomSerializablePayloadEdgeValue($targetClass->getName(), $key, $payload[$key]); + } } } diff --git a/src/Serialization/SupportsAwareCustomizedSerializablePayloadEdgeValues.php b/src/Serialization/SupportsAwareCustomizedSerializablePayloadEdgeValues.php new file mode 100644 index 0000000..f1b4877 --- /dev/null +++ b/src/Serialization/SupportsAwareCustomizedSerializablePayloadEdgeValues.php @@ -0,0 +1,29 @@ +