• 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

80.43
/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\Api\ResourceClassResolverInterface as LegacyResourceClassResolverInterface;
17
use ApiPlatform\Metadata\Operation;
18
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
19
use ApiPlatform\Metadata\ResourceClassResolverInterface;
20
use ApiPlatform\State\Pagination\PaginatorInterface;
21
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
22
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
23
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
24
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
25
use Symfony\Component\Serializer\Serializer;
26

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

40
    /**
41
     * This constant must be overridden in the child class.
42
     */
43
    // @noRector \Rector\Php81\Rector\ClassConst\FinalizePublicClassConstantRector
44
    public const FORMAT = 'to-override';
45

46
    public function __construct(protected ResourceClassResolverInterface|LegacyResourceClassResolverInterface $resourceClassResolver, protected string $pageParameterName, protected ?ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null)
47
    {
48
    }
176✔
49

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

58
    public function hasCacheableSupportsMethod(): bool
59
    {
60
        if (method_exists(Serializer::class, 'getSupportedTypes')) {
×
61
            trigger_deprecation(
×
62
                'api-platform/core',
×
63
                '3.1',
×
64
                'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
×
65
                __METHOD__
×
66
            );
×
67
        }
68

69
        return true;
×
70
    }
71

72
    public function getSupportedTypes(?string $format): array
73
    {
74
        /*
75
         * At this point, support anything that is_iterable(), i.e. array|Traversable
76
         * for non-objects, symfony uses 'native-'.\gettype($data) :
77
         * https://github.com/tucksaun/symfony/blob/400685a68b00b0932f8ef41096578872b643099c/src/Symfony/Component/Serializer/Serializer.php#L254
78
         */
79
        if (static::FORMAT === $format) {
80✔
80
            return [
56✔
81
                'native-array' => true,
56✔
82
                '\Traversable' => true,
56✔
83
            ];
56✔
84
        }
85

86
        return [];
80✔
87
    }
88

89
    /**
90
     * {@inheritdoc}
91
     *
92
     * @param iterable $object
93
     */
94
    public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
95
    {
96
        if (!isset($context['resource_class']) || isset($context['api_sub_level'])) {
88✔
97
            return $this->normalizeRawCollection($object, $format, $context);
8✔
98
        }
99

100
        $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class']);
80✔
101
        $collectionContext = $this->initContext($resourceClass, $context);
80✔
102
        $data = [];
80✔
103
        $paginationData = $this->getPaginationData($object, $collectionContext);
80✔
104

105
        $childContext = $this->createOperationContext($collectionContext, $resourceClass);
80✔
106
        $itemsData = $this->getItemsData($object, $format, $childContext);
80✔
107

108
        return array_merge_recursive($data, $paginationData, $itemsData);
76✔
109
    }
110

111
    /**
112
     * Normalizes a raw collection (not API resources).
113
     */
114
    protected function normalizeRawCollection(iterable $object, string $format = null, array $context = []): array|\ArrayObject
115
    {
116
        if (!$object && ($context[Serializer::EMPTY_ARRAY_AS_OBJECT] ?? false) && \is_array($object)) {
8✔
117
            return new \ArrayObject();
×
118
        }
119

120
        $data = [];
8✔
121
        foreach ($object as $index => $obj) {
8✔
122
            $data[$index] = $this->normalizer->normalize($obj, $format, $context);
4✔
123
        }
124

125
        return $data;
8✔
126
    }
127

128
    /**
129
     * Gets the pagination configuration.
130
     */
131
    protected function getPaginationConfig(iterable $object, array $context = []): array
132
    {
133
        $currentPage = $lastPage = $itemsPerPage = $pageTotalItems = $totalItems = null;
28✔
134
        $paginated = $paginator = false;
28✔
135

136
        if ($object instanceof PartialPaginatorInterface) {
28✔
137
            $paginated = $paginator = true;
16✔
138
            if ($object instanceof PaginatorInterface) {
16✔
139
                $paginated = 1. !== $lastPage = $object->getLastPage();
8✔
140
                $totalItems = $object->getTotalItems();
8✔
141
            } else {
142
                $pageTotalItems = (float) \count($object);
8✔
143
            }
144

145
            $currentPage = $object->getCurrentPage();
16✔
146
            $itemsPerPage = $object->getItemsPerPage();
16✔
147
        } elseif (is_countable($object)) {
12✔
148
            $totalItems = \count($object);
12✔
149
        }
150

151
        return [$paginator, $paginated, $currentPage, $itemsPerPage, $lastPage, $pageTotalItems, $totalItems];
28✔
152
    }
153

154
    protected function getOperation(array $context = []): Operation
155
    {
156
        $metadata = $this->resourceMetadataFactory->create($context['resource_class'] ?? '');
28✔
157

158
        return $metadata->getOperation($context['operation_name'] ?? null);
28✔
159
    }
160

161
    /**
162
     * Gets the pagination data.
163
     */
164
    abstract protected function getPaginationData(iterable $object, array $context = []): array;
165

166
    /**
167
     * Gets items data.
168
     */
169
    abstract protected function getItemsData(iterable $object, string $format = null, array $context = []): array;
170
}
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