-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathJSONObject.php
49 lines (39 loc) · 1.27 KB
/
JSONObject.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
/*
* Created Datetime:2016-1-8 20:53:52
* Creator:Jimmy Jaw <[email protected]>
* Copyright:TimeCheer Inc. 2016-1-8
*
*/
/**
* 将json数据字符串转换为php class类
*
* @author Jimmy Jaw <[email protected]>
*/
class JSONObject {
public function toClass($className, array $json_array) {
$str = "<?php \n";
$str .= "\n";
$str .= "class {$className} {/** created by JSONObject */\n\n";
if (count($json_array) > 0) {
foreach ($json_array as $key => $value) {
$type = gettype($value);
if ($type == 'boolean') {
$_value = $value ? 'true' : 'false';
} elseif ($type == 'string') {
$_value = "'{$value}'";
} elseif (!in_array($type, array('object', 'array'))) {
$_value = $value;
} else {
$_value = '';
}
$str .= " /**\n";
$str .= " * @var {$type} \${$key} 如 {$_value}\n";
$str .= " */\n";
$str .= " public \${$key};\n\n";
}
}
$str .= '}';
return $str;
}
}