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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

0 of 2 new or added lines in 1 file covered. (0.0%)

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

93.65
/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\JsonLd\Serializer\HydraPrefixTrait;
17
use ApiPlatform\Metadata\HttpOperation;
18
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
19
use ApiPlatform\Metadata\UrlGeneratorInterface;
20
use ApiPlatform\Metadata\Util\IriHelper;
21
use ApiPlatform\State\Pagination\PaginatorInterface;
22
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
23
use Symfony\Component\PropertyAccess\PropertyAccess;
24
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
25
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
26
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
27
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
28

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

40
    /**
41
     * @param array<string, mixed> $defaultContext
42
     */
43
    public function __construct(private readonly NormalizerInterface $collectionNormalizer, private readonly string $pageParameterName = 'page', private string $enabledParameterName = 'pagination', private readonly ?ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null, ?PropertyAccessorInterface $propertyAccessor = null, private readonly int $urlGenerationStrategy = UrlGeneratorInterface::ABS_PATH, private readonly array $defaultContext = [])
44
    {
UNCOV
45
        $this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor();
950✔
46
    }
47

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

UNCOV
55
        if (isset($context['api_sub_level'])) {
433✔
UNCOV
56
            return $data;
232✔
57
        }
58

UNCOV
59
        if (!\is_array($data)) {
286✔
60
            throw new UnexpectedValueException('Expected data to be an array');
×
61
        }
62

UNCOV
63
        $currentPage = $lastPage = $itemsPerPage = $pageTotalItems = null;
286✔
UNCOV
64
        if ($paginated = ($object instanceof PartialPaginatorInterface)) {
286✔
UNCOV
65
            if ($object instanceof PaginatorInterface) {
257✔
UNCOV
66
                $paginated = 1. !== $lastPage = $object->getLastPage();
257✔
67
            } else {
68
                $itemsPerPage = $object->getItemsPerPage();
×
69
                $pageTotalItems = (float) \count($object);
×
70
            }
71

UNCOV
72
            $currentPage = $object->getCurrentPage();
257✔
73
        }
74

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

UNCOV
82
        if (!$appliedFilters && !$paginated) {
286✔
UNCOV
83
            return $data;
77✔
84
        }
85

UNCOV
86
        $isPaginatedWithCursor = false;
209✔
UNCOV
87
        $cursorPaginationAttribute = null;
209✔
UNCOV
88
        $operation = $context['operation'] ?? null;
209✔
UNCOV
89
        if (!$operation && $this->resourceMetadataFactory && isset($context['resource_class']) && $paginated) {
209✔
90
            $operation = $this->resourceMetadataFactory->create($context['resource_class'])->getOperation($context['operation_name'] ?? null);
×
91
        }
92

UNCOV
93
        $cursorPaginationAttribute = $operation instanceof HttpOperation ? $operation->getPaginationViaCursor() : null;
209✔
UNCOV
94
        $isPaginatedWithCursor = (bool) $cursorPaginationAttribute;
209✔
95

UNCOV
96
        $hydraPrefix = $this->getHydraPrefix($context + $this->defaultContext);
209✔
UNCOV
97
        $data[$hydraPrefix.'view'] = ['@id' => null, '@type' => $hydraPrefix.'PartialCollectionView'];
209✔
98

UNCOV
99
        if ($isPaginatedWithCursor) {
209✔
100
            return $this->populateDataWithCursorBasedPagination($data, $parsed, $object, $cursorPaginationAttribute, $operation?->getUrlGenerationStrategy() ?? $this->urlGenerationStrategy, $hydraPrefix);
2✔
101
        }
102

UNCOV
103
        $data[$hydraPrefix.'view']['@id'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $paginated ? $currentPage : null, $operation?->getUrlGenerationStrategy() ?? $this->urlGenerationStrategy);
207✔
104

UNCOV
105
        if ($paginated) {
207✔
UNCOV
106
            return $this->populateDataWithPagination($data, $parsed, $currentPage, $lastPage, $itemsPerPage, $pageTotalItems, $operation?->getUrlGenerationStrategy() ?? $this->urlGenerationStrategy, $hydraPrefix);
71✔
107
        }
108

UNCOV
109
        return $data;
139✔
110
    }
