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

api-platform / core / 13814792797

12 Mar 2025 03:09PM UTC coverage: 5.889% (-1.4%) from 7.289%
13814792797

Pull #7012

github

web-flow
Merge 199d44919 into 284937039
Pull Request #7012: doc: comment typo in ApiResource.php

10048 of 170615 relevant lines covered (5.89%)

5.17 hits per line

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

23.81
/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\Metadata\IriConverterInterface;
17
use Symfony\Component\Serializer\Exception\LogicException;
18
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
19
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20

21
/**
22
 * Decorates the output with JSON HAL metadata when appropriate, but otherwise
23
 * just passes through to the decorated normalizer.
24
 */
25
final class ObjectNormalizer implements NormalizerInterface, DenormalizerInterface
26
{
27
    public const FORMAT = 'jsonhal';
28

29
    public function __construct(private readonly NormalizerInterface $decorated, private readonly IriConverterInterface $iriConverter)
30
    {
31
    }
393✔
32

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

41
    public function getSupportedTypes($format): array
42
    {
43
        return self::FORMAT === $format ? $this->decorated->getSupportedTypes($format) : [];
294✔
44
    }
45

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

56
        $data = $this->decorated->normalize($object, $format, $context);
×
57
        if (!\is_array($data)) {
×
58
            return $data;
×
59
        }
60

61
        if (!isset($originalResource)) {
×
62
            return $data;
×
63
        }
64

65
        $metadata = [
×
66
            '_links' => [
×
67
                'self' => [
×
68
                    'href' => $this->iriConverter->getIriFromResource($originalResource),
×
69
                ],
×
70
            ],
×
71
        ];
×
72

73
        return $metadata + $data;
×
74
    }
75

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

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