如果你(像我一样)在这里寻找一个用于数组->类的源代码生成器,我并没有真正找到任何东西,然后我想出了这个(正在进行中,没有经过充分测试或其他任何事情,json_decode返回数组。):
<?php
declare(strict_types = 1);
$json = <<<'JSON'
{"object_kind":"push","event_name":"push","before":"657dbca6668a99012952c58e8c8072d338b48d20","after":"5ac3eda70dbb44bfdf98a3db87515864036db0f9","ref":"refs/heads/master","checkout_sha":"5ac3eda70dbb44bfdf98a3db87515864036db0f9","message":null,"user_id":805411,"user_name":"hanshenrik","user_email":"divinity76@gmail.com","user_avatar":"https://secure.gravatar.com/avatar/e3af2bd4b5604b0b661b5e6646544eba?s=80\u0026d=identicon","project_id":3498684,"project":{"name":"gitlab_integration_tests","description":"","web_url":"https://gitlab.com/divinity76/gitlab_integration_tests","avatar_url":null,"git_ssh_url":"git@gitlab.com:divinity76/gitlab_integration_tests.git","git_http_url":"https://gitlab.com/divinity76/gitlab_integration_tests.git","namespace":"divinity76","visibility_level":0,"path_with_namespace":"divinity76/gitlab_integration_tests","default_branch":"master","homepage":"https://gitlab.com/divinity76/gitlab_integration_tests","url":"git@gitlab.com:divinity76/gitlab_integration_tests.git","ssh_url":"git@gitlab.com:divinity76/gitlab_integration_tests.git","http_url":"https://gitlab.com/divinity76/gitlab_integration_tests.git"},"commits":[{"id":"5ac3eda70dbb44bfdf98a3db87515864036db0f9","message":"dsf\n","timestamp":"2017-06-14T02:21:50+02:00","url":"https://gitlab.com/divinity76/gitlab_integration_tests/commit/5ac3eda70dbb44bfdf98a3db87515864036db0f9","author":{"name":"hanshenrik","email":"divinity76@gmail.com"},"added":[],"modified":["gitlab_callback_page.php"],"removed":[]}],"total_commits_count":1,"repository":{"name":"gitlab_integration_tests","url":"git@gitlab.com:divinity76/gitlab_integration_tests.git","description":"","homepage":"https://gitlab.com/divinity76/gitlab_integration_tests","git_http_url":"https://gitlab.com/divinity76/gitlab_integration_tests.git","git_ssh_url":"git@gitlab.com:divinity76/gitlab_integration_tests.git","visibility_level":0}}
JSON;
$arr = json_decode ( $json, true );
var_dump ( array_to_class ( $arr ) );
function array_to_class(array $arr, string $top_class_name = "TopClass"): string {
$top_class_name = ucfirst ( $top_class_name );
$classes = array ();
$internal = function (array $arr, string $top_class_name) use (&$classes, &$internal) {
$curr = 'Class ' . $top_class_name . ' {' . "\n";
foreach ( $arr as $key => $val ) {
$type = gettype ( $val );
if (is_array ( $val )) {
$type = ucfirst ( ( string ) $key );
$classes [$internal ( $val, ( string ) $key )] = true;
}
$curr .= <<<FOO
/**
* @property $type \$$key
*/
FOO;
$curr .= "\n public $" . $key . ";\n";
}
$curr .= '}';
$classes [$curr] = true;
};
$internal ( $arr, $top_class_name );
return implode ( "\n", array_keys ( $classes ) );
}
输出:
Class project {
public $name;
public $description;
public $web_url;
public $avatar_url;
public $git_ssh_url;
public $git_http_url;
public $namespace;
public $visibility_level;
public $path_with_namespace;
public $default_branch;
public $homepage;
public $url;
public $ssh_url;
public $http_url;
}
Class author {
public $name;
public $email;
}
Class added {
}
Class modified {
public $0;
}
Class removed {
}
Class 0 {
public $id;
public $message;
public $timestamp;
public $url;
public $author;
public $added;
public $modified;
public $removed;
}
Class commits {
public $0;
}
Class repository {
public $name;
public $url;
public $description;
public $homepage;
public $git_http_url;
public $git_ssh_url;
public $visibility_level;
}
Class TopClass {
public $object_kind;
public $event_name;
public $before;
public $after;
public $ref;
public $checkout_sha;
public $message;
public $user_id;
public $user_name;
public $user_email;
public $user_avatar;
public $project_id;
public $project;
public $commits;
public $total_commits_count;
public $repository;
}