Skip to content

Commit d0ca33b

Browse files
Add missing methods from serializer interface
1 parent ba830d8 commit d0ca33b

File tree

3 files changed

+77
-6
lines changed

3 files changed

+77
-6
lines changed

src/Contracts/SerializerInterface.php

+26-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,38 @@
44

55
namespace WayOfDev\Serializer\Contracts;
66

7+
use ArrayObject;
78
use Stringable;
9+
use Symfony\Component\Serializer\Exception\ExceptionInterface;
810

911
interface SerializerInterface
1012
{
1113
public function serialize(mixed $payload): string|Stringable;
1214

1315
public function unserialize(string|Stringable $payload, string|object $type = null): mixed;
1416

15-
public function normalize(mixed $data, string $format = null, array $context = []);
17+
/**
18+
* @param mixed $data
19+
* @param string|null $format
20+
* @param array $context
21+
*
22+
* @throws ExceptionInterface
23+
*
24+
* @return array|string|int|float|bool|ArrayObject|null
25+
*/
26+
public function normalize(mixed $data, string $format = null, array $context = []): array | string | int | float | bool | ArrayObject | null;
27+
28+
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed;
29+
30+
public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool;
31+
32+
public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool;
33+
34+
public function encode(mixed $data, string $format, array $context = []);
35+
36+
public function decode(string $data, string $format, array $context = []);
37+
38+
public function supportsEncoding(string $format, array $context = []): bool;
39+
40+
public function supportsDecoding(string $format, array $context = []): bool;
1641
}

src/Serializer.php

-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use ArrayObject;
88
use Stringable;
9-
use Symfony\Component\Serializer\Exception\ExceptionInterface;
109
use Symfony\Component\Serializer\Serializer as SymfonySerializer;
1110
use WayOfDev\Serializer\Contracts\SerializerInterface;
1211
use WayOfDev\Serializer\Exceptions\UnsupportedTypeException;
@@ -43,9 +42,6 @@ public function unserialize(
4342
);
4443
}
4544

46-
/**
47-
* @throws ExceptionInterface
48-
*/
4945
public function normalize(
5046
mixed $data,
5147
string $format = null,

src/SerializerManager.php

+51-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace WayOfDev\Serializer;
66

7+
use ArrayObject;
78
use Stringable;
89
use WayOfDev\Serializer\Contracts\SerializerInterface;
910

@@ -33,8 +34,57 @@ public function unserialize(
3334
return $this->getSerializer($format ?? $this->defaultFormat)->unserialize($payload, $type);
3435
}
3536

36-
public function normalize(mixed $data, string $format = null, array $context = [])
37+
public function normalize(mixed $data, string $format = null, array $context = []): array | string | int | float | bool | ArrayObject | null
3738
{
3839
return $this->getSerializer($format ?? $this->defaultFormat)->normalize($data, $format, $context);
3940
}
41+
42+
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed
43+
{
44+
$format ??= $this->defaultFormat;
45+
46+
return $this->getSerializer($format)->denormalize($data, $type, $format, $context);
47+
}
48+
49+
public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
50+
{
51+
$format ??= $this->defaultFormat;
52+
53+
return $this->getSerializer($format)->supportsNormalization($data, $format, $context);
54+
}
55+
56+
public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool
57+
{
58+
$format ??= $this->defaultFormat;
59+
60+
return $this->getSerializer($format)->supportsDenormalization($data, $type, $format, $context);
61+
}
62+
63+
public function encode(mixed $data, string $format, array $context = [])
64+
{
65+
$format ??= $this->defaultFormat;
66+
67+
return $this->getSerializer($format)->encode($data, $format, $context);
68+
}
69+
70+
public function decode(string $data, string $format, array $context = [])
71+
{
72+
$format ??= $this->defaultFormat;
73+
74+
return $this->getSerializer($format)->decode($data, $format, $context);
75+
}
76+
77+
public function supportsEncoding(string $format, array $context = []): bool
78+
{
79+
$format ??= $this->defaultFormat;
80+
81+
return $this->getSerializer($format)->supportsEncoding($format, $context);
82+
}
83+
84+
public function supportsDecoding(string $format, array $context = []): bool
85+
{
86+
$format ??= $this->defaultFormat;
87+
88+
return $this->getSerializer($format)->supportsDecoding($format, $context);
89+
}
4090
}

0 commit comments

Comments
 (0)