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

src/JsonLd/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\JsonLd\Serializer;
15
16
use ApiPlatform\Core\Api\IriConverterInterface;
17
use ApiPlatform\Core\JsonLd\AnonymousContextBuilderInterface;
18
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
19
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
20
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
21
22
/**
23
 * Decorates the output with JSON-LD metadata when appropriate, but otherwise just
24
 * passes through to the decorated normalizer.
25
 */
26
final class ObjectNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
27
{
28
    use JsonLdContextTrait;
29
30
    public const FORMAT = 'jsonld';
31
32
    private $decorated;
33
    private $iriConverter;
34
    private $anonymousContextBuilder;
35
36
    public function __construct(NormalizerInterface $decorated, IriConverterInterface $iriConverter, AnonymousContextBuilderInterface $anonymousContextBuilder)
37
    {
38
        $this->decorated = $decorated;
39
        $this->iriConverter = $iriConverter;
40
        $this->anonymousContextBuilder = $anonymousContextBuilder;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function supportsNormalization($data, $format = null, array $context = []): bool
47
    {
48
        return self::FORMAT === $format && $this->decorated->supportsNormalization($data, $format, $context);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function hasCacheableSupportsMethod(): bool
55
    {
56
        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 Symfony\Component\Serial...er\SerializerNormalizer or ApiPlatform\Core\Tests\F...DocumentationNormalizer or Symfony\Component\Serial...rmalizer\TestNormalizer or Symfony\Component\Serial...sonSerializerNormalizer or Symfony\Component\Serial...wareNormalizerInterface or Symfony\Component\Serial...ectSerializerNormalizer 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

56
        return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->/** @scrutinizer ignore-call */ hasCacheableSupportsMethod();
Loading history...
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function normalize($object, $format = null, array $context = [])
63
    {
64
        if (isset($context['api_resource'])) {
65
            $originalResource = $context['api_resource'];
66
            unset($context['api_resource']);
67
        }
68
69
        /*
70
         * Converts the normalized data array of a resource into an IRI, if the
71
         * normalized data array is empty.
72
         *
73
         * This is useful when traversing from a non-resource towards an attribute
74
         * which is a resource, as we do not have the benefit of {@see PropertyMetadata::isReadableLink}.
75
         *
76
         * It must not be propagated to subresources, as {@see PropertyMetadata::isReadableLink}
77
         * should take effect.
78
         */
79
        $context['api_empty_resource_as_iri'] = true;
80
81
        $data = $this->decorated->normalize($object, $format, $context);
82
        if (!\is_array($data)) {
83
            return $data;
84
        }
85
86
        if (isset($originalResource)) {
87
            $context['output']['iri'] = $this->iriConverter->getIriFromItem($originalResource);
88
            $context['api_resource'] = $originalResource;
89
        }
90
91
        $metadata = $this->createJsonLdContext($this->anonymousContextBuilder, $object, $context);
92
93
        return $metadata + $data;
94
    }
95
}
96