{"id":18994065,"url":"https://github.com/swaggest/php-code-builder","last_synced_at":"2025-05-16T09:00:23.663Z","repository":{"id":44800363,"uuid":"89675549","full_name":"swaggest/php-code-builder","owner":"swaggest","description":"JSON Schema enabled PHP code building abstraction for PHP","archived":false,"fork":false,"pushed_at":"2025-02-18T12:48:41.000Z","size":578,"stargazers_count":74,"open_issues_count":4,"forks_count":20,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-12T14:18:56.978Z","etag":null,"topics":["code-generator","json-schema","php"],"latest_commit_sha":null,"homepage":"","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/swaggest.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2017-04-28T06:35:56.000Z","updated_at":"2025-02-18T12:48:13.000Z","dependencies_parsed_at":"2023-12-18T16:09:46.268Z","dependency_job_id":"0f392b48-5b83-49a7-8a0b-721e63a141be","html_url":"https://github.com/swaggest/php-code-builder","commit_stats":{"total_commits":141,"total_committers":13,"mean_commits":"10.846153846153847","dds":0.574468085106383,"last_synced_commit":"7720eb5ffae2e33937688066ee5aab83476b907a"},"previous_names":[],"tags_count":49,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swaggest%2Fphp-code-builder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swaggest%2Fphp-code-builder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swaggest%2Fphp-code-builder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swaggest%2Fphp-code-builder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/swaggest","download_url":"https://codeload.github.com/swaggest/php-code-builder/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254501547,"owners_count":22081526,"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":["code-generator","json-schema","php"],"created_at":"2024-11-08T17:24:03.564Z","updated_at":"2025-05-16T09:00:23.351Z","avatar_url":"https://github.com/swaggest.png","language":"PHP","readme":"# Swaggest JSON-schema enabled PHP code builder\n\n[![Build Status](https://travis-ci.org/swaggest/php-code-builder.svg?branch=master)](https://travis-ci.org/swaggest/php-code-builder)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/swaggest/php-code-builder/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/swaggest/php-code-builder/?branch=master)\n[![Code Climate](https://codeclimate.com/github/swaggest/php-code-builder/badges/gpa.svg)](https://codeclimate.com/github/swaggest/php-code-builder)\n[![codecov](https://codecov.io/gh/swaggest/php-code-builder/branch/master/graph/badge.svg)](https://codecov.io/gh/swaggest/php-code-builder)\n\nThis library generates PHP mapping structures defined by [JSON schema](http://json-schema.org/)\nusing [`swaggest/json-schema`](https://github.com/swaggest/php-json-schema).\n\n## Example\n\n[Generated code](tests/src/Tmp/Example)\n\nYou need to add `swaggest/json-schema` to your dependencies.\n\n```php\n\u003c?php\n\nrequire_once __DIR__ . '/vendor/autoload.php';\n\n$schemaData = json_decode(\u003c\u003c\u003c'JSON'\n{\n    \"type\": \"object\",\n    \"properties\": {\n        \"id\": {\"type\": \"integer\"},\n        \"name\": {\"type\": \"string\"},\n        \"parent\": {\"$ref\": \"#\"},\n        \"children\": {\"type\": \"array\", \"items\": {\"$ref\": \"#\"}},\n        \"info\": {\"$ref\": \"#/definitions/info\"}\n    },\n    \"definitions\": {\n        \"info\": {\n            \"type\": \"object\",\n            \"properties\": {\n                \"lastName\": {\"type\": \"string\"},\n                \"birthDate\": {\"type\": \"string\", \"format\": \"date-time\"},\n                \"options\": {\"$ref\": \"#/definitions/options\"}\n            }\n        },\n        \"options\": {\n            \"type\": \"object\",\n            \"properties\": {\n                \"rememberSession\": {\"type\": \"boolean\"},\n                \"allowNotifications\": {\"type\": \"boolean\"}\n            }\n        }\n    }\n}\nJSON\n);\n\n$swaggerSchema = \\Swaggest\\JsonSchema\\Schema::import($schemaData);\n\n$appPath = realpath(__DIR__ . '/tests/src/Tmp') . '/Example';\n$appNs = 'Swaggest\\PhpCodeBuilder\\Tests\\Tmp\\Example';\n\n$app = new \\Swaggest\\PhpCodeBuilder\\App\\PhpApp();\n$app-\u003esetNamespaceRoot($appNs, '.');\n\n$builder = new \\Swaggest\\PhpCodeBuilder\\JsonSchema\\PhpBuilder();\n$builder-\u003ebuildSetters = true;\n$builder-\u003emakeEnumConstants = true;\n\n$builder-\u003eclassCreatedHook = new \\Swaggest\\PhpCodeBuilder\\JsonSchema\\ClassHookCallback(\n    function (\\Swaggest\\PhpCodeBuilder\\PhpClass $class, $path, $schema) use ($app, $appNs) {\n        $desc = '';\n        if ($schema-\u003etitle) {\n            $desc = $schema-\u003etitle;\n        }\n        if ($schema-\u003edescription) {\n            $desc .= \"\\n\" . $schema-\u003edescription;\n        }\n        if ($fromRefs = $schema-\u003egetFromRefs()) {\n            $desc .= \"\\nBuilt from \" . implode(\"\\n\" . ' \u003c- ', $fromRefs);\n        }\n\n        $class-\u003esetDescription(trim($desc));\n\n        $class-\u003esetNamespace($appNs);\n        if ('#' === $path) {\n            $class-\u003esetName('User'); // Class name for root schema\n        } elseif (strpos($path, '#/definitions/') === 0) {\n            $class-\u003esetName(\\Swaggest\\PhpCodeBuilder\\PhpCode::makePhpClassName(\n                substr($path, strlen('#/definitions/'))));\n        }\n        $app-\u003eaddClass($class);\n    }\n);\n\n$builder-\u003egetType($swaggerSchema);\n$app-\u003eclearOldFiles($appPath);\n$app-\u003estore($appPath);\n```\n\nCreating and exporting an instance\n\n```php\n$user = new \\Swaggest\\PhpCodeBuilder\\Tests\\Tmp\\Example\\User();\n$user-\u003ename = \"John\";\n$user-\u003einfo = (new \\Swaggest\\PhpCodeBuilder\\Tests\\Tmp\\Example\\Info())\n    -\u003esetLastName(\"Doe\")\n    -\u003esetBirthDate(\"1980-01-01\")\n    -\u003esetOptions(\n        (new \\Swaggest\\PhpCodeBuilder\\Tests\\Tmp\\Example\\Options())\n            -\u003esetRememberSession(true)\n            -\u003esetAllowNotifications(false)\n    );\n\n// No exception on exporting valid data\n$jsonData = \\Swaggest\\PhpCodeBuilder\\Tests\\Tmp\\Example\\User::export($user);\n\n// {\"name\":\"John\",\"info\":{\"lastName\":\"Doe\",\"birthDate\":\"1980-01-01\",\"options\":{\"rememberSession\":true,\"allowNotifications\":false}}}\necho json_encode($jsonData);\n\n// Setting invalid value (integer instead of string)\n$user-\u003ename = 123;\n\n// Exception: String expected, 123 received at #-\u003e$ref[#]-\u003eproperties:name\n$jsonData = \\Swaggest\\PhpCodeBuilder\\Tests\\Tmp\\Example\\User::export($user);\n```\n\nCreating class instance from raw data\n\n```php\n// Importing raw data to entity class instance will do validation and mapping\n$user = \\Swaggest\\PhpCodeBuilder\\Tests\\Tmp\\Example\\User::import(\n    json_decode('{\"name\":\"John\",\"info\":{\"lastName\":\"Doe\",\"birthDate\":\"1980-01-01\",\"options\":{\"rememberSession\":true,\"allowNotifications\":false}}}')\n);\n\nvar_dump($user-\u003einfo-\u003eoptions-\u003eallowNotifications); // bool(false)\n```\n\n## CLI Tool\n\nYou can use [json-cli](https://github.com/swaggest/json-cli#genphp) to render JSON Schema into PHP classes from command line.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswaggest%2Fphp-code-builder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fswaggest%2Fphp-code-builder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswaggest%2Fphp-code-builder/lists"}