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

api-platform / core / 10537652610

24 Aug 2024 10:04AM UTC coverage: 7.707%. Remained the same
10537652610

push

github

dunglas
cleanup

12490 of 162060 relevant lines covered (7.71%)

22.98 hits per line

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

95.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
    {
46
    }
2,255✔
47

48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
52
    {
53
        return static::FORMAT === $format && is_iterable($data);
1,134✔
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
         */
63
        if (static::FORMAT === $format) {
2,021✔
64
            return [
1,571✔
65
                'native-array' => true,
1,571✔
66
                '\Traversable' => true,
1,571✔
67
            ];
1,571✔
68
        }
69

70
        return [];
2,012✔
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
    {
80
        if (!isset($context['resource_class']) || isset($context['api_sub_level'])) {
1,134✔
81
            return $this->normalizeRawCollection($object, $format, $context);
774✔
82
        }
83

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

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

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

96
        return array_merge_recursive($data, $paginationData, $itemsData);
684✔
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
    {
104
        if (!$object && ($context[Serializer::EMPTY_ARRAY_AS_OBJECT] ?? false) && \is_array($object)) {
774✔
105
            return new \ArrayObject();
3✔
106
        }
107

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

113
        return $data;
774✔
114
    }
115

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

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

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

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

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

146
        return $metadata->getOperation($context['operation_name'] ?? null);
×
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