Skip to content

Commit

Permalink
Fix #41 - Add support for DateTime in payload (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
yvoyer authored Jan 28, 2025
1 parent 0693368 commit 9c366b1
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 92 deletions.
81 changes: 34 additions & 47 deletions src/Serialization/AlwaysThrowExceptionOnFailure.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
namespace Star\Component\DomainEvent\Serialization;

use Assert\Assertion;
use DateTimeImmutable;
use DateTimeInterface;
use function json_encode;
use function sprintf;

final class AlwaysThrowExceptionOnFailure implements PayloadFailureStrategy
{
/**
* @param string $key
* @param SerializableAttribute[]|string[]|int[]|bool[]|float[] $payload
* @return SerializableAttribute|bool|float|int|string
*/
public function handleKeyNotFound(string $key, array $payload)
{
throw new PayloadKeyNotFound(
Expand All @@ -24,19 +21,42 @@ public function handleKeyNotFound(string $key, array $payload)
);
}

public function handleInvalidStringValue(string $key, $value): string
{
throw UnexpectedTypeForPayloadKey::unexpectedValueForKey($key, $value, 'string');
}

public function handleInvalidIntegerValue(string $key, $value): int
{
throw UnexpectedTypeForPayloadKey::unexpectedValueForKey($key, $value, 'integer');
}

public function handleInvalidFloatValue(string $key, $value): float
{
throw UnexpectedTypeForPayloadKey::unexpectedValueForKey($key, $value, 'float');
}

public function handleInvalidBooleanValue(string $key, $value): bool
{
throw UnexpectedTypeForPayloadKey::unexpectedValueForKey($key, $value, 'boolean');
}

public function handleInvalidDateTimeValue(string $key, $value): DateTimeInterface
{
throw UnexpectedTypeForPayloadKey::unexpectedValueForKey($key, $value, 'datetime');
}

/**
* @param string $value
* @return string
*/
public function transformRawValueToString($value): string
{
Assertion::string($value);
return $value;
return (string) $value;
}

/**
* @param int $value
* @return int
* @param float|int|string $value
*/
public function transformRawValueToInt($value): int
{
Expand All @@ -45,8 +65,7 @@ public function transformRawValueToInt($value): int
}

/**
* @param float $value
* @return float
* @param int|float|string $value
*/
public function transformRawValueToFloat($value): float
{
Expand All @@ -55,8 +74,7 @@ public function transformRawValueToFloat($value): float
}

/**
* @param bool $value
* @return bool
* @param string|int|bool $value
*/
public function transformRawValueToBoolean($value): bool
{
Expand All @@ -65,42 +83,11 @@ public function transformRawValueToBoolean($value): bool
}

/**
* @param string $key
* @param string $value
* @return string
*/
public function handleInvalidStringValue(string $key, $value): string
{
throw UnexpectedTypeForPayloadKey::unexpectedValueForKey($key, $value, 'string');
}

/**
* @param string $key
* @param int $value
* @return int
*/
public function handleInvalidIntegerValue(string $key, $value): int
{
throw UnexpectedTypeForPayloadKey::unexpectedValueForKey($key, $value, 'integer');
}

/**
* @param string $key
* @param float $value
* @return float
*/
public function handleInvalidFloatValue(string $key, $value): float
{
throw UnexpectedTypeForPayloadKey::unexpectedValueForKey($key, $value, 'float');
}

/**
* @param string $key
* @param bool $value
* @return bool
*/
public function handleInvalidBooleanValue(string $key, $value): bool
public function transformRawValueToDateTime($value): DateTimeInterface
{
throw UnexpectedTypeForPayloadKey::unexpectedValueForKey($key, $value, 'boolean');
Assertion::string($value);
return new DateTimeImmutable($value);
}
}
23 changes: 23 additions & 0 deletions src/Serialization/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Star\Component\DomainEvent\Serialization;

use DateTimeInterface;
use function array_key_exists;
use function in_array;
use function is_numeric;
use function is_string;
use function json_decode;

final class Payload
{
Expand Down Expand Up @@ -66,6 +68,17 @@ public function getBoolean(string $key, PayloadFailureStrategy $strategy = null)
return $strategy->transformRawValueToBoolean($value);
}

public function getDateTime(string $key, PayloadFailureStrategy $strategy = null): DateTimeInterface
{
$strategy = $this->assertStrategy($strategy);
$value = $this->getValue($key, $strategy);
if (!is_string($value)) {
return $strategy->handleInvalidDateTimeValue($key, $value);
}

return $strategy->transformRawValueToDateTime($value);
}

