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

api-platform / core / 15283185369

27 May 2025 06:43PM UTC coverage: 0.0% (-26.4%) from 26.397%
15283185369

Pull #7180

github

web-flow
Merge 9a6703d44 into c022b441b
Pull Request #7180: feat(deps): allow Elasticsearch v9

0 of 51334 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/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\Metadata\IriConverterInterface;
17
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
18
use ApiPlatform\Metadata\ResourceClassResolverInterface;
19
use ApiPlatform\Metadata\Util\ClassInfoTrait;
20
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
21

22
/**
23
 * Decorates the output with JSON API metadata when appropriate, but otherwise
24
 * just passes through to the decorated normalizer.
25
 */
26
final class ObjectNormalizer implements NormalizerInterface
27
{
28
    use ClassInfoTrait;
29

30
    public const FORMAT = 'jsonapi';
31

32
    public function __construct(private readonly NormalizerInterface $decorated, private readonly IriConverterInterface $iriConverter, private readonly ResourceClassResolverInterface $resourceClassResolver, private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory)
33
    {
34
    }
×
35

36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
40
    {
41
        return self::FORMAT === $format && $this->decorated->supportsNormalization($data, $format, $context);
×
42
    }
43

44
    public function getSupportedTypes($format): array
45
    {
46
        return self::FORMAT === $format ? $this->decorated->getSupportedTypes($format) : [];
×
47
    }
48

49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
53
    {
54
        if (isset($context['api_resource'])) {
×
55
            $originalResource = $context['api_resource'];
×
56
            unset($context['api_resource']);
×
57
        }
58

59
        $data = $this->decorated->normalize($object, $format, $context);
×
60
        if (!\is_array($data) || isset($context['api_attribute'])) {
×
61
            return $data;
×
62
        }
63

64
        if (isset($originalResource)) {
×
65
            $resourceClass = $this->resourceClassResolver->getResourceClass($originalResource);
×
66
            $resourceData = [
×
67
                'id' => $this->iriConverter->getIriFromResource($originalResource),
×
68
                'type' => $this->resourceMetadataFactory->create($resourceClass)->getOperation()->getShortName(),
×
69
            ];
×
70
        } else {
71
            $resourceData = [
×
72
                'id' => $this->iriConverter->getIriFromResource($object),
×
73
                'type' => (new \ReflectionClass($this->getObjectClass($object)))->getShortName(),
×
74
            ];
×
75
        }
76

77
        if ($data) {
×
78
            $resourceData['attributes'] = $data;
×
79
        }
80

81
        return ['data' => $resourceData];
×
82
    }
83
}
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