Passed
Push — master ( 8bd912...d93388 )
by Alan
06:58 queued 02:20
created

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

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);
0 ignored issues
show
The call to Symfony\Component\Serial...supportsNormalization() has too many arguments starting with $context. ( Ignorable by Annotation )

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

48
        return self::FORMAT === $format && $this->decorated->/** @scrutinizer ignore-call */ supportsNormalization($data, $format, $context);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function hasCacheableSupportsMethod(): bool
55
    {
56
        return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
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