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

api-platform / core / 14635100171

24 Apr 2025 06:39AM UTC coverage: 8.271% (+0.02%) from 8.252%
14635100171

Pull #6904

github

web-flow
Merge c9cefd82e into a3e5e53ea
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

0 of 73 new or added lines in 3 files covered. (0.0%)

1999 existing lines in 144 files now uncovered.

13129 of 158728 relevant lines covered (8.27%)

13.6 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
    {
46
    }
1,166✔
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);
635✔
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) {
1,031✔
64
            return [
834✔
65
                'native-array' => true,
834✔
66
                '\Traversable' => true,
834✔
67
            ];
834✔
68
        }
69

70
        return [];
1,026✔
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'])) {
637✔
81
            return $this->normalizeRawCollection($object, $format, $context);
287✔
82
        }
83

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

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

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

96
        return array_merge_recursive($data, $paginationData, $itemsData);
455✔
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)) {
287✔
UNCOV
105
            return new \ArrayObject();
1✔
106
        }
107

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

113
        return $data;
287✔
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;
68✔
122
        $paginated = $paginator = false;
68✔
123

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

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

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

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

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