JsonLastError   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 41
c 2
b 1
f 0
dl 0
loc 73
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCollection() 0 53 2
A check() 0 6 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: sheldon
5
 * Date: 18-6-12
6
 * Time: 下午4:14.
7
 */
8
9
namespace MiotApi\Util\Jsoner;
10
11
/**
12
 * The JSON last error collection.
13
 */
14
class JsonLastError
15
{
16
    /**
17
     * Check for errors.
18
     *
19
     * @return array|null
20
     */
21
    public static function check()
22
    {
23
        $collections = self::getCollection();
24
        $jsonLastError = json_last_error();
25
26
        return isset($jsonLastError) ? $collections[$jsonLastError] : $collections['default'];
27
    }
28
29
    /**
30
     * Get collection of JSON errors.
31
     *
32
     * @return array
33
     */
34
    public static function getCollection()
35
    {
36
        $collections = [
37
            JSON_ERROR_NONE  => null,
38
            JSON_ERROR_DEPTH => [
39
                'message'    => 'Maximum stack depth exceeded',
40
                'error-code' => 1,
41
            ],
42
            JSON_ERROR_STATE_MISMATCH => [
43
                'message'    => 'Underflow or the modes mismatch',
44
                'error-code' => 2,
45
            ],
46
            JSON_ERROR_CTRL_CHAR => [
47
                'message'    => 'Unexpected control char found',
48
                'error-code' => 3,
49
            ],
50
            JSON_ERROR_SYNTAX => [
51
                'message'    => 'Syntax error, malformed JSON',
52
                'error-code' => 4,
53
            ],
54
            JSON_ERROR_UTF8 => [
55
                'message'    => 'Malformed UTF-8 characters',
56
                'error-code' => 5,
57
            ],
58
            JSON_ERROR_RECURSION => [
59
                'message'    => 'Recursion error in value to be encoded',
60
                'error-code' => 6,
61
            ],
62
            JSON_ERROR_INF_OR_NAN => [
63
                'message'    => 'Error NAN/INF in value to be encoded',
64
                'error-code' => 7,
65
            ],
66
            JSON_ERROR_UNSUPPORTED_TYPE => [
67
                'message'    => 'Type value given cannot be encoded',
68
                'error-code' => 8,
69
            ],
70
            'default' => [
71
                'message'    => 'Unknown error',
72
                'error-code' => 999,
73
            ],
74
        ];
75
        if (version_compare(PHP_VERSION, '7.0.0', '>=')) {
76
            $collections[JSON_ERROR_INVALID_PROPERTY_NAME] = [
77
                'message'    => 'Name value given cannot be encoded',
78
                'error-code' => 9,
79
            ];
80
            $collections[JSON_ERROR_UTF16] = [
81
                'message'    => 'Malformed UTF-16 characters',
82
                'error-code' => 10,
83
            ];
84
        }
85
86
        return $collections;
87
    }
88
}
89