|
1
|
|
|
<?php |
|
2
|
1 |
|
include_once("xnop.class.php"); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Class jsonHelper |
|
6
|
|
|
*/ |
|
7
|
|
|
class jsonHelper |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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()) |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
133
|
|
|
$new_json .= ",\n" . str_repeat($tab, $indent_level); |
|
134
|
|
|
} else { |
|
135
|
|
|
$new_json .= $char; |
|
136
|
|
|
} |
|
137
|
|
|
break; |
|
138
|
|
|
case ':': |
|
139
|
|
|
if (!$in_string) { |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
// no break ??? |
|
150
|
|
|
default: |
|
151
|
|
|
$new_json .= $char; |
|
152
|
|
|
break; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
return $new_json; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
} |
|
160
|
|
|
|
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.