Skip to content

Commit

Permalink
Copy CustomNormalizer to CustomReferencedNormalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed Jul 16, 2024
1 parent e899a98 commit 7c7f516
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
54 changes: 51 additions & 3 deletions src/Serializer/Normalizer/CustomReferencedNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand All @@ -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);
}
}

0 comments on commit 7c7f516

Please sign in to comment.