Passed
Push — master ( ce7fff...9920f1 )
by Alan
04:04
created

src/Hal/Serializer/ObjectNormalizer.php (1 issue)

Labels
Severity
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
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\Core\Hal\Serializer;
15
16
use ApiPlatform\Core\Api\IriConverterInterface;
17
use Symfony\Component\Serializer\Exception\LogicException;
18
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
19
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
20
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
21
22
/**
23
 * Decorates the output with JSON HAL metadata when appropriate, but otherwise
24
 * just passes through to the decorated normalizer.
25
 */
26
final class ObjectNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
27
{
28
    public const FORMAT = 'jsonhal';
29
30
    private $decorated;
31
    private $iriConverter;
32
33
    public function __construct(NormalizerInterface $decorated, IriConverterInterface $iriConverter)
34
    {
35
        $this->decorated = $decorated;
36
        $this->iriConverter = $iriConverter;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function supportsNormalization($data, $format = null, array $context = []): bool
43
    {
44
        return self::FORMAT === $format && $this->decorated->supportsNormalization($data, $format, $context);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function hasCacheableSupportsMethod(): bool
51
    {
52
        return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
0 ignored issues
show
The method hasCacheableSupportsMethod() does not exist on Symfony\Component\Serial...zer\NormalizerInterface. It seems like you code against a sub-type of said class. However, the method does not exist in ApiPlatform\Core\GraphQl...HttpExceptionNormalizer or Symfony\Component\Serial...er\SerializerNormalizer or ApiPlatform\Core\GraphQl...ception\ErrorNormalizer or ApiPlatform\Core\Tests\F...DocumentationNormalizer or Symfony\Component\Serial...rmalizer\TestNormalizer or Symfony\Component\Serial...sonSerializerNormalizer or ApiPlatform\Core\GraphQl...tionExceptionNormalizer or Symfony\Component\Serial...wareNormalizerInterface or Symfony\Component\Serial...ectSerializerNormalizer or ApiPlatform\Core\GraphQl...timeExceptionNormalizer or Symfony\Component\Serializer\Serializer. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
        return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->/** @scrutinizer ignore-call */ hasCacheableSupportsMethod();
Loading history...
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function normalize($object, $format = null, array $context = [])
59
    {
60
        if (isset($context['api_resource'])) {
61
            $originalResource = $context['api_resource'];
62
            unset($context['api_resource']);
63
        }
64
65
        $data = $this->decorated->normalize($object, $format, $context);
66
        if (!\is_array($data)) {
67
            return $data;
68
        }
69
70
        if (!isset($originalResource)) {
71
            return $data;
72
        }
73
74
        $metadata = [
75
            '_links' => [
76
                'self' => [
77
                    'href' => $this->iriConverter->getIriFromItem($originalResource),
78
                ],
79
            ],
80
        ];
81
82
        return $metadata + $data;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
89
    {
90
        // prevent the use of lower priority normalizers (e.g. serializer.normalizer.object) for this format
91
        return self::FORMAT === $format;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     *
97
     * @throws LogicException
98
     */
99
    public function denormalize($data, $class, $format = null, array $context = [])
100
    {
101
        throw new LogicException(sprintf('%s is a read-only format.', self::FORMAT));
102
    }
103
}
104