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

api-platform / core / 3711198772

pending completion
3711198772

push

github

GitHub
fix(jsonschema): remove @id @type @context from input schema  (#5267)

2 of 2 new or added lines in 1 file covered. (100.0%)

7447 of 12203 relevant lines covered (61.03%)

82.13 hits per line

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

42.86
/src/Hal/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\Hal\Serializer;
15

16
use ApiPlatform\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
    public function __construct(private readonly NormalizerInterface $decorated, private readonly IriConverterInterface $iriConverter)
31
    {
32
    }
758✔
33

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

42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function hasCacheableSupportsMethod(): bool
46
    {
47
        return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
595✔
48
    }
49

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

60
        $data = $this->decorated->normalize($object, $format, $context);
2✔
61
        if (!\is_array($data)) {
2✔
62
            return $data;
×
63
        }
64

65
        if (!isset($originalResource)) {
2✔
66
            return $data;
2✔
67
        }
68

69
        $metadata = [
×
70
            '_links' => [
×
71
                'self' => [
×
72
                    'href' => $this->iriConverter->getIriFromResource($originalResource),
×
73
                ],
×
74
            ],
×
75
        ];
×
76

77
        return $metadata + $data;
×
78
    }
79

80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool
84
    {
85
        // prevent the use of lower priority normalizers (e.g. serializer.normalizer.object) for this format
86
        return self::FORMAT === $format;
23✔
87
    }
88

89
    /**
90
     * {@inheritdoc}
91
     *
92
     * @throws LogicException
93
     */
94
    public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed
95
    {
96
        throw new LogicException(sprintf('%s is a read-only format.', self::FORMAT));
×
97
    }
98
}
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