diff --git a/.github/workflows/test-application.yaml b/.github/workflows/test-application.yaml index 0e882996..1a920cae 100644 --- a/.github/workflows/test-application.yaml +++ b/.github/workflows/test-application.yaml @@ -9,17 +9,13 @@ 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: fail-fast: false matrix: include: - - php-version: '8.0' - lint: false - symfony-version: '^2.8' - - php-version: '8.0' lint: false symfony-version: '^3.4' @@ -29,21 +25,38 @@ jobs: symfony-version: '^4.4' - php-version: '8.0' - lint: true + lint: false symfony-version: '^5.0' - php-version: '8.1' - lint: true + lint: false symfony-version: '^5.4' - php-version: '8.0' - lint: true + lint: false symfony-version: '^6.0' - php-version: '8.1' + lint: false + symfony-version: '^6.0' + + - php-version: '8.2' lint: true symfony-version: '^6.0' + - php-version: '8.2' + lint: false + symfony-version: '^7.0' + + - php-version: '8.3' + lint: false + symfony-version: '^7.0' + + - php-version: '8.4' + lint: false + symfony-version: '^7.0' + composer-options: '--ignore-platform-reqs' + services: elasticsearch: image: elasticsearch:7.5.2 @@ -81,7 +94,7 @@ jobs: run: | composer validate --strict composer require --no-update symfony/symfony:${{ matrix.symfony-version }} - composer install --no-interaction --prefer-dist + composer update --no-interaction --prefer-dist ${{ matrix.compser-options }} - name: Fix code style if: ${{ matrix.lint }} diff --git a/README.md b/README.md index c5b21453..f2467d64 100644 --- a/README.md +++ b/README.md @@ -3,13 +3,14 @@ This is a fork of the [ongr/elasticsearch-dsl](https://github.com/ongr-io/ElasticsearchDSL). With some basic changes to support wider range of Symfony Versions. -| Version | Supported Elasticsearch Version | Supported Symfony Version | -|---------|---------------------------------|---------------------------------| -| 7.x | ^7.0 | ^6.0, ^5.0, ^4.0, ^3.4, ^2.8 | -| 6.x | ^6.0 | ^5.0, ^4.0, ^3.4, ^2.8 | -| 5.x | ^5.0 | ^5.0, ^4.0, ^3.4, ^2.8 | -| 2.x | ^2.0 | ^3.0, ^2.7 | -| 1.x | ^1.0 | ^3.0, ^2.7 | +| Version | Supported Elasticsearch Version | Supported Symfony Version | +|---------|---------------------------------|--------------------------------| +| 7.x | ^7.0 | ^7.0, ^6.0, ^5.0, ^4.0, ^3.4 | +| 7.3 | ^7.0 | ^6.0, ^5.0, ^4.0, ^3.4, ^2.8 | +| 6.x | ^6.0 | ^5.0, ^4.0, ^3.4, ^2.8 | +| 5.x | ^5.0 | ^5.0, ^4.0, ^3.4, ^2.8 | +| 2.x | ^2.0 | ^3.0, ^2.7 | +| 1.x | ^1.0 | ^3.0, ^2.7 | ## Documentation diff --git a/composer.json b/composer.json index 340706fe..94b6aba9 100644 --- a/composer.json +++ b/composer.json @@ -17,11 +17,11 @@ "require": { "ext-json": "*", "php": "^8.0", - "symfony/serializer": "^2.8 || ^3.4 || ^4.0 || ^5.0 || ^6.0", + "symfony/serializer": "^3.4 || ^4.0 || ^5.0 || ^6.0 || ^7.0", "elasticsearch/elasticsearch": "^7.0" }, "require-dev": { - "phpunit/phpunit": "^5.7.26 || ^7.5.20 || ^8.0", + "phpunit/phpunit": "^9.6.20", "squizlabs/php_codesniffer": "^2.0 || ^3.0" }, "suggest": { diff --git a/src/Serializer/Normalizer/CustomReferencedNormalizer.php b/src/Serializer/Normalizer/CustomReferencedNormalizer.php index 0d84dd84..4d3d8001 100644 --- a/src/Serializer/Normalizer/CustomReferencedNormalizer.php +++ b/src/Serializer/Normalizer/CustomReferencedNormalizer.php @@ -11,30 +11,82 @@ 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. + * + * Most parts are copy of + * https://github.com/symfony/symfony/blob/7.2/src/Symfony/Component/Serializer/Normalizer/CustomNormalizer.php + * as @final in Symfony 7. */ -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} */ public function normalize( - mixed $object, + $object, $format = null, 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($data, $type, $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($data, $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($data, $type, $format = null, array $context = []): bool + { + return is_subclass_of($type, DenormalizableInterface::class); + } } diff --git a/src/Serializer/OrderedSerializer.php b/src/Serializer/OrderedSerializer.php index fdb41b9e..97d3383e 100644 --- a/src/Serializer/OrderedSerializer.php +++ b/src/Serializer/OrderedSerializer.php @@ -34,10 +34,15 @@ public function __construct(array $normalizers = []) $this->serializer = new Serializer($normalizers); } + public function getSupportedTypes(?string $format): array + { + return ['*' => true]; + } + /** * {@inheritdoc} */ - public function normalize($data, $format = null, array $context = []) + public function normalize($data, $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null { return $this->serializer->normalize( is_array($data) ? $this->order($data) : $data, @@ -49,7 +54,7 @@ public function normalize($data, $format = null, array $context = []) /** * {@inheritdoc} */ - public function denormalize($data, $type, $format = null, array $context = []) + public function denormalize($data, $type, $format = null, array $context = []): mixed { return $this->serializer->denormalize( is_array($data) ? $this->order($data) : $data, @@ -104,7 +109,7 @@ function ($value) { /** * {@inheritdoc} */ - public function supportsDenormalization($data, $type, $format = null) + public function supportsDenormalization($data, $type, $format = null, array $context = []): bool { return $this->serializer->supportsDenormalization($data, $format); } @@ -112,7 +117,7 @@ public function supportsDenormalization($data, $type, $format = null) /** * {@inheritdoc} */ - public function supportsNormalization($data, $format = null) + public function supportsNormalization($data, $format = null, array $context = []): bool { return $this->serializer->supportsNormalization($data, $format); }