diff --git a/src/Contracts/SerializerInterface.php b/src/Contracts/SerializerInterface.php index f480ba9a..555d340e 100644 --- a/src/Contracts/SerializerInterface.php +++ b/src/Contracts/SerializerInterface.php @@ -4,7 +4,9 @@ namespace WayOfDev\Serializer\Contracts; +use ArrayObject; use Stringable; +use Symfony\Component\Serializer\Exception\ExceptionInterface; interface SerializerInterface { @@ -12,5 +14,28 @@ public function serialize(mixed $payload): string|Stringable; public function unserialize(string|Stringable $payload, string|object $type = null): mixed; - public function normalize(mixed $data, string $format = null, array $context = []); + /** + * @param mixed $data + * @param string|null $format + * @param array $context + * + * @throws ExceptionInterface + * + * @return array|string|int|float|bool|ArrayObject|null + */ + public function normalize(mixed $data, string $format = null, array $context = []): array | string | int | float | bool | ArrayObject | null; + + public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed; + + public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool; + + public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool; + + public function encode(mixed $data, string $format, array $context = []); + + public function decode(string $data, string $format, array $context = []); + + public function supportsEncoding(string $format, array $context = []): bool; + + public function supportsDecoding(string $format, array $context = []): bool; } diff --git a/src/Serializer.php b/src/Serializer.php index 5bf2e2c5..bf224b83 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -6,7 +6,6 @@ use ArrayObject; use Stringable; -use Symfony\Component\Serializer\Exception\ExceptionInterface; use Symfony\Component\Serializer\Serializer as SymfonySerializer; use WayOfDev\Serializer\Contracts\SerializerInterface; use WayOfDev\Serializer\Exceptions\UnsupportedTypeException; @@ -43,9 +42,6 @@ public function unserialize( ); } - /** - * @throws ExceptionInterface - */ public function normalize( mixed $data, string $format = null, diff --git a/src/SerializerManager.php b/src/SerializerManager.php index f614cb0d..a5b541b0 100644 --- a/src/SerializerManager.php +++ b/src/SerializerManager.php @@ -4,6 +4,7 @@ namespace WayOfDev\Serializer; +use ArrayObject; use Stringable; use WayOfDev\Serializer\Contracts\SerializerInterface; @@ -33,8 +34,57 @@ public function unserialize( return $this->getSerializer($format ?? $this->defaultFormat)->unserialize($payload, $type); } - public function normalize(mixed $data, string $format = null, array $context = []) + public function normalize(mixed $data, string $format = null, array $context = []): array | string | int | float | bool | ArrayObject | null { return $this->getSerializer($format ?? $this->defaultFormat)->normalize($data, $format, $context); } + + public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed + { + $format ??= $this->defaultFormat; + + return $this->getSerializer($format)->denormalize($data, $type, $format, $context); + } + + public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool + { + $format ??= $this->defaultFormat; + + return $this->getSerializer($format)->supportsNormalization($data, $format, $context); + } + + public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool + { + $format ??= $this->defaultFormat; + + return $this->getSerializer($format)->supportsDenormalization($data, $type, $format, $context); + } + + public function encode(mixed $data, string $format, array $context = []) + { + $format ??= $this->defaultFormat; + + return $this->getSerializer($format)->encode($data, $format, $context); + } + + public function decode(string $data, string $format, array $context = []) + { + $format ??= $this->defaultFormat; + + return $this->getSerializer($format)->decode($data, $format, $context); + } + + public function supportsEncoding(string $format, array $context = []): bool + { + $format ??= $this->defaultFormat; + + return $this->getSerializer($format)->supportsEncoding($format, $context); + } + + public function supportsDecoding(string $format, array $context = []): bool + { + $format ??= $this->defaultFormat; + + return $this->getSerializer($format)->supportsDecoding($format, $context); + } }