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

api-platform / core / 7196499749

13 Dec 2023 02:17PM UTC coverage: 37.359% (+1.4%) from 36.003%
7196499749

push

github

web-flow
ci: conflict sebastian/comparator (#6032)

* ci: conflict sebastian/comparator

* for lowest

10295 of 27557 relevant lines covered (37.36%)

28.14 hits per line

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

81.94
/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\Serializer\CacheableSupportsMethodInterface;
19
use ApiPlatform\State\Pagination\PaginatorInterface;
20
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
21
use ApiPlatform\Util\IriHelper;
22
use Symfony\Component\PropertyAccess\PropertyAccess;
23
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
24
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
25
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
26
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
27
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
28
use Symfony\Component\Serializer\Serializer;
29

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

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

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

52
        if (isset($context['api_sub_level'])) {
60✔
53
            return $data;
8✔
54
        }
55

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

60
        $currentPage = $lastPage = $itemsPerPage = $pageTotalItems = null;
52✔
61
        if ($paginated = ($object instanceof PartialPaginatorInterface)) {
52✔
62
            if ($object instanceof PaginatorInterface) {
40✔
63
                $paginated = 1. !== $lastPage = $object->getLastPage();
36✔
64
            } else {
65
                $itemsPerPage = $object->getItemsPerPage();
4✔
66
                $pageTotalItems = (float) \count($object);
4✔
67
            }
68

69
            $currentPage = $object->getCurrentPage();
40✔
70
        }
71

72
        // TODO: This needs to be changed as well as I wrote in the CollectionFiltersNormalizer
73
        // We should not rely on the request_uri but instead rely on the UriTemplate
74
        // This needs that we implement the RFC and that we do more parsing before calling the serialization (MainController)
75
        $parsed = IriHelper::parseIri($context['request_uri'] ?? '/', $this->pageParameterName);
52✔
76
        $appliedFilters = $parsed['parameters'];
52✔
77
        unset($appliedFilters[$this->enabledParameterName]);
52✔
78

79
        if (!$appliedFilters && !$paginated) {
52✔
80
            return $data;
40✔
81
        }
82

83
        $isPaginatedWithCursor = false;
12✔
84
        $cursorPaginationAttribute = null;
12✔
85
        if ($this->resourceMetadataFactory && isset($context['resource_class']) && $paginated) {
12✔
86
            /** @var HttpOperation $operation */
87
            $operation = $this->resourceMetadataFactory->create($context['resource_class'])->getOperation($context['operation_name'] ?? null);
4✔
88
            $isPaginatedWithCursor = [] !== $cursorPaginationAttribute = ($operation->getPaginationViaCursor() ?? []);
4✔
89
        }
90

91
        $data['hydra:view'] = ['@id' => null, '@type' => 'hydra:PartialCollectionView'];
12✔
92

93
        if ($isPaginatedWithCursor) {
12✔
94
            return $this->populateDataWithCursorBasedPagination($data, $parsed, $object, $cursorPaginationAttribute);
4✔
95
        }
96

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

99
        if ($paginated) {
8✔
100
            return $this->populateDataWithPagination($data, $parsed, $currentPage, $lastPage, $itemsPerPage, $pageTotalItems);
8✔
101
        }
102

103
        return $data;
×
104
    }
105

106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
110
    {
111
        return $this->collectionNormalizer->supportsNormalization($data, $format, $context);
44✔
112
    }
113

114
    public function getSupportedTypes($format): array
115
    {
116
        // @deprecated remove condition when support for symfony versions under 6.3 is dropped
117
        if (!method_exists($this->collectionNormalizer, 'getSupportedTypes')) {
72✔
118
            return [
×
119
                '*' => $this->collectionNormalizer instanceof BaseCacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod(),
×
120
            ];
×
121
        }
122

123
        return $this->collectionNormalizer->getSupportedTypes($format);
72✔
124
    }
125

126
    public function hasCacheableSupportsMethod(): bool
127
    {
128
        if (method_exists(Serializer::class, 'getSupportedTypes')) {
×
129
            trigger_deprecation(
×
130
                'api-platform/core',
×
131
                '3.1',
×
132
                'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
×
133
                __METHOD__
×
134
            );
×
135
        }
136

137
        return $this->collectionNormalizer instanceof BaseCacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod();
×
138
    }
139

140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function setNormalizer(NormalizerInterface $normalizer): void
144
    {
145
        if ($this->collectionNormalizer instanceof NormalizerAwareInterface) {
120✔
146
            $this->collectionNormalizer->setNormalizer($normalizer);
120✔
147
        }
148
    }
149

150
    private function cursorPaginationFields(array $fields, int $direction, $object): array
151
    {
152
        $paginationFilters = [];
4✔
153

154
        foreach ($fields as $field) {
4✔
155
            $forwardRangeOperator = 'desc' === strtolower($field['direction']) ? 'lt' : 'gt';
4✔
156
            $backwardRangeOperator = 'gt' === $forwardRangeOperator ? 'lt' : 'gt';
4✔
157

158
            $operator = $direction > 0 ? $forwardRangeOperator : $backwardRangeOperator;
4✔
159

160
            $paginationFilters[$field['field']] = [
4✔
161
                $operator => (string) $this->propertyAccessor->getValue($object, $field['field']),
4✔
162
            ];
4✔
163
        }
164

165
        return $paginationFilters;
4✔
166
    }
167

168
    private function populateDataWithCursorBasedPagination(array $data, array $parsed, \Traversable $object, ?array $cursorPaginationAttribute): array
169
    {
170
        $objects = iterator_to_array($object);
4✔
171
        $firstObject = current($objects);
4✔
172
        $lastObject = end($objects);
4✔
173

174
        $data['hydra:view']['@id'] = IriHelper::createIri($parsed['parts'], $parsed['parameters']);
4✔
175

176
        if (false !== $lastObject && \is_array($cursorPaginationAttribute)) {
4✔
177
            $data['hydra:view']['hydra:next'] = IriHelper::createIri($parsed['parts'], array_merge($parsed['parameters'], $this->cursorPaginationFields($cursorPaginationAttribute, 1, $lastObject)));
4✔
178
        }
179

180
        if (false !== $firstObject && \is_array($cursorPaginationAttribute)) {
4✔
181
            $data['hydra:view']['hydra:previous'] = IriHelper::createIri($parsed['parts'], array_merge($parsed['parameters'], $this->cursorPaginationFields($cursorPaginationAttribute, -1, $firstObject)));
4✔
182
        }
183

184
        return $data;
4✔
185
    }
186

187
    private function populateDataWithPagination(array $data, array $parsed, ?float $currentPage, ?float $lastPage, ?float $itemsPerPage, ?float $pageTotalItems): array
188
    {
189
        if (null !== $lastPage) {
8✔
190
            $data['hydra:view']['hydra:first'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, 1.);
4✔
191
            $data['hydra:view']['hydra:last'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $lastPage);
4✔
192
        }
193

194
        if (1. !== $currentPage) {
8✔
195
            $data['hydra:view']['hydra:previous'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $currentPage - 1.);
8✔
196
        }
197

198
        if ((null !== $lastPage && $currentPage < $lastPage) || (null === $lastPage && $pageTotalItems >= $itemsPerPage)) {
8✔
199
            $data['hydra:view']['hydra:next'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $currentPage + 1.);
8✔
200
        }
201

202
        return $data;
8✔
203
    }
204
}
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