Skip to content

Commit

Permalink
完善json数据到php类文件生成工具的细节,主要是注解中对类型的判断方面
Browse files Browse the repository at this point in the history
  • Loading branch information
web3d committed Jan 8, 2016
1 parent 73f9f84 commit 0c18489
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
18 changes: 10 additions & 8 deletions examples/JSONObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ public function toClass($className, array $json_array) {

if (count($json_array) > 0) {
foreach ($json_array as $key => $value) {
if (is_int($value)) {
$type = 'int';
} elseif (is_string($value)) {
$type = 'string';
} elseif (is_bool($value)) {
$type = 'string';
$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 {
$type = 'mixed';
$_value = '';
}

$str .= " /**\n";
$str .= " * @var {$type} \${$key}'{$value}'\n";
$str .= " * @var {$type} \${$key} {$_value}\n";
$str .= " */\n";

$str .= " public \${$key};\n\n";
Expand Down
34 changes: 23 additions & 11 deletions examples/convert.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,29 @@
$jo = new JSONObject;

$json_str = '{
"id": 2,
"name": "钉钉事业部",
"order" : 10,
"parentid": 1,
"createDeptGroup": true,
"autoAddUser": true,
"deptHiding" : true,
"deptPerimits" : "3|4",
"orgDeptOwner" : "manager1122",
"deptManagerUseridList" : "manager1122|manager3211"
"userid": "zhangsan",
"name": "张三",
"tel" : "010-123333",
"workPlace" :"",
"remark" : "",
"mobile" : "13800000000",
"email" : "[email protected]",
"active" : true,
"orderInDepts" : "{1:10, 2:20}",
"isAdmin" : false,
"isBoss" : false,
"dingId" : "WsUDaq7DCVIHc6z1GAsYDSA",
"isLeaderInDepts" : "{1:true, 2:false}",
"isHide" : false,
"department": [1, 2],
"position": "工程师",
"avatar": "dingtalk.com/abc.jpg",
"jobnumber": "111111",
"extattr": {
"爱好":"旅游",
"年龄":"24"
}
}';


echo $jo->toClass('Department', json_decode($json_str, true));
echo $jo->toClass('User', json_decode($json_str, true));

0 comments on commit 0c18489

Please sign in to comment.