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

api-platform / core / 9710836697

28 Jun 2024 09:35AM UTC coverage: 63.285% (+1.2%) from 62.122%
9710836697

push

github

soyuka
docs: changelog v3.3.7

11104 of 17546 relevant lines covered (63.29%)

52.26 hits per line

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

8.82
/src/JsonApi/Serializer/ObjectNormalizer.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\JsonApi\Serializer;
15

16
use ApiPlatform\Api\IriConverterInterface as LegacyIriConverterInterface;
17
use ApiPlatform\Api\ResourceClassResolverInterface as LegacyResourceClassResolverInterface;
18
use ApiPlatform\Metadata\IriConverterInterface;
19
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
20
use ApiPlatform\Metadata\ResourceClassResolverInterface;
21
use ApiPlatform\Metadata\Util\ClassInfoTrait;
22
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
23
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
24
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
25
use Symfony\Component\Serializer\Serializer;
26

27
/**
28
 * Decorates the output with JSON API metadata when appropriate, but otherwise
29
 * just passes through to the decorated normalizer.
30
 */
31
final class ObjectNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
32
{
33
    use ClassInfoTrait;
34

35
    public const FORMAT = 'jsonapi';
36

37
    public function __construct(private readonly NormalizerInterface $decorated, private readonly IriConverterInterface|LegacyIriConverterInterface $iriConverter, private readonly ResourceClassResolverInterface|LegacyResourceClassResolverInterface $resourceClassResolver, private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory)
38
    {
39
    }
259✔
40

41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
45
    {
46
        return self::FORMAT === $format && $this->decorated->supportsNormalization($data, $format, $context);
×
47
    }
48

49
    public function getSupportedTypes($format): array
50
    {
51
        // @deprecated remove condition when support for symfony versions under 6.3 is dropped
52
        if (!method_exists($this->decorated, 'getSupportedTypes')) {
175✔
53
            return [
×
54
                '*' => $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
×
55
            ];
×
56
        }
57

58
        return self::FORMAT === $format ? $this->decorated->getSupportedTypes($format) : [];
175✔
59
    }
60

61
    public function hasCacheableSupportsMethod(): bool
62
    {
63
        if (method_exists(Serializer::class, 'getSupportedTypes')) {
×
64
            trigger_deprecation(
×
65
                'api-platform/core',
×
66
                '3.1',
×
67
                'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
×
68
                __METHOD__
×
69
            );
×
70
        }
71

72
        return $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
×
73
    }
74

75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
79
    {
80
        if (isset($context['api_resource'])) {
×
81
            $originalResource = $context['api_resource'];
×
82
            unset($context['api_resource']);
×
83
        }
84

85
        $data = $this->decorated->normalize($object, $format, $context);
×
86
        if (!\is_array($data) || isset($context['api_attribute'])) {
×
87
            return $data;
×
88
        }
89

90
        if (isset($originalResource)) {
×
91
            $resourceClass = $this->resourceClassResolver->getResourceClass($originalResource);
×
92
            $resourceData = [
×
93
                'id' => $this->iriConverter->getIriFromResource($originalResource),
×
94
                'type' => $this->resourceMetadataFactory->create($resourceClass)->getOperation()->getShortName(),
×
95
            ];
×
96
        } else {
97
            $resourceData = [
×
98
                'id' => $this->iriConverter->getIriFromResource($object),
×
99
                'type' => (new \ReflectionClass($this->getObjectClass($object)))->getShortName(),
×
100
            ];
×
101
        }
102

103
        if ($data) {
×
104
            $resourceData['attributes'] = $data;
×
105
        }
106

107
        return ['data' => $resourceData];
×
108
    }
109
}
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