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

api-platform / core / 6690762601

30 Oct 2023 09:24AM UTC coverage: 67.319% (+0.02%) from 67.301%
6690762601

push

github

web-flow
ci: fix PHPUNIT (#5907)

* ci: fix phpunit


???

* Unset handler_id for symfony 6.3+

* Fix serializer configuration for PHP 8.1 (dev)

* Fix https://github.com/api-platform/api-platform/issues/2437

* Fix excepted deprecation in swagger


Trigger deprecation to fit tests. Can change test if needed


forgot semicolon


try fix deprecation

* remove copied WebTestCase to fix 8.1 dev

PR https://github.com/symfony/symfony/pull/32207 got merged

* fix no deprecation

* try tag legacy to valide


add a bc layer for reworked profiler UI

* fix warning about deprecated method


ensure method exists

* skip an exceptDeprecation, this case fails for a particular CI run

* remove uneccesary changes

* change BC deprecation system for doctrine

* fix some deprecations about validation html mode and attributes for recent sf

* fix doctrine lexer deprecations

* fix bootstrap missing

* improve doc for sf 6


f


Fix tiny bug & deprecation


--

* fix possible deprecation on 7.4

* Update ci.yml

* Update ci.yml

15610 of 23188 relevant lines covered (67.32%)

10.87 hits per line

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

96.88
/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\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
18
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
19
use ApiPlatform\Metadata\CollectionOperationInterface;
20
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
21
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
22
use ApiPlatform\State\Pagination\PaginatorInterface;
23
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
24
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
25
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
26
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
27
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
28

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

42
    /**
43
     * This constant must be overridden in the child class.
44
     */
45
    public const FORMAT = 'to-override';
46

47
    protected $resourceClassResolver;
48
    protected $pageParameterName;
49

50
    /**
51
     * @var ResourceMetadataCollectionFactoryInterface|ResourceMetadataFactoryInterface
52
     */
53
    protected $resourceMetadataFactory;
54

55
    public function __construct(ResourceClassResolverInterface $resourceClassResolver, string $pageParameterName, $resourceMetadataFactory = null)
56
    {
57
        $this->resourceClassResolver = $resourceClassResolver;
36✔
58
        $this->pageParameterName = $pageParameterName;
36✔
59
        $this->resourceMetadataFactory = $resourceMetadataFactory;
36✔
60
    }
61

62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function supportsNormalization($data, $format = null, array $context = []): bool
66
    {
67
        return static::FORMAT === $format && is_iterable($data);
15✔
68
    }
69

70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function hasCacheableSupportsMethod(): bool
74
    {
75
        return true;
15✔
76
    }
77

78
    /**
79
     * {@inheritdoc}
80
     *
81
     * @param iterable $object
82
     *
83
     * @return array|string|int|float|bool|\ArrayObject|null
84
     */
85
    public function normalize($object, $format = null, array $context = [])
86
    {
87
        if (!isset($context['resource_class']) || isset($context['api_sub_level'])) {
8✔
88
            return $this->normalizeRawCollection($object, $format, $context);
1✔
89
        }
90

91
        $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class']);
7✔
92
        $collectionContext = $this->initContext($resourceClass, $context);
7✔
93
        $data = [];
7✔
94
        $paginationData = $this->getPaginationData($object, $collectionContext);
7✔
95

96
        $childContext = $this->createOperationContext($collectionContext, $resourceClass);
7✔
97
        $itemsData = $this->getItemsData($object, $format, $childContext);
7✔
98

99
        return array_merge_recursive($data, $paginationData, $itemsData);
6✔
100
    }
101

102
    /**
103
     * Normalizes a raw collection (not API resources).
104
     *
105
     * @param string|null $format
106
     * @param mixed       $object
107
     */
108
    protected function normalizeRawCollection($object, $format = null, array $context = []): array
109
    {
110
        $data = [];
1✔
111
        foreach ($object as $index => $obj) {
1✔
112
            $data[$index] = $this->normalizer->normalize($obj, $format, $context);
1✔
113
        }
114

115
        return $data;
1✔
116
    }
117

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

128
        if ($object instanceof PartialPaginatorInterface) {
7✔
129
            $paginated = $paginator = true;
4✔
130
            if ($object instanceof PaginatorInterface) {
4✔
131
                $paginated = 1. !== $lastPage = $object->getLastPage();
2✔
132
                $totalItems = $object->getTotalItems();
2✔
133
            } else {
134
                $pageTotalItems = (float) \count($object);
2✔
135
            }
136

137
            $currentPage = $object->getCurrentPage();
4✔
138
            $itemsPerPage = $object->getItemsPerPage();
4✔
139
        } elseif (\is_array($object) || $object instanceof \Countable) {
3✔
140
            $totalItems = \count($object);
3✔
141
        }
142

143
        return [$paginator, $paginated, $currentPage, $itemsPerPage, $lastPage, $pageTotalItems, $totalItems];
7✔
144
    }
145

146
    /**
147
     * Gets the pagination data.
148
     *
149
     * @param iterable $object
150
     */
151
    abstract protected function getPaginationData($object, array $context = []): array;
152

153
    /**
154
     * Gets items data.
155
     *
156
     * @param iterable $object
157
     */
158
    abstract protected function getItemsData($object, string $format = null, array $context = []): array;
159
}
160

161
class_alias(AbstractCollectionNormalizer::class, \ApiPlatform\Core\Serializer\AbstractCollectionNormalizer::class);
×
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