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

api-platform / core / 9710836697

28 Jun 2024 09:35AM UTC coverage: 63.285% (+1.2%) from 62.122%
9710836697

push

github

soyuka
docs: changelog v3.3.7

11104 of 17546 relevant lines covered (63.29%)

52.26 hits per line

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

79.17
/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
    }
319✔
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);
156✔
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) {
223✔
80
            return [
196✔
81
                'native-array' => true,
196✔
82
                '\Traversable' => true,
196✔
83
            ];
196✔
84
        }
85

86
        return [];
219✔
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'])) {
192✔
97
            return $this->normalizeRawCollection($object, $format, $context);
68✔
98
        }
99

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

105
        $childContext = $this->createOperationContext($collectionContext, $resourceClass);
128✔
106
        if (isset($collectionContext['force_resource_class'])) {
128✔
107
            $childContext['force_resource_class'] = $collectionContext['force_resource_class'];
×
108
        }
109

110
        $itemsData = $this->getItemsData($object, $format, $childContext);
128✔
111

112
        return array_merge_recursive($data, $paginationData, $itemsData);
124✔
113
    }
114

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

124
        $data = [];
68✔
125
        foreach ($object as $index => $obj) {
68✔
126
            $data[$index] = $this->normalizer->normalize($obj, $format, $context);
64✔
127
        }
128

129
        return $data;
68✔
130
    }
131

132
    /**
133
     * Gets the pagination configuration.
134
     */
135
    protected function getPaginationConfig(iterable $object, array $context = []): array
136
    {
137
        $currentPage = $lastPage = $itemsPerPage = $pageTotalItems = $totalItems = null;
52✔
138
        $paginated = $paginator = false;
52✔
139

140
        if ($object instanceof PartialPaginatorInterface) {
52✔
141
            $paginated = $paginator = true;
32✔
142
            if ($object instanceof PaginatorInterface) {
32✔
143
                $paginated = 1. !== $lastPage = $object->getLastPage();
24✔
144
                $totalItems = $object->getTotalItems();
24✔
145
            } else {
146
                $pageTotalItems = (float) \count($object);
8✔
147
            }
148

149
            $currentPage = $object->getCurrentPage();
32✔
150
            $itemsPerPage = $object->getItemsPerPage();
32✔
151
        } elseif (is_countable($object)) {
20✔
152
            $totalItems = \count($object);
12✔
153
        }
154

155
        return [$paginator, $paginated, $currentPage, $itemsPerPage, $lastPage, $pageTotalItems, $totalItems];
52✔
156
    }
157

158
    protected function getOperation(array $context = []): Operation
159
    {
160
        $metadata = $this->resourceMetadataFactory->create($context['resource_class'] ?? '');
28✔
161

162
        return $metadata->getOperation($context['operation_name'] ?? null);
28✔
163
    }
164

165
    /**
166
     * Gets the pagination data.
167
     */
168
    abstract protected function getPaginationData(iterable $object, array $context = []): array;
169

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