Skip to content

Commit

Permalink
Upgrade symfony/serializer v7
Browse files Browse the repository at this point in the history
  • Loading branch information
Moln committed Dec 6, 2024
1 parent a133d0a commit 3a11bff
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 46 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
}
},
"require": {
"php": ">=7.4",
"symfony/serializer": "^5.2|^6.0"
"php": ">=8.1",
"symfony/serializer": "^7.0"
},
"extra": {
"laminas": {
Expand Down
22 changes: 12 additions & 10 deletions src/Basic/ArrayNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\SerializerAwareInterface;
use Symfony\Component\Serializer\SerializerAwareTrait;

final class ArrayNormalizer implements NormalizerInterface, SerializerAwareInterface, CacheableSupportsMethodInterface
final class ArrayNormalizer implements NormalizerInterface, SerializerAwareInterface
{
use SerializerAwareTrait;

Expand Down Expand Up @@ -48,15 +47,15 @@ public function __construct(NameConverterInterface $nameConverter = null, array
}
}

public function supportsNormalization($data, string $format = null)
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
return (is_array($data) && count($data) > 0 && ! is_int(key($data))) || $data instanceof \ArrayObject;
return (is_array($data) && array_is_list($data)) || $data instanceof \ArrayObject;
}

/**
* {@inheritdoc}
*/
public function normalize($object, string $format = null, array $context = [])
public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
{
$data = [];
foreach ($object as $attribute => $attributeValue) {
Expand Down Expand Up @@ -99,11 +98,6 @@ private function createChildContext(array $parentContext, string $attribute, ?st
return $parentContext;
}

public function hasCacheableSupportsMethod(): bool
{
return false;
}

private function isAllowedAttribute(string $attribute, array $context = []): bool
{
$ignoredAttributes = $context[AbstractNormalizer::IGNORED_ATTRIBUTES] ?? $this->defaultContext[AbstractNormalizer::IGNORED_ATTRIBUTES];
Expand All @@ -123,4 +117,12 @@ private function isAllowedAttribute(string $attribute, array $context = []): boo

return true;
}

public function getSupportedTypes(?string $format): array
{
return [
\ArrayObject::class => true,
'native-array' => true,
];
}
}
15 changes: 9 additions & 6 deletions src/Basic/CollectionNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ final class CollectionNormalizer extends AbstractCollectionNormalizer
{
public const FORMAT = 'json';

public function hasCacheableSupportsMethod(): bool
{
return false;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -45,7 +40,7 @@ protected function getPaginationData($object, array $context = []): array
*
* @throws UnexpectedValueException
*/
protected function getItemsData($object, string $format = null, array $context = []): array
protected function getItemsData(iterable $object, string $format = null, array $context = []): array
{
$data = [
'data' => [],
Expand All @@ -62,4 +57,12 @@ protected function getItemsData($object, string $format = null, array $context =

return $data;
}

public function getSupportedTypes(?string $format): array
{
return [
\Traversable::class => true,
'native-array' => true,
];
}
}
10 changes: 9 additions & 1 deletion src/Basic/CursorPaginationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CursorPaginationNormalizer extends AbstractCollectionNormalizer
{
public const FORMAT = 'json';

public function supportsNormalization($data, $format = null, array $context = [])
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
return $data instanceof CursorPaginatorInterface && parent::supportsNormalization($data, $format, $context);
}
Expand Down Expand Up @@ -53,4 +53,12 @@ protected function getItemsData($object, string $format = null, array $context =

return $data;
}

public function getSupportedTypes(?string $format): array
{
return [
\Traversable::class => true,
'native-array' => true,
];
}
}
42 changes: 15 additions & 27 deletions src/Serializer/AbstractCollectionNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);
declare(strict_types = 1);

namespace Zfegg\ApiSerializerExt\Serializer;

use Zfegg\ApiSerializerExt\Paginator\OffsetPaginatorInterface;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use function count;
use function is_countable;

/**
* Base collection normalizer.
*
* @author Baptiste Meyer <baptiste.meyer@gmail.com>
*/
abstract class AbstractCollectionNormalizer implements NormalizerInterface, NormalizerAwareInterface, CacheableSupportsMethodInterface
abstract class AbstractCollectionNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;

Expand All @@ -33,39 +34,30 @@ abstract class AbstractCollectionNormalizer implements NormalizerInterface, Norm
*/
public const FORMAT = 'to-override';

protected $pageParameterName;

public function __construct(string $pageParameterName = 'page')
public function __construct(protected string $pageParameterName = 'page')
{
$this->pageParameterName = $pageParameterName;
}

/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null, array $context = [])
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
return static::FORMAT === $format &&
is_iterable($data) &&
isset($context['api_resource']) &&
$context['api_resource'] == 'collection' &&
!isset($context['api_sub_level']);
! isset($context['api_sub_level']);
}

/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
{
return true;
}

/**
* {@inheritdoc}
*
* @param iterable $object
*/
public function normalize($object, string $format = null, array $context = [])
public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
{
if (isset($context['api_sub_level'])) {
return $this->normalizeRawCollection($object, $format, $context);
Expand All @@ -84,9 +76,8 @@ public function normalize($object, string $format = null, array $context = [])
/**
* Normalizes a raw collection (not API resources).
*
* @param string|null $format
*/
protected function normalizeRawCollection($object, $format = null, array $context = []): array
protected function normalizeRawCollection($object, ?string $format = null, array $context = []): array
{
$data = [];
foreach ($object as $index => $obj) {
Expand All @@ -99,14 +90,13 @@ protected function normalizeRawCollection($object, $format = null, array $contex
/**
* Gets the pagination configuration.
*
* @param iterable $object
*/
protected function getPaginationConfig($object, array $context = []): array
protected function getPaginationConfig(iterable $object, array $context = []): array
{
$currentPage = $itemsPerPage = $totalItems = $pageCount = null;

if (\is_array($object) || $object instanceof \Countable) {
$totalItems = \count($object);
if (is_countable($object)) {
$totalItems = count($object);
}
if ($object instanceof OffsetPaginatorInterface) {
$currentPage = $object->getCurrentPage();
Expand All @@ -120,18 +110,16 @@ protected function getPaginationConfig($object, array $context = []): array
/**
* Gets the pagination data.
*
* @param iterable $object
*/
abstract protected function getPaginationData($object, array $context = []): array;
abstract protected function getPaginationData(iterable $object, array $context = []): array;

/**
* Gets items data.
*
* @param iterable $object
*/
abstract protected function getItemsData($object, string $format = null, array $context = []): array;
abstract protected function getItemsData(iterable $object, ?string $format = null, array $context = []): array;

private function initContext()
private function initContext(): array
{
return [
'api_sub_level' => true,
Expand Down

0 comments on commit 3a11bff

Please sign in to comment.