Completed
Pull Request — master (#590)
by Kévin
07:36 queued 03:30
created

JsonLdEncoder::decode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
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
namespace ApiPlatform\Core\JsonLd\Serializer;
13
14
use Symfony\Component\HttpFoundation\JsonResponse;
15
use Symfony\Component\Serializer\Encoder\DecoderInterface;
16
use Symfony\Component\Serializer\Encoder\EncoderInterface;
17
use Symfony\Component\Serializer\Encoder\JsonDecode;
18
use Symfony\Component\Serializer\Encoder\JsonEncode;
19
use Symfony\Component\Serializer\Encoder\JsonEncoder;
20
21
/**
22
 * JSON-LD Encoder.
23
 *
24
 * @author Kévin Dunglas <[email protected]>
25
 */
26
final class JsonLdEncoder implements EncoderInterface, DecoderInterface
27
{
28
    const FORMAT = 'jsonld';
29
30
    private $jsonEncoder;
31
32
    public function __construct(JsonEncoder $jsonEncoder = null)
33
    {
34
        // Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be embedded into HTML.
35
        $this->jsonEncoder = $jsonEncoder ?: new JsonEncoder(
36
            new JsonEncode(JsonResponse::DEFAULT_ENCODING_OPTIONS), new JsonDecode(true)
37
        );
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function supportsEncoding($format)
44
    {
45
        return self::FORMAT === $format;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function encode($data, $format, array $context = [])
52
    {
53
        return $this->jsonEncoder->encode($data, $format, $context);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function supportsDecoding($format)
60
    {
61
        return self::FORMAT === $format;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function decode($data, $format, array $context = [])
68
    {
69
        return $this->jsonEncoder->decode($data, $format, $context);
70
    }
71
}
72