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

api-platform / core / 10014117656

19 Jul 2024 08:44PM UTC coverage: 7.856% (-56.3%) from 64.185%
10014117656

push

github

soyuka
Merge branch 'sf/remove-flag'

0 of 527 new or added lines in 83 files covered. (0.0%)

10505 existing lines in 362 files now uncovered.

12705 of 161727 relevant lines covered (7.86%)

26.85 hits per line

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

90.0
/src/Serializer/JsonEncoder.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\Serializer;
15

16
use Symfony\Component\Serializer\Encoder\DecoderInterface;
17
use Symfony\Component\Serializer\Encoder\EncoderInterface;
18
use Symfony\Component\Serializer\Encoder\JsonDecode;
19
use Symfony\Component\Serializer\Encoder\JsonEncode;
20
use Symfony\Component\Serializer\Encoder\JsonEncoder as BaseJsonEncoder;
21

22
/**
23
 * A JSON encoder with appropriate default options to embed the generated document into HTML.
24
 *
25
 * @author Kévin Dunglas <dunglas@gmail.com>
26
 */
27
final class JsonEncoder implements EncoderInterface, DecoderInterface
28
{
29
    // @noRector \Rector\Php81\Rector\Property\ReadOnlyPropertyRector
30
    public function __construct(private readonly string $format, private ?BaseJsonEncoder $jsonEncoder = null)
31
    {
UNCOV
32
        if (null !== $this->jsonEncoder) {
2,609✔
33
            return;
×
34
        }
35

UNCOV
36
        $this->jsonEncoder = new BaseJsonEncoder(
2,609✔
37
            // Encode <, >, ', &, and " characters in the JSON, making it also safe to be embedded into HTML.
UNCOV
38
            new JsonEncode(['json_encode_options' => \JSON_HEX_TAG | \JSON_HEX_APOS | \JSON_HEX_AMP | \JSON_HEX_QUOT | \JSON_UNESCAPED_UNICODE | \JSON_INVALID_UTF8_IGNORE]),
2,609✔
UNCOV
39
            new JsonDecode(['json_decode_associative' => true])
2,609✔
UNCOV
40
        );
2,609✔
41
    }
42

43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function supportsEncoding($format, array $context = []): bool
47
    {
UNCOV
48
        return $this->format === $format;
2,112✔
49
    }
50

51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function encode($data, $format, array $context = []): string
55
    {
UNCOV
56
        return $this->jsonEncoder->encode($data, $format, $context);
1,866✔
57
    }
58

59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function supportsDecoding($format, array $context = []): bool
63
    {
UNCOV
64
        return $this->format === $format;
597✔
65
    }
66

67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function decode($data, $format, array $context = []): mixed
71
    {
72
        return $this->jsonEncoder->decode($data, $format, $context);
411✔
73
    }
74
}
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