private function assertStrategy(PayloadFailureStrategy $strategy = null): PayloadFailureStrategy
{
if (!$strategy) {
Expand Down Expand Up @@ -97,4 +110,14 @@ public static function fromArray(array $payload): self
{
return new self($payload);
}

public static function fromJson(string $json): self
{
/**
* @var string[]|int[]|bool[]|float[] $payload
*/
$payload = json_decode($json, true);

return self::fromArray($payload);
}
}
21 changes: 21 additions & 0 deletions src/Serialization/PayloadFailureStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,58 @@

namespace Star\Component\DomainEvent\Serialization;

use DateTimeInterface;

interface PayloadFailureStrategy
{
/**
* @param string $key
* @param SerializableAttribute[]|string[]|int[]|bool[]|float[] $payload
* @return SerializableAttribute|bool|float|int|string
* @throws PayloadKeyNotFound
*/
public function handleKeyNotFound(string $key, array $payload);

/**
* @param string $key
* @param mixed $value
* @return string
* @throws UnexpectedTypeForPayloadKey
*/
public function handleInvalidStringValue(string $key, $value): string;

/**
* @param string $key
* @param mixed $value
* @return int
* @throws UnexpectedTypeForPayloadKey
*/
public function handleInvalidIntegerValue(string $key, $value): int;

/**
* @param string $key
* @param mixed $value
* @return float
* @throws UnexpectedTypeForPayloadKey
*/
public function handleInvalidFloatValue(string $key, $value): float;

/**
* @param string $key
* @param mixed $value
* @return bool
* @throws UnexpectedTypeForPayloadKey
*/
public function handleInvalidBooleanValue(string $key, $value): bool;

/**
* @param string $key
* @param mixed $value
* @return DateTimeInterface
* @throws UnexpectedTypeForPayloadKey
*/
public function handleInvalidDateTimeValue(string $key, $value): DateTimeInterface;

/**
* @param mixed $value
* @return string
Expand All @@ -62,4 +77,10 @@ public function transformRawValueToFloat($value): float;
* @return bool
*/
public function transformRawValueToBoolean($value): bool;

/**
* @param mixed $value
* @return DateTimeInterface
*/
public function transformRawValueToDateTime($value): DateTimeInterface;
}
79 changes: 34 additions & 45 deletions src/Serialization/ReturnDefaultValueOnFailure.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Star\Component\DomainEvent\Serialization;

use Assert\Assertion;
use DateTimeImmutable;
use DateTimeInterface;
use RuntimeException;

final class ReturnDefaultValueOnFailure implements PayloadFailureStrategy
{
Expand All @@ -19,19 +22,38 @@ public function __construct($value)
$this->value = $value;
}

/**
* @param string $key
* @param SerializableAttribute[]|string[]|int[]|bool[]|float[] $payload
* @return SerializableAttribute|bool|float|int|string
*/
public function handleKeyNotFound(string $key, array $payload)
{
return $this->value;
}

public function handleInvalidStringValue(string $key, $value): string
{
throw new RuntimeException(__METHOD__ . ' not implemented yet.');
}

public function handleInvalidIntegerValue(string $key, $value): int
{
throw new RuntimeException(__METHOD__ . ' not implemented yet.');
}

public function handleInvalidFloatValue(string $key, $value): float
{
throw new RuntimeException(__METHOD__ . ' not implemented yet.');
}

public function handleInvalidBooleanValue(string $key, $value): bool
{
throw new RuntimeException(__METHOD__ . ' not implemented yet.');
}

public function handleInvalidDateTimeValue(string $key, $value): DateTimeInterface
{
throw new RuntimeException(__METHOD__ . ' not implemented yet.');
}

/**
* @param string $value
* @return string
*/
public function transformRawValueToString($value): string
{
Expand All @@ -41,8 +63,7 @@ public function transformRawValueToString($value): string
}

/**
* @param int $value
* @return int
* @param int|float|string $value
*/
public function transformRawValueToInt($value): int
{
Expand All @@ -52,8 +73,7 @@ public function transformRawValueToInt($value): int
}

/**
* @param float $value
* @return float
* @param int|float|string $value
*/
public function transformRawValueToFloat($value): float
{
Expand All @@ -63,8 +83,7 @@ public function transformRawValueToFloat($value): float
}

/**
* @param mixed $value
* @return bool
* @param int|float|string|bool $value
*/
public function transformRawValueToBoolean($value): bool
{
Expand All @@ -74,42 +93,12 @@ public function transformRawValueToBoolean($value): bool
}

/**
* @param string $key
* @param string $value
* @return string
*/
public function handleInvalidStringValue(string $key, $value): string
{
throw new \RuntimeException(__METHOD__ . ' not implemented yet.');
}

/**
* @param string $key
* @param int $value
* @return int
*/
public function handleInvalidIntegerValue(string $key, $value): int
{
throw new \RuntimeException(__METHOD__ . ' not implemented yet.');
}

/**
* @param string $key
* @param float $value
* @return float
*/
public function handleInvalidFloatValue(string $key, $value): float
public function transformRawValueToDateTime($value): DateTimeInterface
{
throw new \RuntimeException(__METHOD__ . ' not implemented yet.');
}
Assertion::string($value);

/**
* @param string $key
* @param bool $value
* @return bool
*/
public function handleInvalidBooleanValue(string $key, $value): bool
{
throw new \RuntimeException(__METHOD__ . ' not implemented yet.');
return new DateTimeImmutable($value);
}
}
Loading

0 comments on commit 9c366b1

Please sign in to comment.