Completed
Pull Request — master (#608)
by Amrouche
06:33 queued 02:02
created

JsonHalEncoder::decode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 4
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\Hal\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 View Code Duplication
final class JsonHalEncoder implements EncoderInterface, DecoderInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
{
28
    const FORMAT = 'jsonhal';
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