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

api-platform / core / 20847864477

09 Jan 2026 09:47AM UTC coverage: 29.1% (+0.005%) from 29.095%
20847864477

Pull #7649

github

web-flow
Merge b342dd5db into d640d106b
Pull Request #7649: feat(validator): uuid/ulid parameter validation

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

15050 existing lines in 491 files now uncovered.

16996 of 58406 relevant lines covered (29.1%)

81.8 hits per line

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

96.72
/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\Hydra\State\Util\PaginationHelperTrait;
17
use ApiPlatform\JsonLd\Serializer\HydraPrefixTrait;
18
use ApiPlatform\Metadata\HttpOperation;
19
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
20
use ApiPlatform\Metadata\UrlGeneratorInterface;
21
use ApiPlatform\Metadata\Util\IriHelper;
22
use ApiPlatform\State\Pagination\PaginatorInterface;
23
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
24
use Symfony\Component\PropertyAccess\PropertyAccess;
25
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
26
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
27
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
28
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
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
37
{
38
    use HydraPrefixTrait;
39
    use PaginationHelperTrait;
40
    private readonly PropertyAccessorInterface $propertyAccessor;
41

42
    /**
43
     * @param array<string, mixed> $defaultContext
44
     */
45
    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 = [])
46
    {
UNCOV
47
        $this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor();
2,409✔
48
    }
49

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

UNCOV
57
        if (isset($context['api_sub_level'])) {
1,134✔
UNCOV
58
            return $normalizedData;
617✔
59
        }
60

UNCOV
61
        if (!\is_array($normalizedData)) {
705✔
62
            throw new UnexpectedValueException('Expected data to be an array');
×
63
        }
64

UNCOV
65
        $paginated = $data instanceof PartialPaginatorInterface;
705✔
UNCOV
66
        if ($paginated && $data instanceof PaginatorInterface) {
705✔
UNCOV
67
            $paginated = 1. !== $data->getLastPage();
615✔
68
        }
69

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

UNCOV
74
        if (!$appliedFilters && !$paginated) {
705✔
UNCOV
75
            return $normalizedData;
192✔
76
        }
77

UNCOV
78
        $isPaginatedWithCursor = false;
513✔
UNCOV
79
        $cursorPaginationAttribute = null;
513✔
UNCOV
80
        $operation = $context['operation'] ?? null;
513✔
UNCOV
81
        if (!$operation && $this->resourceMetadataFactory && isset($context['resource_class']) && $paginated) {
513✔
82
            $operation = $this->resourceMetadataFactory->create($context['resource_class'])->getOperation($context['operation_name'] ?? null);
×
83
        }
84

UNCOV
85
        $cursorPaginationAttribute = $operation instanceof HttpOperation ? $operation->getPaginationViaCursor() : null;
513✔
UNCOV
86
        $isPaginatedWithCursor = (bool) $cursorPaginationAttribute;
513✔
87

UNCOV
88
        $hydraPrefix = $this->getHydraPrefix($context + $this->defaultContext);
513✔
89

UNCOV
90
        if ($isPaginatedWithCursor) {
513✔
91
            $normalizedData[$hydraPrefix.'view'] = ['@id' => null, '@type' => $hydraPrefix.'PartialCollectionView'];
6✔
92

93
            return $this->populateDataWithCursorBasedPagination($normalizedData, $parsed, $data, $cursorPaginationAttribute, $operation?->getUrlGenerationStrategy() ?? $this->urlGenerationStrategy, $hydraPrefix);
6✔
94
        }
95

UNCOV
96
        $partialCollectionView = $this->getPartialCollectionView($data, $context['uri'] ?? $context['request_uri'] ?? '/', $this->pageParameterName, $this->enabledParameterName, $operation?->getUrlGenerationStrategy() ?? $this->urlGenerationStrategy);
507✔
97

UNCOV
98
        $view = [
507✔
UNCOV
99
            '@id' => $partialCollectionView->id,
507✔
UNCOV
100
            '@type' => $hydraPrefix.'PartialCollectionView',
507✔
UNCOV
101
        ];
507✔
102

UNCOV
103
        if (null !== $partialCollectionView->first) {
507✔
UNCOV
104
            $view[$hydraPrefix.'first'] = $partialCollectionView->first;
148✔
UNCOV
105
            $view[$hydraPrefix.'last'] = $partialCollectionView->last;
148✔
106
        }
107

UNCOV
108
        if (null !== $partialCollectionView->previous) {
507✔
109
            $view[$hydraPrefix.'previous'] = $partialCollectionView->previous;
12✔
110
        }
111

UNCOV
112
        if (null !== $partialCollectionView->next) {
507✔
UNCOV
113
            $view[$hydraPrefix.'next'] = $partialCollectionView->next;
147✔
114
        }
115

UNCOV
116
        $normalizedData[$hydraPrefix.'view'] = $view;
507✔
117

UNCOV
118
        return $normalizedData;
507✔
119
    }
120

121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
125
    {
UNCOV
126
        return $this->collectionNormalizer->supportsNormalization($data, $format, $context);
1,134✔
127
    }
128

129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function getSupportedTypes(?string $format): array
133
    {
UNCOV
134
        return $this->collectionNormalizer->getSupportedTypes($format);
2,078✔
135
    }
136

137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function setNormalizer(NormalizerInterface $normalizer): void
141
    {
UNCOV
142
        if ($this->collectionNormalizer instanceof NormalizerAwareInterface) {
2,409✔
UNCOV
143
            $this->collectionNormalizer->setNormalizer($normalizer);
2,409✔
144
        }
145
    }
146

147
    /**
148
     * @param object $object
149
     */
150
    private function cursorPaginationFields(array $fields, int $direction, $object): array
151
    {
152
        $paginationFilters = [];
2✔
153

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

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

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

165
        return $paginationFilters;
2✔
166
    }
167

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

174
        $data[$hydraPrefix.'view']['@id'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], urlGenerationStrategy: $urlGenerationStrategy);
6✔
175

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

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

184
        return $data;
6✔
185
    }
186
}
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