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

api-platform / core / 3712739783

pending completion
3712739783

Pull #5254

github

GitHub
Merge 9dfa88fa6 into ac711530f
Pull Request #5254: [OpenApi] Add ApiResource::openapi and deprecate openapiContext

199 of 199 new or added lines in 6 files covered. (100.0%)

7494 of 12363 relevant lines covered (60.62%)

67.55 hits per line

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

92.11
/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;
17
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
18
use ApiPlatform\State\Pagination\PaginatorInterface;
19
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
20
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
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, CacheableSupportsMethodInterface
32
{
33
    use ContextTrait {
34
        initContext as protected;
35
    }
36
    use NormalizerAwareTrait;
37

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

44
    public function __construct(protected ResourceClassResolverInterface $resourceClassResolver, protected string $pageParameterName, protected ?ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null)
45
    {
46
    }
649✔
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);
519✔
54
    }
55

56
    public function hasCacheableSupportsMethod(): bool
57
    {
58
        return true;
519✔
59
    }
60

61
    /**
62
     * {@inheritdoc}
63
     *
64
     * @param iterable $object
65
     */
66
    public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
67
    {
68
        if (!isset($context['resource_class']) || isset($context['api_sub_level'])) {
55✔
69
            return $this->normalizeRawCollection($object, $format, $context);
35✔
70
        }
71

72
        $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class']);
40✔
73
        $context = $this->initContext($resourceClass, $context);
40✔
74
        $data = [];
40✔
75
        $paginationData = $this->getPaginationData($object, $context);
40✔
76

77
        if (($operation = $context['operation'] ?? null) && method_exists($operation, 'getItemUriTemplate')) {
40✔
78
            $context['item_uri_template'] = $operation->getItemUriTemplate();
40✔
79
        }
80

81
        // We need to keep this operation for serialization groups for later
82
        if (isset($context['operation'])) {
40✔
83
            $context['root_operation'] = $context['operation'];
40✔
84
        }
85

86
        if (isset($context['operation_name'])) {
40✔
87
            $context['root_operation_name'] = $context['operation_name'];
40✔
88
        }
89

90
        unset($context['operation']);
40✔
91
        unset($context['operation_type'], $context['operation_name']);
40✔
92
        $itemsData = $this->getItemsData($object, $format, $context);
40✔
93

94
        return array_merge_recursive($data, $paginationData, $itemsData);
40✔
95
    }
96

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

106
        $data = [];
35✔
107
        foreach ($object as $index => $obj) {
35✔
108
            $data[$index] = $this->normalizer->normalize($obj, $format, $context);
7✔
109
        }
110

111
        return $data;
35✔
112
    }
113

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

122
        if ($object instanceof PartialPaginatorInterface) {
40✔
123
            $paginated = $paginator = true;
39✔
124
            if ($object instanceof PaginatorInterface) {
39✔
125
                $paginated = 1. !== $lastPage = $object->getLastPage();
39✔
126
                $totalItems = $object->getTotalItems();
39✔
127
            } else {
128
                $pageTotalItems = (float) \count($object);
×
129
            }
130

131
            $currentPage = $object->getCurrentPage();
39✔
132
            $itemsPerPage = $object->getItemsPerPage();
39✔
133
        } elseif (is_countable($object)) {
1✔
134
            $totalItems = \count($object);
×
135
        }
136

137
        return [$paginator, $paginated, $currentPage, $itemsPerPage, $lastPage, $pageTotalItems, $totalItems];
40✔
138
    }
139

140
    /**
141
     * Gets the pagination data.
142
     */
143
    abstract protected function getPaginationData(iterable $object, array $context = []): array;
144

145
    /**
146
     * Gets items data.
147
     */
148
    abstract protected function getItemsData(iterable $object, string $format = null, array $context = []): array;
149
}
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