| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Serializer\Normalizer; |
| 13 |
|
| 14 |
use Symfony\Component\Serializer\Exception\InvalidArgumentException; |
| 15 |
use Symfony\Component\Serializer\Exception\LogicException; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
final class JsonSerializableNormalizer extends AbstractNormalizer |
| 23 |
{ |
| 24 |
public function normalize(mixed $data, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null |
| 25 |
{ |
| 26 |
if ($this->isCircularReference($data, $context)) { |
| 27 |
return $this->handleCircularReference($data, $format, $context); |
| 28 |
} |
| 29 |
|
| 30 |
if (!$data instanceof \JsonSerializable) { |
| 31 |
throw new InvalidArgumentException(\sprintf('The object must implement "%s".', \JsonSerializable::class)); |
| 32 |
} |
| 33 |
|
| 34 |
if (!$this->serializer instanceof NormalizerInterface) { |
| 35 |
throw new LogicException('Cannot normalize object because injected serializer is not a normalizer.'); |
| 36 |
} |
| 37 |
|
| 38 |
return $this->serializer->normalize($data->jsonSerialize(), $format, $context); |
| 39 |
} |
| 40 |
|
| 41 |
public function getSupportedTypes(?string $format): array |
| 42 |
{ |
| 43 |
return [ |
| 44 |
\JsonSerializable::class => true, |
| 45 |
]; |
| 46 |
} |
| 47 |
|
| 48 |
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool |
| 49 |
{ |
| 50 |
return $data instanceof \JsonSerializable; |
| 51 |
} |
| 52 |
|
| 53 |
public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool |
| 54 |
{ |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed |
| 59 |
{ |
| 60 |
throw new LogicException(\sprintf('Cannot denormalize with "%s".', \JsonSerializable::class)); |
| 61 |
} |
| 62 |
} |