{"id":22248290,"url":"https://github.com/yunlyz/json2php","last_synced_at":"2025-03-25T11:43:19.801Z","repository":{"id":62520570,"uuid":"146538973","full_name":"yunlyz/json2php","owner":"yunlyz","description":"Generate php files(PHP class) based on json","archived":false,"fork":false,"pushed_at":"2018-09-21T04:34:26.000Z","size":22,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-30T10:43:08.659Z","etag":null,"topics":["json","json2php","php","php-files","yii"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yunlyz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-29T03:18:53.000Z","updated_at":"2021-07-12T07:31:48.000Z","dependencies_parsed_at":"2022-11-02T10:31:50.959Z","dependency_job_id":null,"html_url":"https://github.com/yunlyz/json2php","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yunlyz%2Fjson2php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yunlyz%2Fjson2php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yunlyz%2Fjson2php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yunlyz%2Fjson2php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yunlyz","download_url":"https://codeload.github.com/yunlyz/json2php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245458114,"owners_count":20618693,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json","json2php","php","php-files","yii"],"created_at":"2024-12-03T06:14:55.097Z","updated_at":"2025-03-25T11:43:19.770Z","avatar_url":"https://github.com/yunlyz.png","language":"PHP","readme":"# json2php\n\n## Introduction\nGenerate php files(PHP class) based on json\n\n## Installation\nVia `Composer`:\n```shell\ncomposer require nofuck/json2php \"*\"\n```\n\n## Usage\nUsage is very easy. Can easily generate a PHP file:\n```php\n$namespace = 'tests\\\\output\\\\general';\n$output = TEST_ROOT . '/output/general/';\nif (!is_dir($output)) {\n    mkdir($output);\n}\n\n$g = new GeneratorPhpFile();\n$jsonPath = TEST_ROOT . '/json/normal.json';\n$g-\u003egenerator(file_get_contents($jsonPath), 'Normal', $namespace, $output);\n```\n\nIf you want to generate a model of Yii2:\n```php\n$namespace = 'tests\\\\output\\\\yii2';\n$output = TEST_ROOT . '/output/yii2/';\nif (!is_dir($output)) {\n    mkdir($output);\n}\n\n$g = new GeneratorYiiModel();\n$jsonPath = TEST_ROOT . '/json/normal.json';\n$g-\u003egenerator(file_get_contents($jsonPath), 'Normal', $namespace, $output);\n```\n\nIt’s that simple.\n\n`generator` Method accepts four parameters:\n- json \n- className\n- namespace\n- phpOut\n\n### Console usage\n```shell\n12:09:05 › ./json2php -h\nDescription:\n  Lists commands\n\nUsage:\n  list [options] [--] [\u003cnamespace\u003e]\n\nArguments:\n  namespace            The namespace name\n\nOptions:\n      --raw            To output raw command list\n      --format=FORMAT  The output format (txt, xml, json, or md) [default: \"txt\"]\n\nHelp:\n  The list command lists all commands:\n\n    php ./json2php list\n\n  You can also display the commands for a specific namespace:\n\n    php ./json2php list test\n\n  You can also output the information in other formats by using the --format option:\n\n    php ./json2php list --format=xml\n\n  It's also possible to get raw list of commands (useful for embedding command runner):\n\n    php ./json2php list --raw\n```\n\nThere are currently two generators available, one for generating generic PHP files and the other for generating files in the Yii2 model form:\n```bash\ngenerator:default  [g:d] Generate a generic php object file containing properties, get and set methods\ngenerator:yii2     [g:yii2] Generate a generic php object file containing properties, get and set methods\n```\n\n```bash\n12:12:14 › ./json2php g:d -h\nDescription:\n  Generate a generic php object file containing properties, get and set methods\n\nUsage:\n  generator:default [options] [--] \u003cjson\u003e\n  g:d\n\nArguments:\n  json                           Json file path or json string\n\nOptions:\n  -t, --type[=TYPE]              Json source, file or json string, value in [file | string] [default: \"file\"]\n  -c, --class_name[=CLASS_NAME]  Php class name, the default value is Default [default: \"Generator1536207140\"]\n  -o, --php_out[=PHP_OUT]        Generate php file storage path, default to current directory [default: \"./\"]\n  -h, --help                     Display this help message\n  -q, --quiet                    Do not output any message\n  -V, --version                  Display this application version\n      --ansi                     Force ANSI output\n      --no-ansi                  Disable ANSI output\n  -n, --no-interaction           Do not ask any interactive question\n  -ns, --namespace[=NAMESPACE]   Php namespace, please ensure the correctness of the input namespace\n  -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug\n\nHelp:\n  This command can generate a generic php object\n```\n\n```bash\n./json2php g:d -t string '{\"name\": \"lvyun\"}' --namespace \"test\\\\lvyun\" -c UserInfo -o .\n```\n\nOutput`UserInfo.php`:\n```php\n\u003c?php\n\n/**\n * Code generated by json2php. DO NOT EDIT.\n * Generator at 2018-09-06 04:15:27\n */\n\nnamespace test\\lvyun;\n\nclass UserInfo\n{\n    /** @var string */\n    private $name;\n\n    /**\n     * @return string\n     */\n    public function getName()\n    {\n        return $this-\u003ename;\n    }\n\n    /**\n     * @param $name string\n     */\n    public function setName($name)\n    {\n        $this-\u003ename = $name;\n    }\n}\n\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyunlyz%2Fjson2php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyunlyz%2Fjson2php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyunlyz%2Fjson2php/lists"}