jsonHelper::jsonDecode()   B
last analyzed

Complexity

Conditions 9
Paths 36

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
eloc 21
c 0
b 0
f 0
dl 0
loc 32
ccs 0
cts 23
cp 0
rs 8.0555
cc 9
nc 36
nop 3
crap 90
1
<?php
2 1
include_once("xnop.class.php");
3
4
/**
5
 * Class jsonHelper
6
 */
7
class jsonHelper
0 ignored issues
show
Coding Style introduced by
Class name "jsonHelper" is not in PascalCase format

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
8
{
9
    /**
10
     * @var array
11
     */
12
    protected static $_error = array(
13
        0 => 'error_none', //JSON_ERROR_NONE
14
        1 => 'error_depth', //JSON_ERROR_DEPTH
15
        2 => 'error_state_mismatch', //JSON_ERROR_STATE_MISMATCH
16
        3 => 'error_ctrl_char', //JSON_ERROR_CTRL_CHAR
17
        4 => 'error_syntax', //JSON_ERROR_SYNTAX
18
        5 => 'error_utf8' //SON_ERROR_UTF8
19
    );
20
21
    /**
22
     * Разбор JSON строки при помощи json_decode
23
     *
24
     * @param $json string строка c JSON
25
     * @param array $config ассоциативный массив с настройками для json_decode
26
     * @param bool $nop создавать ли пустой объект запрашиваемого типа
27
     * @return array|mixed|xNop
28
     */
29
    public static function jsonDecode($json, $config = array(), $nop = false)
30
    {
31
        if (isset($config['assoc'])) {
32
            $assoc = (boolean)$config['assoc'];
33
        } else {
34
            $assoc = false;
35
        }
36
37
        if (isset($config['depth']) && (int)$config['depth'] > 0) {
38
            $depth = (int)$config['depth'];
39
        } else {
40
            $depth = 512;
41
        }
42
        if (is_scalar($json)) {
43
            if (version_compare(phpversion(), '5.3.0', '<')) {
44
                $out = json_decode($json, $assoc);
45
            } else {
46
                $out = json_decode($json, $assoc, $depth);
47
            }
48
        } else {
49
            $out = null;
50
        }
51
52
        if ($nop && is_null($out)) {
53
            if ($assoc) {
54
                $out = array();
55
            } else {
56
                $out = new xNop();
57
            }
58
        }
59
60
        return $out;
61
    }
62
63
    /**
64
     * Получение кода последенй ошибки
65
     * @see http://www.php.net/manual/ru/function.json-last-error-msg.php
66
     * @return string
67
     */
68
    public static function json_last_error_msg()
0 ignored issues
show
Coding Style introduced by
Method name "jsonHelper::json_last_error_msg" is not in camel caps format
Loading history...
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
69
    {
70
        if (function_exists('json_last_error')) {
71
            $error = json_last_error();
72
        } else {
73
            $error = 999;
74
        }
75
76
        return isset(self::$_error[$error]) ? self::$_error[$error] : 'other';
77
    }
78
79
    /**
80
     * @param array $data
81
     * @return bool|string
82
     */
83
    public static function toJSON(array $data = array())
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
84
    {
85
        if (version_compare(PHP_VERSION, '5.4.0') < 0) {
86
            $out = json_encode($data);
87
            $out = str_replace('\\/', '/', $out);
88
        } else {
89
            $out = json_encode($data, JSON_UNESCAPED_SLASHES);
90
        }
91
92
        return self::json_format($out);
93
    }
94
95
    /**
96
     * @param $json
97
     * @return bool|string
98
     */
99
    public static function json_format($json)
0 ignored issues
show
Coding Style introduced by
Function's nesting level (4) exceeds 3; consider refactoring the function
Loading history...
Coding Style introduced by
Method name "jsonHelper::json_format" is not in camel caps format
Loading history...
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
100
    {
101
        $tab = "  ";
102
        $new_json = "";
103
        $indent_level = 0;
104
        $in_string = false;
105
        $json_obj = json_decode($json);
106
        if ($json_obj === false) {
107
            return false;
108
        }
109
        $len = strlen($json);
110
        for ($c = 0; $c < $len; $c++) {
111
            $char = $json[$c];
112
            switch ($char) {
113
                case '{':
114
                case '[':
115
                    if (!$in_string) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
Loading history...
116
                        $new_json .= $char . "\n" . str_repeat($tab, $indent_level + 1);
117
                        $indent_level++;
118
                    } else {
119
                        $new_json .= $char;
120
                    }
121
                    break;
122
                case '}':
123
                case ']':
124
                    if (!$in_string) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
Loading history...
125
                        $indent_level--;
126
                        $new_json .= "\n" . str_repeat($tab, $indent_level) . $char;
127
                    } else {
128
                        $new_json .= $char;
129
                    }
130
                    break;
131
                case ',':
132
                    if (!$in_string) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
Loading history...
133
                        $new_json .= ",\n" . str_repeat($tab, $indent_level);
134
                    } else {
135
                        $new_json .= $char;
136
                    }
137
                    break;
138
                case ':':
139
                    if (!$in_string) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
Loading history...
140
                        $new_json .= ": ";
141
                    } else {
142
                        $new_json .= $char;
143
                    }
144
                    break;
145
                case '"':
146
                    if ($c > 0 && $json[$c - 1] != '\\') {
147
                        $in_string = !$in_string;
0 ignored issues
show
introduced by
The condition $in_string is always false.
Loading history...
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
Loading history...
148
                    }
149
                    // no break ???
150
                default:
151
                    $new_json .= $char;
152
                    break;
153
            }
154
        }
155
156
        return $new_json;
157
    }
158
159
}
160