diff --git a/.github/workflows/test-application.yaml b/.github/workflows/test-application.yaml index 5962f1ca..dcc605cc 100644 --- a/.github/workflows/test-application.yaml +++ b/.github/workflows/test-application.yaml @@ -9,7 +9,7 @@ on: jobs: php: - name: 'Run tests with php ${{ matrix.php-version }}' + name: 'Run tests with php ${{ matrix.php-version }} Symfony ${{ matrix.symfony-version }}' runs-on: ubuntu-latest strategy: diff --git a/src/Serializer/Normalizer/CustomReferencedNormalizer.php b/src/Serializer/Normalizer/CustomReferencedNormalizer.php index 0d84dd84..1c628bb9 100644 --- a/src/Serializer/Normalizer/CustomReferencedNormalizer.php +++ b/src/Serializer/Normalizer/CustomReferencedNormalizer.php @@ -11,18 +11,35 @@ namespace ONGR\ElasticsearchDSL\Serializer\Normalizer; -use Symfony\Component\Serializer\Normalizer\CustomNormalizer; +use Symfony\Component\Serializer\Normalizer\DenormalizableInterface; +use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; +use Symfony\Component\Serializer\Normalizer\NormalizableInterface; +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; +use Symfony\Component\Serializer\Normalizer\ObjectToPopulateTrait; +use Symfony\Component\Serializer\SerializerAwareInterface; +use Symfony\Component\Serializer\SerializerAwareTrait; /** * Normalizer used with referenced normalized objects. */ -class CustomReferencedNormalizer extends CustomNormalizer +class CustomReferencedNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface { + use ObjectToPopulateTrait; + use SerializerAwareTrait; + /** * @var array */ private $references = []; + public function getSupportedTypes(?string $format): array + { + return [ + NormalizableInterface::class => true, + DenormalizableInterface::class => true, + ]; + } + /** * {@inheritdoc} */ @@ -32,9 +49,40 @@ public function normalize( array $context = [] ): array|string|int|float|bool|\ArrayObject|null { $object->setReferences($this->references); - $data = parent::normalize($object, $format, $context); + $data = $object->normalize($this->serializer, $format, $context); $this->references = array_merge($this->references, $object->getReferences()); return $data; } + + public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed + { + $object = $this->extractObjectToPopulate($type, $context) ?? new $type(); + $object->denormalize($this->serializer, $data, $format, $context); + + return $object; + } + + /** + * Checks if the given class implements the NormalizableInterface. + * + * @param mixed $data Data to normalize + * @param string|null $format The format being (de-)serialized from or into + */ + public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool + { + return $data instanceof NormalizableInterface; + } + + /** + * Checks if the given class implements the DenormalizableInterface. + * + * @param mixed $data Data to denormalize from + * @param string $type The class to which the data should be denormalized + * @param string|null $format The format being deserialized from + */ + public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool + { + return is_subclass_of($type, DenormalizableInterface::class); + } }