Passed
Pull Request — master (#9)
by Quang
02:23
created

JsonEncoder::encode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\Lumen\GraphQL\Helpers;
4
5
use Digia\JsonHelpers\JsonEncoder as BaseJsonEncoder;
6
7
/**
8
 * Class JsonEncoder
9
 * @package Digia\Lumen\GraphQL\Helpers
10
 */
11
class JsonEncoder extends BaseJsonEncoder
12
{
13
14
    /**
15
     * @var array unwanted JSON Unicode characters and their replacements
16
     */
17
    private static $unwantedJsonUnicodeCharacters = [
18
        "\\u2028" => '',
19
        "\\u2029" => '',
20
    ];
21
22
    /**
23
     * Encodes JSON with some additional filtering and sanitization. In contrast to just using json_encode(), this
24
     * method throws an exception if encoding fails.
25
     *
26
     * @param mixed $data the data to encode
27
     * @param int   $options
28
     * @param int   $depth
29
     *
30
     * @return string the encoded JSON
31
     *
32
     */
33
    public static function encode($data, int $options = 0, int $depth = 512): string
34
    {
35
        // Encode and convert encoding errors to exceptions
36
        $json = parent::encode($data);
37
38
        // Filter out unwanted Unicode characters from the encoded JSON
39
        return strtr($json, self::$unwantedJsonUnicodeCharacters);
40
    }
41
42
}
43