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

api-platform / core / 7047458607

30 Nov 2023 01:53PM UTC coverage: 37.263% (+0.002%) from 37.261%
7047458607

push

github

soyuka
Merge 3.2

20 of 35 new or added lines in 12 files covered. (57.14%)

10 existing lines in 3 files now uncovered.

10295 of 27628 relevant lines covered (37.26%)

21.04 hits per line

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

0.0
/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 ApiPlatform\Serializer\CacheableSupportsMethodInterface;
17
use Symfony\Component\Serializer\Exception\LogicException;
18
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
19
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
20
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
21
use Symfony\Component\Serializer\Serializer;
22
use Symfony\Component\Serializer\SerializerAwareInterface;
23
use Symfony\Component\Serializer\SerializerInterface;
24

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

35
    public function __construct(private readonly NormalizerInterface $decorated)
36
    {
37
    }
×
38

39
    /**
40
     * @throws LogicException
41
     */
42
    public function hasCacheableSupportsMethod(): bool
43
    {
44
        if (method_exists(Serializer::class, 'getSupportedTypes')) {
×
45
            trigger_deprecation(
×
46
                'api-platform/core',
×
47
                '3.1',
×
48
                'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
×
49
                __METHOD__
×
50
            );
×
51
        }
52

53
        if (!$this->decorated instanceof BaseCacheableSupportsMethodInterface) {
×
54
            throw new LogicException(sprintf('The decorated normalizer must be an instance of "%s".', BaseCacheableSupportsMethodInterface::class));
×
55
        }
56

57
        return $this->decorated->hasCacheableSupportsMethod();
×
58
    }
59

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

71
        return $this->decorated->denormalize($data, $type, $format, $context);
×
72
    }
73

74
    /**
75
     * {@inheritdoc}
76
     *
77
     * @throws LogicException
78
     */
79
    public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool
80
    {
81
        if (!$this->decorated instanceof DenormalizerInterface) {
×
82
            throw new LogicException(sprintf('The decorated normalizer must be an instance of "%s".', DenormalizerInterface::class));
×
83
        }
84

NEW
85
        return DocumentNormalizer::FORMAT !== $format && $this->decorated->supportsDenormalization($data, $type, $format, $context);
×
86
    }
87

88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function normalize(mixed $object, string $format = null, array $context = []): array
92
    {
93
        return $this->decorated->normalize($object, $format, $context);
×
94
    }
95

96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
100
    {
101
        return DocumentNormalizer::FORMAT !== $format && $this->decorated->supportsNormalization($data, $format);
×
102
    }
103

104
    public function getSupportedTypes($format): array
105
    {
106
        // @deprecated remove condition when support for symfony versions under 6.3 is dropped
107
        if (!method_exists($this->decorated, 'getSupportedTypes')) {
×
108
            return [
×
109
                DocumentNormalizer::FORMAT => null,
×
110
                '*' => $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
×
111
            ];
×
112
        }
113

114
        return DocumentNormalizer::FORMAT !== $format ? $this->decorated->getSupportedTypes($format) : [];
×
115
    }
116

117
    /**
118
     * {@inheritdoc}
119
     *
120
     * @throws LogicException
121
     */
122
    public function setSerializer(SerializerInterface $serializer): void
123
    {
124
        if (!$this->decorated instanceof SerializerAwareInterface) {
×
125
            throw new LogicException(sprintf('The decorated normalizer must be an instance of "%s".', SerializerAwareInterface::class));
×
126
        }
127

128
        $this->decorated->setSerializer($serializer);
×
129
    }
130
}
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