• 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

97.22
/src/Hydra/Serializer/CollectionNormalizer.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\Hydra\Serializer;
15

16
use ApiPlatform\Api\IriConverterInterface;
17
use ApiPlatform\Api\ResourceClassResolverInterface;
18
use ApiPlatform\Api\UrlGeneratorInterface;
19
use ApiPlatform\Core\Api\IriConverterInterface as LegacyIriConverterInterface;
20
use ApiPlatform\Core\Api\OperationType;
21
use ApiPlatform\JsonLd\ContextBuilderInterface;
22
use ApiPlatform\JsonLd\Serializer\JsonLdContextTrait;
23
use ApiPlatform\Metadata\CollectionOperationInterface;
24
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
25
use ApiPlatform\Serializer\ContextTrait;
26
use ApiPlatform\Serializer\OperationContextTrait;
27
use ApiPlatform\State\Pagination\PaginatorInterface;
28
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
29
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
30
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
31
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
32
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
33

34
/**
35
 * This normalizer handles collections.
36
 *
37
 * @author Kevin Dunglas <dunglas@gmail.com>
38
 * @author Samuel ROZE <samuel.roze@gmail.com>
39
 */
40
final class CollectionNormalizer implements NormalizerInterface, NormalizerAwareInterface, CacheableSupportsMethodInterface
41
{
42
    use ContextTrait;
43
    use JsonLdContextTrait;
44
    use NormalizerAwareTrait;
45
    use OperationContextTrait;
46

47
    public const FORMAT = 'jsonld';
48
    public const IRI_ONLY = 'iri_only';
49

50
    private $contextBuilder;
51
    private $resourceClassResolver;
52
    private $iriConverter;
53
    private $resourceMetadataCollectionFactory;
54
    private $defaultContext = [
55
        self::IRI_ONLY => false,
56
    ];
57

58
    public function __construct(ContextBuilderInterface $contextBuilder, ResourceClassResolverInterface $resourceClassResolver, $iriConverter, ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null, array $defaultContext = [])
59
    {
60
        $this->contextBuilder = $contextBuilder;
35✔
61
        $this->resourceClassResolver = $resourceClassResolver;
35✔
62

63
        if ($iriConverter instanceof LegacyIriConverterInterface) {
35✔
64
            trigger_deprecation('api-platform/core', '2.7', sprintf('Use an implementation of "%s" instead of "%s".', IriConverterInterface::class, LegacyIriConverterInterface::class));
9✔
65
        }
66
        $this->iriConverter = $iriConverter;
35✔
67
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
35✔
68
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
35✔
69
    }
70

71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function supportsNormalization($data, $format = null, array $context = []): bool
75
    {
76
        return self::FORMAT === $format && is_iterable($data);
14✔
77
    }
78

79
    /**
80
     * {@inheritdoc}
81
     *
82
     * @param iterable $object
83
     */
84
    public function normalize($object, $format = null, array $context = []): array
85
    {
86
        if (!isset($context['resource_class']) || isset($context['api_sub_level'])) {
17✔
87
            return $this->normalizeRawCollection($object, $format, $context);
5✔
88
        }
89

90
        $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class']);
12✔
91
        $context = $this->initContext($resourceClass, $context);
12✔
92
        $context['api_collection_sub_level'] = true;
12✔
93
        $data = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
12✔
94

95
        if ($this->iriConverter instanceof LegacyIriConverterInterface) {
12✔
96
            // TODO: remove in 3.0
97
            $data['@id'] = isset($context['operation_type']) && OperationType::SUBRESOURCE === $context['operation_type'] ? $this->iriConverter->getSubresourceIriFromResourceClass($resourceClass, $context) : $this->iriConverter->getIriFromResourceClass($resourceClass);
5✔
98
        } else {
99
            $data['@id'] = $this->iriConverter->getIriFromResource($resourceClass, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context);
7✔
100
        }
101

102
        $data['@type'] = 'hydra:Collection';
12✔
103
        $data['hydra:member'] = [];
12✔
104
        $iriOnly = $context[self::IRI_ONLY] ?? $this->defaultContext[self::IRI_ONLY];
12✔
105

106
        $childContext = $this->createOperationContext($context, $resourceClass);
12✔
107
        foreach ($object as $obj) {
12✔
108
            if ($iriOnly) {
6✔
109
                $data['hydra:member'][] = $this->iriConverter instanceof LegacyIriConverterInterface ? $this->iriConverter->getIriFromItem($obj) : $this->iriConverter->getIriFromResource($obj);
2✔
110
            } else {
111
                $data['hydra:member'][] = $this->normalizer->normalize($obj, $format, $childContext);
4✔
112
            }
113
        }
114

115
        if ($object instanceof PaginatorInterface) {
12✔
116
            $data['hydra:totalItems'] = $object->getTotalItems();
7✔
117
        }
118
        if (\is_array($object) || ($object instanceof \Countable && !$object instanceof PartialPaginatorInterface)) {
12✔
119
            $data['hydra:totalItems'] = \count($object);
3✔
120
        }
121

122
        return $data;
12✔
123
    }
124

125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function hasCacheableSupportsMethod(): bool
129
    {
130
        return true;
14✔
131
    }
132

133
    /**
134
     * Normalizes a raw collection (not API resources).
135
     */
136
    private function normalizeRawCollection(iterable $object, ?string $format, array $context): array
137
    {
138
        $data = [];
5✔
139
        foreach ($object as $index => $obj) {
5✔
140
            $data[$index] = $this->normalizer->normalize($obj, $format, $context);
4✔
141
        }
142

143
        return $data;
5✔
144
    }
145
}
146

147
class_alias(CollectionNormalizer::class, \ApiPlatform\Core\Hydra\Serializer\CollectionNormalizer::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