Skip to content

Commit

Permalink
Remove WHRC reference, phpstan-baseline.neon, and added customizable …
Browse files Browse the repository at this point in the history
…payload edge value serialization
  • Loading branch information
simensen committed Aug 23, 2023
1 parent 67c365b commit b4d7318
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 20 deletions.
11 changes: 0 additions & 11 deletions phpstan-baseline.neon

This file was deleted.

11 changes: 5 additions & 6 deletions src/Messages/MessageInflection.php
Expand Up @@ -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
{
Expand Down Expand Up @@ -53,13 +52,13 @@ public function extractAggregateRootId(Message $message, ?string $identityType =
}

/**
* @template T of Identity
* @template T of AggregateRootId
*
* @param class-string<T> $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,
Expand All @@ -68,15 +67,15 @@ public function extractOptionalIdentityFromHeader(Message $message, string $iden
}

/**
* @template T of Identity
* @template T of AggregateRootId
*
* @param class-string<T> $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');
Expand Down
27 changes: 27 additions & 0 deletions src/Serialization/CustomizedSerializablePayloadEdgeValue.php
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Dflydev\EventSauce\Support\Serialization;

interface CustomizedSerializablePayloadEdgeValue
{
/**
* @param class-string $type
*/
public static function hasCustomSerializablePayloadEdgeValue(string $type, string $name): bool;

/**
* @param class-string $type
*/
public function toCustomSerializablePayloadEdgeValue(string $type, string $name, mixed $value = null): string|array|null;

/**
* @template TType
*
* @param class-string<TType> $type
*
* @return TType
*/
public static function fromCustomSerializablePayloadEdgeValue(string $type, string $name, string|array|null $value = null): mixed;
}
27 changes: 24 additions & 3 deletions src/Serialization/EdgeAwareReflectionSerializing.php
Expand Up @@ -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) {
Expand Down Expand Up @@ -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]);
}
}
}

Expand Down
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Dflydev\EventSauce\Support\Serialization;

trait SupportsAwareCustomizedSerializablePayloadEdgeValues
{
abstract public static function supportsCustomSerializablePayloadEdgeValues(): array;

public static function hasCustomSerializablePayloadEdgeValue(string $type, string $name): bool
{
return array_key_exists($name, self::supportsCustomSerializablePayloadEdgeValues());
}

public function toCustomSerializablePayloadEdgeValue(string $type, string $name, mixed $value = null): string|array|null
{
$callable = static::supportsCustomSerializablePayloadEdgeValues()[$name]['toPayload'];

return $callable($type, $name, $value);
}

public static function fromCustomSerializablePayloadEdgeValue(string $type, string $name, array|string|null $value = null): mixed
{
$callable = static::supportsCustomSerializablePayloadEdgeValues()[$name]['fromPayload'];

return $callable($type, $name, $value);
}
}

0 comments on commit b4d7318

Please sign in to comment.