GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 486f11...b55f68 )
by t
07:38 queued 01:58
created

Json::decode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * Class Json
4
 *
5
 * @link https://www.icy2003.com/
6
 * @author icy2003 <[email protected]>
7
 * @copyright Copyright (c) 2017, icy2003
8
 */
9
10
namespace icy2003\php\ihelpers;
11
12
use icy2003\php\I;
13
14
/**
15
 * Json 类
16
 */
17
class Json
18
{
19
    /**
20
     * Json 编码
21
     *
22
     * @param array $value 数组
23
     * @param int $options Json 选项,默认 JSON_UNESCAPED_UNICODE,其他选项说明:
24
     * ```
25
     * JSON_HEX_QUOT:所有的 " 转换成 \u0022。PHP>=5.3.0
26
     * JSON_HEX_TAG:所有的 < 和 > 转换成 \u003C 和 \u003E。PHP>=5.3.0
27
     * JSON_HEX_AMP:所有的 & 转换成 \u0026。PHP>=5.3.0
28
     * JSON_HEX_APOS:所有的 ' 转换成 \u0027。PHP>=5.3.0
29
     * JSON_NUMERIC_CHECK:将所有数字字符串编码成数字(numbers)。PHP>=5.3.3
30
     * JSON_PRETTY_PRINT:用空白字符格式化返回的数据。PHP>=5.4.0
31
     * JSON_UNESCAPED_SLASHES:不要编码 /。PHP>=5.4.0
32
     * JSON_FORCE_OBJECT:使一个非关联数组输出一个类(Object)而非数组。 在数组为空而接受者需要一个类(Object)的时候尤其有用。PHP>=5.3.0
33
     * JSON_PRESERVE_ZERO_FRACTION:确保 float 值一直都编码成 float。PHP>=5.6.6
34
     * JSON_UNESCAPED_UNICODE:以字面编码多字节 Unicode 字符(默认是编码成 \uXXXX)。PHP>=5.4.0
35
     * JSON_PARTIAL_OUTPUT_ON_ERROR:替换一些不可编码的值,而不是失败。PHP>=5.5.0
36
     * ```
37
     * @return string
38
     */
39 4
    public static function encode($value, $options = null)
40
    {
41 4
        null === $options && $options = JSON_UNESCAPED_UNICODE;
42 4
        return json_encode($value, $options);
43
    }
44
45
    /**
46
     * Json 解码
47
     *
48
     * @param string $json Json 字符串
49
     * @param boolean $assoc 是否返回对象,默认否(和原生函数相反)
50
     *
51
     * @return array
52
     */
53 6
    public static function decode($json, $assoc = true)
54
    {
55 6
        return json_decode($json, $assoc);
56
    }
57
58
    /**
59
     * 判断是否是 Json 字符串
60
     *
61
     * @param string $json
62
     *
63
     * @return boolean
64
     */
65 1
    public static function isJson($json)
66
    {
67 1
        if (!is_string($json)) {
0 ignored issues
show
introduced by
The condition is_string($json) is always true.
Loading history...
68 1
            return false;
69
        }
70 1
        $array = static::decode($json);
71 1
        if (is_array($array)) {
0 ignored issues
show
introduced by
The condition is_array($array) is always true.
Loading history...
72 1
            return true;
73
        }
74
75 1
        return false;
76
    }
77
78
    /**
79
     * Json 字符串的 Ajax 返回
80
     *
81
     * @param string|array $json 数组或 Json 字符串
82
     *
83
     * @return void
84
     */
85
    public static function ajax($json)
86
    {
87
        header('Content-Type:application/json; charset=utf-8');
88
        if (is_array($json)) {
89
            $json = static::encode($json);
90
        }
91
        if (false === static::isJson($json)) {
0 ignored issues
show
introduced by
The condition false === static::isJson($json) is always false.
Loading history...
92
            $json = '[]';
93
        }
94
        echo $json;die;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
95
    }
96
97
    /**
98
     * 获取 Json 字符串里的参数
99
     *
100
     * @param string $json Json 字符串
101
     * @param string $key @see \icy2003\php\I::get
102
     * @param mixed $defaultValue 取不到对应的值时返回的默认值,默认为 null
103
     *
104
     * @return mixed
105
     */
106 1
    public static function get($json, $key, $defaultValue = null)
107
    {
108 1
        $array = static::decode($json);
109 1
        return I::get($array, $key, $defaultValue);
110
    }
111
}
112