From 3a11bffa83a39f3dcb3a10d139a9b74872315363 Mon Sep 17 00:00:00 2001 From: Moln Date: Fri, 6 Dec 2024 17:06:56 +0800 Subject: [PATCH] Upgrade symfony/serializer v7 --- composer.json | 4 +- src/Basic/ArrayNormalizer.php | 22 +++++----- src/Basic/CollectionNormalizer.php | 15 ++++--- src/Basic/CursorPaginationNormalizer.php | 10 ++++- .../AbstractCollectionNormalizer.php | 42 +++++++------------ 5 files changed, 47 insertions(+), 46 deletions(-) diff --git a/composer.json b/composer.json index 1492c97..a432ca7 100644 --- a/composer.json +++ b/composer.json @@ -21,8 +21,8 @@ } }, "require": { - "php": ">=7.4", - "symfony/serializer": "^5.2|^6.0" + "php": ">=8.1", + "symfony/serializer": "^7.0" }, "extra": { "laminas": { diff --git a/src/Basic/ArrayNormalizer.php b/src/Basic/ArrayNormalizer.php index f5a0bda..b69316b 100644 --- a/src/Basic/ArrayNormalizer.php +++ b/src/Basic/ArrayNormalizer.php @@ -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; @@ -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) { @@ -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]; @@ -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, + ]; + } } diff --git a/src/Basic/CollectionNormalizer.php b/src/Basic/CollectionNormalizer.php index f3099ce..546be78 100644 --- a/src/Basic/CollectionNormalizer.php +++ b/src/Basic/CollectionNormalizer.php @@ -11,11 +11,6 @@ final class CollectionNormalizer extends AbstractCollectionNormalizer { public const FORMAT = 'json'; - public function hasCacheableSupportsMethod(): bool - { - return false; - } - /** * {@inheritdoc} */ @@ -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' => [], @@ -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, + ]; + } } diff --git a/src/Basic/CursorPaginationNormalizer.php b/src/Basic/CursorPaginationNormalizer.php index 1e7a4c5..ba60c9b 100644 --- a/src/Basic/CursorPaginationNormalizer.php +++ b/src/Basic/CursorPaginationNormalizer.php @@ -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); } @@ -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, + ]; + } } \ No newline at end of file diff --git a/src/Serializer/AbstractCollectionNormalizer.php b/src/Serializer/AbstractCollectionNormalizer.php index e7ba8c5..61b89d0 100644 --- a/src/Serializer/AbstractCollectionNormalizer.php +++ b/src/Serializer/AbstractCollectionNormalizer.php @@ -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 */ -abstract class AbstractCollectionNormalizer implements NormalizerInterface, NormalizerAwareInterface, CacheableSupportsMethodInterface +abstract class AbstractCollectionNormalizer implements NormalizerInterface, NormalizerAwareInterface { use NormalizerAwareTrait; @@ -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); @@ -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) { @@ -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(); @@ -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,