|
19 | 19 | use ApiPlatform\JsonLd\ContextBuilderInterface;
|
20 | 20 | use ApiPlatform\JsonLd\Serializer\JsonLdContextTrait;
|
21 | 21 | use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
|
22 |
| -use ApiPlatform\Serializer\ContextTrait; |
| 22 | +use ApiPlatform\Serializer\AbstractCollectionNormalizer; |
23 | 23 | use ApiPlatform\State\Pagination\PaginatorInterface;
|
24 | 24 | use ApiPlatform\State\Pagination\PartialPaginatorInterface;
|
25 |
| -use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; |
26 |
| -use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; |
27 |
| -use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; |
28 |
| -use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
29 |
| -use Symfony\Component\Serializer\Serializer; |
30 | 25 |
|
31 | 26 | /**
|
32 | 27 | * This normalizer handles collections.
|
33 | 28 | *
|
34 | 29 | * @author Kevin Dunglas <[email protected]>
|
35 | 30 | * @author Samuel ROZE <[email protected]>
|
36 | 31 | */
|
37 |
| -final class CollectionNormalizer implements NormalizerInterface, NormalizerAwareInterface, CacheableSupportsMethodInterface |
| 32 | +final class CollectionNormalizer extends AbstractCollectionNormalizer |
38 | 33 | {
|
39 |
| - use ContextTrait; |
40 | 34 | use JsonLdContextTrait;
|
41 |
| - use NormalizerAwareTrait; |
42 | 35 |
|
43 | 36 | public const FORMAT = 'jsonld';
|
44 | 37 | public const IRI_ONLY = 'iri_only';
|
45 | 38 | private array $defaultContext = [
|
46 | 39 | self::IRI_ONLY => false,
|
47 | 40 | ];
|
48 | 41 |
|
49 |
| - public function __construct(private readonly ContextBuilderInterface $contextBuilder, private readonly ResourceClassResolverInterface $resourceClassResolver, private readonly IriConverterInterface $iriConverter, private readonly ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null, array $defaultContext = []) |
| 42 | + public function __construct(private readonly ContextBuilderInterface $contextBuilder, ResourceClassResolverInterface $resourceClassResolver, private readonly IriConverterInterface $iriConverter, private readonly ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null, array $defaultContext = []) |
50 | 43 | {
|
51 | 44 | $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
|
52 | 45 |
|
53 | 46 | if ($this->resourceMetadataCollectionFactory) {
|
54 | 47 | trigger_deprecation('api-platform/core', '3.0', sprintf('Injecting "%s" within "%s" is not needed anymore and this dependency will be removed in 4.0.', ResourceMetadataCollectionFactoryInterface::class, self::class));
|
55 | 48 | }
|
56 |
| - } |
57 | 49 |
|
58 |
| - /** |
59 |
| - * {@inheritdoc} |
60 |
| - */ |
61 |
| - public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool |
62 |
| - { |
63 |
| - return self::FORMAT === $format && is_iterable($data); |
| 50 | + parent::__construct($resourceClassResolver, ''); |
64 | 51 | }
|
65 | 52 |
|
66 | 53 | /**
|
67 |
| - * {@inheritdoc} |
68 |
| - * |
69 |
| - * @param iterable $object |
| 54 | + * Gets the pagination data. |
70 | 55 | */
|
71 |
| - public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null |
| 56 | + protected function getPaginationData(iterable $object, array $context = []): array |
72 | 57 | {
|
73 |
| - if (!isset($context['resource_class']) || isset($context['api_sub_level'])) { |
74 |
| - return $this->normalizeRawCollection($object, $format, $context); |
75 |
| - } |
76 |
| - |
77 | 58 | $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class']);
|
78 | 59 | $context = $this->initContext($resourceClass, $context);
|
79 | 60 | $context['api_collection_sub_level'] = true;
|
80 | 61 | $data = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
|
81 | 62 | $data['@id'] = $this->iriConverter->getIriFromResource($resourceClass, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context);
|
82 | 63 | $data['@type'] = 'hydra:Collection';
|
| 64 | + |
| 65 | + if ($object instanceof PaginatorInterface) { |
| 66 | + $data['hydra:totalItems'] = $object->getTotalItems(); |
| 67 | + } |
| 68 | + |
| 69 | + if (\is_array($object) || ($object instanceof \Countable && !$object instanceof PartialPaginatorInterface)) { |
| 70 | + $data['hydra:totalItems'] = \count($object); |
| 71 | + } |
| 72 | + |
| 73 | + return $data; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Gets items data. |
| 78 | + */ |
| 79 | + protected function getItemsData(iterable $object, string $format = null, array $context = []): array |
| 80 | + { |
| 81 | + $data = []; |
83 | 82 | $data['hydra:member'] = [];
|
| 83 | + |
84 | 84 | $iriOnly = $context[self::IRI_ONLY] ?? $this->defaultContext[self::IRI_ONLY];
|
85 | 85 |
|
86 | 86 | if (($operation = $context['operation'] ?? null) && method_exists($operation, 'getItemUriTemplate')) {
|
@@ -108,36 +108,6 @@ public function normalize(mixed $object, string $format = null, array $context =
|
108 | 108 | }
|
109 | 109 | }
|
110 | 110 |
|
111 |
| - if ($object instanceof PaginatorInterface) { |
112 |
| - $data['hydra:totalItems'] = $object->getTotalItems(); |
113 |
| - } |
114 |
| - |
115 |
| - if (\is_array($object) || ($object instanceof \Countable && !$object instanceof PartialPaginatorInterface)) { |
116 |
| - $data['hydra:totalItems'] = \count($object); |
117 |
| - } |
118 |
| - |
119 |
| - return $data; |
120 |
| - } |
121 |
| - |
122 |
| - public function hasCacheableSupportsMethod(): bool |
123 |
| - { |
124 |
| - return true; |
125 |
| - } |
126 |
| - |
127 |
| - /** |
128 |
| - * Normalizes a raw collection (not API resources). |
129 |
| - */ |
130 |
| - protected function normalizeRawCollection(iterable $object, string $format = null, array $context = []): array|\ArrayObject |
131 |
| - { |
132 |
| - if (\is_array($object) && !$object && ($context[Serializer::EMPTY_ARRAY_AS_OBJECT] ?? false)) { |
133 |
| - return new \ArrayObject(); |
134 |
| - } |
135 |
| - |
136 |
| - $data = []; |
137 |
| - foreach ($object as $index => $obj) { |
138 |
| - $data[$index] = $this->normalizer->normalize($obj, $format, $context); |
139 |
| - } |
140 |
| - |
141 | 111 | return $data;
|
142 | 112 | }
|
143 | 113 | }
|
0 commit comments