Skip to content

Commit

Permalink
Add missing methods from serializer interface
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelcozzolino committed Oct 15, 2023
1 parent ba830d8 commit d0ca33b
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 6 deletions.
27 changes: 26 additions & 1 deletion src/Contracts/SerializerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,38 @@

namespace WayOfDev\Serializer\Contracts;

use ArrayObject;
use Stringable;
use Symfony\Component\Serializer\Exception\ExceptionInterface;

interface SerializerInterface
{
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;
}
4 changes: 0 additions & 4 deletions src/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,9 +42,6 @@ public function unserialize(
);
}

/**
* @throws ExceptionInterface
*/
public function normalize(
mixed $data,
string $format = null,
Expand Down
52 changes: 51 additions & 1 deletion src/SerializerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace WayOfDev\Serializer;

use ArrayObject;
use Stringable;
use WayOfDev\Serializer\Contracts\SerializerInterface;

Expand Down Expand Up @@ -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);
}
}

0 comments on commit d0ca33b

Please sign in to comment.