• 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

100.0
/src/Serializer/AbstractCollectionNormalizer.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\Serializer;
15

16
use ApiPlatform\Metadata\Operation;
17
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
18
use ApiPlatform\Metadata\ResourceClassResolverInterface;
19
use ApiPlatform\State\Pagination\PaginatorInterface;
20
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
21
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
22
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
23
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
24
use Symfony\Component\Serializer\Serializer;
25

26
/**
27
 * Base collection normalizer.
28
 *
29
 * @author Baptiste Meyer <baptiste.meyer@gmail.com>
30
 */
31
abstract class AbstractCollectionNormalizer implements NormalizerInterface, NormalizerAwareInterface
32
{
33
    use ContextTrait {
34
        initContext as protected;
35
    }
36
    use NormalizerAwareTrait;
37
    use OperationContextTrait;
38

39
    /**
40
     * This constant must be overridden in the child class.
41
     */
42
    public const FORMAT = 'to-override';
43

44
    public function __construct(protected ResourceClassResolverInterface $resourceClassResolver, protected string $pageParameterName, protected ?ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null)
45
    {
UNCOV
46
    }
953✔
47

48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
52
    {
UNCOV
53
        return static::FORMAT === $format && is_iterable($data);
508✔
54
    }
55

56
    public function getSupportedTypes(?string $format): array
57
    {
58
        /*
59
         * At this point, support anything that is_iterable(), i.e. array|Traversable
60
         * for non-objects, symfony uses 'native-'.\gettype($data) :
61
         * https://github.com/tucksaun/symfony/blob/400685a68b00b0932f8ef41096578872b643099c/src/Symfony/Component/Serializer/Serializer.php#L254
62
         */
UNCOV
63
        if (static::FORMAT === $format) {
844✔
UNCOV
64
            return [
673✔
UNCOV
65
                'native-array' => true,
673✔
UNCOV
66
                '\Traversable' => true,
673✔
UNCOV
67
            ];
673✔
68
        }
69

UNCOV
70
        return [];
840✔
71
    }
72

73
    /**
74
     * {@inheritdoc}
75
     *
76
     * @param iterable $object
77
     */
78
    public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
79
    {
UNCOV
80
        if (!isset($context['resource_class']) || isset($context['api_sub_level'])) {
509✔
UNCOV
81
            return $this->normalizeRawCollection($object, $format, $context);
272✔
82
        }
83

UNCOV
84
        $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class']);
342✔
UNCOV
85
        $collectionContext = $this->initContext($resourceClass, $context);
342✔
UNCOV
86
        $data = [];
342✔
UNCOV
87
        $paginationData = $this->getPaginationData($object, $collectionContext);
342✔
88

UNCOV
89
        $childContext = $this->createOperationContext($collectionContext, $resourceClass);
342✔
UNCOV
90
        if (isset($collectionContext['force_resource_class'])) {
342✔
UNCOV
91
            $childContext['force_resource_class'] = $collectionContext['force_resource_class'];
4✔
92
        }
93

UNCOV
94
        $itemsData = $this->getItemsData($object, $format, $childContext);
342✔
95

UNCOV
96
        return array_merge_recursive($data, $paginationData, $itemsData);
342✔
97
    }
98

99
    /**
100
     * Normalizes a raw collection (not API resources).
101
     */
102
    protected function normalizeRawCollection(iterable $object, ?string $format = null, array $context = []): array|\ArrayObject
103
    {
UNCOV
104
        if (!$object && ($context[Serializer::EMPTY_ARRAY_AS_OBJECT] ?? false) && \is_array($object)) {
272✔
105
            return new \ArrayObject();
1✔
106
        }
107

UNCOV
108
        $data = [];
272✔
UNCOV
109
        foreach ($object as $index => $obj) {
272✔
UNCOV
110
            $data[$index] = $this->normalizer->normalize($obj, $format, $context);
162✔
111
        }
112

UNCOV
113
        return $data;
272✔
114
    }
115

116
    /**
117
     * Gets the pagination configuration.
118
     */
119
    protected function getPaginationConfig(iterable $object, array $context = []): array
120
    {
UNCOV
121
        $currentPage = $lastPage = $itemsPerPage = $pageTotalItems = $totalItems = null;
56✔
UNCOV
122
        $paginated = $paginator = false;
56✔
123

UNCOV
124
        if ($object instanceof PartialPaginatorInterface) {
56✔
UNCOV
125
            $paginated = $paginator = true;
45✔
UNCOV
126
            if ($object instanceof PaginatorInterface) {
45✔
UNCOV
127
                $paginated = 1. !== $lastPage = $object->getLastPage();
44✔
UNCOV
128
                $totalItems = $object->getTotalItems();
44✔
129
            } else {
UNCOV
130
                $pageTotalItems = (float) \count($object);
1✔
131
            }
132

UNCOV
133
            $currentPage = $object->getCurrentPage();
45✔
UNCOV
134
            $itemsPerPage = $object->getItemsPerPage();
45✔
UNCOV
135
        } elseif (is_countable($object)) {
11✔
UNCOV
136
            $totalItems = \count($object);
9✔
137
        }
138

UNCOV
139
        return [$paginator, $paginated, $currentPage, $itemsPerPage, $lastPage, $pageTotalItems, $totalItems];
56✔
140
    }
141

142
    protected function getOperation(array $context = []): Operation
143
    {
UNCOV
144
        $metadata = $this->resourceMetadataFactory->create($context['resource_class'] ?? '');
2✔
145

UNCOV
146
        return $metadata->getOperation($context['operation_name'] ?? null);
2✔
147
    }
148

149
    /**
150
     * Gets the pagination data.
151
     */
152
    abstract protected function getPaginationData(iterable $object, array $context = []): array;
153

154
    /**
155
     * Gets items data.
156
     */
157
    abstract protected function getItemsData(iterable $object, ?string $format = null, array $context = []): array;
158
}
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