111

112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
116
    {
UNCOV
117
        return $this->collectionNormalizer->supportsNormalization($data, $format, $context);
433✔
118
    }
119

120
    public function getSupportedTypes($format): array
121
    {
UNCOV
122
        return $this->collectionNormalizer->getSupportedTypes($format);
839✔
123
    }
124

125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function setNormalizer(NormalizerInterface $normalizer): void
129
    {
UNCOV
130
        if ($this->collectionNormalizer instanceof NormalizerAwareInterface) {
950✔
UNCOV
131
            $this->collectionNormalizer->setNormalizer($normalizer);
950✔
132
        }
133
    }
134

135
    private function cursorPaginationFields(array $fields, int $direction, $object): array
136
    {
137
        $paginationFilters = [];
1✔
138

139
        foreach ($fields as $field) {
1✔
140
            $forwardRangeOperator = 'desc' === strtolower($field['direction']) ? 'lt' : 'gt';
1✔
141
            $backwardRangeOperator = 'gt' === $forwardRangeOperator ? 'lt' : 'gt';
1✔
142

143
            $operator = $direction > 0 ? $forwardRangeOperator : $backwardRangeOperator;
1✔
144

145
            $paginationFilters[$field['field']] = [
1✔
146
                $operator => (string) $this->propertyAccessor->getValue($object, $field['field']),
1✔
147
            ];
1✔
148
        }
149

150
        return $paginationFilters;
1✔
151
    }
152

153
    private function populateDataWithCursorBasedPagination(array $data, array $parsed, \Traversable $object, ?array $cursorPaginationAttribute, ?int $urlGenerationStrategy, string $hydraPrefix): array
154
    {
155
        $objects = iterator_to_array($object);
2✔
156
        $firstObject = current($objects);
2✔
157
        $lastObject = end($objects);
2✔
158

159
        $data[$hydraPrefix.'view']['@id'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], urlGenerationStrategy: $urlGenerationStrategy);
2✔
160

161
        if (false !== $lastObject && \is_array($cursorPaginationAttribute)) {
2✔
162
            $data[$hydraPrefix.'view'][$hydraPrefix.'next'] = IriHelper::createIri($parsed['parts'], array_merge($parsed['parameters'], $this->cursorPaginationFields($cursorPaginationAttribute, 1, $lastObject)), urlGenerationStrategy: $urlGenerationStrategy);
1✔
163
        }
164

165
        if (false !== $firstObject && \is_array($cursorPaginationAttribute)) {
2✔
166
            $data[$hydraPrefix.'view'][$hydraPrefix.'previous'] = IriHelper::createIri($parsed['parts'], array_merge($parsed['parameters'], $this->cursorPaginationFields($cursorPaginationAttribute, -1, $firstObject)), urlGenerationStrategy: $urlGenerationStrategy);
1✔
167
        }
168

169
        return $data;
2✔
170
    }
171

172
    private function populateDataWithPagination(array $data, array $parsed, ?float $currentPage, ?float $lastPage, ?float $itemsPerPage, ?float $pageTotalItems, ?int $urlGenerationStrategy, string $hydraPrefix): array
173
    {
UNCOV
174
        if (null !== $lastPage) {
71✔
UNCOV
175
            $data[$hydraPrefix.'view'][$hydraPrefix.'first'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, 1., $urlGenerationStrategy);
71✔
UNCOV
176
            $data[$hydraPrefix.'view'][$hydraPrefix.'last'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $lastPage, $urlGenerationStrategy);
71✔
177
        }
178

UNCOV
179
        if (1. !== $currentPage) {
71✔
180
            $data[$hydraPrefix.'view'][$hydraPrefix.'previous'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $currentPage - 1., $urlGenerationStrategy);
5✔
181
        }
182

UNCOV
183
        if ((null !== $lastPage && $currentPage < $lastPage) || (null === $lastPage && $pageTotalItems >= $itemsPerPage)) {
71✔
UNCOV
184
            $data[$hydraPrefix.'view'][$hydraPrefix.'next'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $currentPage + 1., $urlGenerationStrategy);
70✔
185
        }
186

UNCOV
187
        return $data;
71✔
188
    }
189
}
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

© 2025 Coveralls, Inc