Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing methods from serializer interface #192

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Comment on lines +7 to +9
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider removing the unused import Symfony\Component\Serializer\Exception\ExceptionInterface if it is not required in this file.


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);
}
}