Skip to content

Commit

Permalink
Add Support for Symfony 7 (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz authored Jul 16, 2024
1 parent c68676d commit 51cb254
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 26 deletions.
31 changes: 22 additions & 9 deletions .github/workflows/test-application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -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 }}
Expand Down
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
60 changes: 56 additions & 4 deletions src/Serializer/Normalizer/CustomReferencedNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
13 changes: 9 additions & 4 deletions src/Serializer/OrderedSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -104,15 +109,15 @@ 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);
}

/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
public function supportsNormalization($data, $format = null, array $context = []): bool
{
return $this->serializer->supportsNormalization($data, $format);
}
Expand Down

0 comments on commit 51cb254

Please sign in to comment.