Test Failed
Push — master ( 90f1e5...8cd561 )
by Jim
02:25
created

Json   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 47
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A decode() 0 11 2
A encode() 0 11 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: lenovo
5
 * Date: 6/14/2018
6
 * Time: 2:43 PM
7
 */
8
namespace TimSDK\Support;
9
10
use TimSDK\Core\Exceptions\JsonParseException;
11
12
class Json
13
{
14
    /**
15
     * Returns the JSON representation of a value
16
     *
17
     * @static
18
     * @param mixed $value
19
     * @param int   $options
20
     * @param int   $depth
21
     * @return string
22
     * @throws JsonParseException
23
     */
24
    public static function encode($value, $options = 0, $depth = 512)
25
    {
26
        $json = \json_encode($value, $options, $depth);
27
        if (JSON_ERROR_NONE !== json_last_error()) {
28
            throw new JsonParseException(
29
                'json_encode error: ' . json_last_error_msg(),
30
                json_last_error()
31
            );
32
        }
33
34
        return $json;
35
    }
36
37
    /**
38
     * Decodes a JSON string
39
     *
40
     * @static
41
     * @param string $json
42
     * @param bool   $assoc
43
     * @param int    $depth
44
     * @param int    $options
45
     * @return mixed
46
     * @throws JsonParseException
47
     */
48
    public static function decode($json, $assoc = false, $depth = 512, $options = 0)
49
    {
50
        $data = \json_decode($json, $assoc, $depth, $options);
51
        if (JSON_ERROR_NONE !== json_last_error()) {
52
            throw new JsonParseException(
53
                'json_decode error: ' . json_last_error_msg(),
54
                json_last_error()
55
            );
56
        }
57
58
        return $data;
59
    }
60
}
61