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

api-platform / core / 5644825543

pending completion
5644825543

push

github

web-flow
feat(serializer): support for getSupportedTypes (symfony 6.3) (#5672)

109 of 109 new or added lines in 29 files covered. (100.0%)

10881 of 18245 relevant lines covered (59.64%)

20.04 hits per line

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

71.43
/src/Elasticsearch/Serializer/ItemNormalizer.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\Elasticsearch\Serializer;
15

16
use Symfony\Component\Serializer\Exception\LogicException;
17
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
18
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
19
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20
use Symfony\Component\Serializer\SerializerAwareInterface;
21
use Symfony\Component\Serializer\SerializerInterface;
22

23
/**
24
 * Item normalizer decorator that prevents {@see \ApiPlatform\Serializer\ItemNormalizer}
25
 * from taking over for the {@see DocumentNormalizer::FORMAT} format because of priorities.
26
 *
27
 * @experimental
28
 */
29
final class ItemNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface, CacheableSupportsMethodInterface
30
{
31
    public const FORMAT = 'elasticsearch';
32

33
    public function __construct(private readonly NormalizerInterface $decorated)
34
    {
35
    }
22✔
36

37
    /**
38
     * @throws LogicException
39
     */
40
    public function hasCacheableSupportsMethod(): bool
41
    {
42
        if (!$this->decorated instanceof CacheableSupportsMethodInterface) {
4✔
43
            throw new LogicException(sprintf('The decorated normalizer must be an instance of "%s".', CacheableSupportsMethodInterface::class));
2✔
44
        }
45

46
        return $this->decorated->hasCacheableSupportsMethod();
2✔
47
    }
48

49
    /**
50
     * {@inheritdoc}
51
     *
52
     * @throws LogicException
53
     */
54
    public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed
55
    {
56
        if (!$this->decorated instanceof DenormalizerInterface) {
4✔
57
            throw new LogicException(sprintf('The decorated normalizer must be an instance of "%s".', DenormalizerInterface::class));
2✔
58
        }
59

60
        return $this->decorated->denormalize($data, $type, $format, $context);
2✔
61
    }
62

63
    /**
64
     * {@inheritdoc}
65
     *
66
     * @throws LogicException
67
     */
68
    public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool
69
    {
70
        if (!$this->decorated instanceof DenormalizerInterface) {
4✔
71
            throw new LogicException(sprintf('The decorated normalizer must be an instance of "%s".', DenormalizerInterface::class));
2✔
72
        }
73

74
        return DocumentNormalizer::FORMAT !== $format && $this->decorated->supportsDenormalization($data, $type, $format, $context); // @phpstan-ignore-line symfony bc-layer
2✔
75
    }
76

77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function normalize(mixed $object, string $format = null, array $context = []): array
81
    {
82
        return $this->decorated->normalize($object, $format, $context);
2✔
83
    }
84

85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
89
    {
90
        return DocumentNormalizer::FORMAT !== $format && $this->decorated->supportsNormalization($data, $format);
2✔
91
    }
92

93
    public function getSupportedTypes($format): array
94
    {
95
        // @deprecated remove condition when support for symfony versions under 6.3 is dropped
96
        if (!method_exists($this->decorated, 'getSupportedTypes')) {
×
97
            return [
×
98
                DocumentNormalizer::FORMAT => null,
×
99
                '*' => $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
×
100
            ];
×
101
        }
102

103
        return DocumentNormalizer::FORMAT !== $format ? $this->decorated->getSupportedTypes($format) : [];
×
104
    }
105

106
    /**
107
     * {@inheritdoc}
108
     *
109
     * @throws LogicException
110
     */
111
    public function setSerializer(SerializerInterface $serializer): void
112
    {
113
        if (!$this->decorated instanceof SerializerAwareInterface) {
4✔
114
            throw new LogicException(sprintf('The decorated normalizer must be an instance of "%s".', SerializerAwareInterface::class));
2✔
115
        }
116

117
        $this->decorated->setSerializer($serializer);
2✔
118
    }
119
}
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

© 2026 Coveralls, Inc