• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 5644825543

pending completion
5644825543

push

github

web-flow
feat(serializer): support for getSupportedTypes (symfony 6.3) (#5672)

109 of 109 new or added lines in 29 files covered. (100.0%)

10881 of 18245 relevant lines covered (59.64%)

20.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.42
/src/Hydra/Serializer/PartialCollectionViewNormalizer.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\Hydra\Serializer;
15

16
use ApiPlatform\Metadata\HttpOperation;
17
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
18
use ApiPlatform\State\Pagination\PaginatorInterface;
19
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
20
use ApiPlatform\Util\IriHelper;
21
use Symfony\Component\PropertyAccess\PropertyAccess;
22
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
23
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
24
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
25
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
26
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
27

28
/**
29
 * Adds a view key to the result of a paginated Hydra collection.
30
 *
31
 * @author Kévin Dunglas <dunglas@gmail.com>
32
 * @author Samuel ROZE <samuel.roze@gmail.com>
33
 */
34
final class PartialCollectionViewNormalizer implements NormalizerInterface, NormalizerAwareInterface, CacheableSupportsMethodInterface
35
{
36
    private readonly PropertyAccessorInterface $propertyAccessor;
37

38
    public function __construct(private readonly NormalizerInterface $collectionNormalizer, private readonly string $pageParameterName = 'page', private string $enabledParameterName = 'pagination', private readonly ?ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null, PropertyAccessorInterface $propertyAccessor = null)
39
    {
40
        $this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor();
68✔
41
    }
42

43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
47
    {
48
        $data = $this->collectionNormalizer->normalize($object, $format, $context);
30✔
49

50
        if (isset($context['api_sub_level'])) {
30✔
51
            return $data;
4✔
52
        }
53

54
        if (!\is_array($data)) {
26✔
55
            throw new UnexpectedValueException('Expected data to be an array');
×
56
        }
57

58
        $currentPage = $lastPage = $itemsPerPage = $pageTotalItems = null;
26✔
59
        if ($paginated = ($object instanceof PartialPaginatorInterface)) {
26✔
60
            if ($object instanceof PaginatorInterface) {
20✔
61
                $paginated = 1. !== $lastPage = $object->getLastPage();
18✔
62
            } else {
63
                $itemsPerPage = $object->getItemsPerPage();
2✔
64
                $pageTotalItems = (float) \count($object);
2✔
65
            }
66

67
            $currentPage = $object->getCurrentPage();
20✔
68
        }
69

70
        $parsed = IriHelper::parseIri($context['request_uri'] ?? '/', $this->pageParameterName);
26✔
71
        $appliedFilters = $parsed['parameters'];
26✔
72
        unset($appliedFilters[$this->enabledParameterName]);
26✔
73

74
        if (!$appliedFilters && !$paginated) {
26✔
75
            return $data;
20✔
76
        }
77

78
        $isPaginatedWithCursor = false;
6✔
79
        $cursorPaginationAttribute = null;
6✔
80
        if ($this->resourceMetadataFactory && isset($context['resource_class']) && $paginated) {
6✔
81
            /** @var HttpOperation $operation */
82
            $operation = $this->resourceMetadataFactory->create($context['resource_class'])->getOperation($context['operation_name'] ?? null);
2✔
83
            $isPaginatedWithCursor = [] !== $cursorPaginationAttribute = ($operation->getPaginationViaCursor() ?? []);
2✔
84
        }
85

86
        $data['hydra:view'] = ['@id' => null, '@type' => 'hydra:PartialCollectionView'];
6✔
87

88
        if ($isPaginatedWithCursor) {
6✔
89
            return $this->populateDataWithCursorBasedPagination($data, $parsed, $object, $cursorPaginationAttribute);
2✔
90
        }
91

92
        $data['hydra:view']['@id'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $paginated ? $currentPage : null);
4✔
93

94
        if ($paginated) {
4✔
95
            return $this->populateDataWithPagination($data, $parsed, $currentPage, $lastPage, $itemsPerPage, $pageTotalItems);
4✔
96
        }
97

98
        return $data;
×
99
    }
100

101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
105
    {
106
        return $this->collectionNormalizer->supportsNormalization($data, $format, $context);
22✔
107
    }
108

109
    public function getSupportedTypes($format): array
110
    {
111
        // @deprecated remove condition when support for symfony versions under 6.3 is dropped
112
        if (!method_exists($this->collectionNormalizer, 'getSupportedTypes')) {
28✔
113
            return [
×
114
                '*' => $this->collectionNormalizer instanceof CacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod(),
×
115
            ];
×
116
        }
117

118
        return $this->collectionNormalizer->getSupportedTypes($format);
28✔
119
    }
120

121
    public function hasCacheableSupportsMethod(): bool
122
    {
123
        trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
2✔
124

125
        return $this->collectionNormalizer instanceof CacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod();
2✔
126
    }
127

128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function setNormalizer(NormalizerInterface $normalizer): void
132
    {
133
        if ($this->collectionNormalizer instanceof NormalizerAwareInterface) {
56✔
134
            $this->collectionNormalizer->setNormalizer($normalizer);
56✔
135
        }
136
    }
137

138
    private function cursorPaginationFields(array $fields, int $direction, $object): array
139
    {
140
        $paginationFilters = [];
2✔
141

142
        foreach ($fields as $field) {
2✔
143
            $forwardRangeOperator = 'desc' === strtolower($field['direction']) ? 'lt' : 'gt';
2✔
144
            $backwardRangeOperator = 'gt' === $forwardRangeOperator ? 'lt' : 'gt';
2✔
145

146
            $operator = $direction > 0 ? $forwardRangeOperator : $backwardRangeOperator;
2✔
147

148
            $paginationFilters[$field['field']] = [
2✔
149
                $operator => (string) $this->propertyAccessor->getValue($object, $field['field']),
2✔
150
            ];
2✔
151
        }
152

153
        return $paginationFilters;
2✔
154
    }
155

156
    private function populateDataWithCursorBasedPagination(array $data, array $parsed, \Traversable $object, ?array $cursorPaginationAttribute): array
157
    {
158
        $objects = iterator_to_array($object);
2✔
159
        $firstObject = current($objects);
2✔
160
        $lastObject = end($objects);
2✔
161

162
        $data['hydra:view']['@id'] = IriHelper::createIri($parsed['parts'], $parsed['parameters']);
2✔
163

164
        if (false !== $lastObject && \is_array($cursorPaginationAttribute)) {
2✔
165
            $data['hydra:view']['hydra:next'] = IriHelper::createIri($parsed['parts'], array_merge($parsed['parameters'], $this->cursorPaginationFields($cursorPaginationAttribute, 1, $lastObject)));
2✔
166
        }
167

168
        if (false !== $firstObject && \is_array($cursorPaginationAttribute)) {
2✔
169
            $data['hydra:view']['hydra:previous'] = IriHelper::createIri($parsed['parts'], array_merge($parsed['parameters'], $this->cursorPaginationFields($cursorPaginationAttribute, -1, $firstObject)));
2✔
170
        }
171

172
        return $data;
2✔
173
    }
174

175
    private function populateDataWithPagination(array $data, array $parsed, ?float $currentPage, ?float $lastPage, ?float $itemsPerPage, ?float $pageTotalItems): array
176
    {
177
        if (null !== $lastPage) {
4✔
178
            $data['hydra:view']['hydra:first'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, 1.);
2✔
179
            $data['hydra:view']['hydra:last'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $lastPage);
2✔
180
        }
181

182
        if (1. !== $currentPage) {
4✔
183
            $data['hydra:view']['hydra:previous'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $currentPage - 1.);
4✔
184
        }
185

186
        if ((null !== $lastPage && $currentPage < $lastPage) || (null === $lastPage && $pageTotalItems >= $itemsPerPage)) {
4✔
187
            $data['hydra:view']['hydra:next'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $currentPage + 1.);
4✔
188
        }
189

190
        return $data;
4✔
191
    }
192
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc