{"id":15061242,"url":"https://github.com/open-code-modeling/json-schema-to-php-ast","last_synced_at":"2025-04-10T06:40:05.430Z","repository":{"id":48232421,"uuid":"306610687","full_name":"open-code-modeling/json-schema-to-php-ast","owner":"open-code-modeling","description":"Provides factories to create PhpParser node visitors or PHP Code AST class builder objects from JSON schema e. g. value objects","archived":false,"fork":false,"pushed_at":"2024-11-06T19:26:53.000Z","size":315,"stargazers_count":10,"open_issues_count":1,"forks_count":0,"subscribers_count":0,"default_branch":"0.6.x","last_synced_at":"2025-03-24T07:41:29.976Z","etag":null,"topics":["abstract-syntax-tree","ast","code-generation","code-generator","json","json-schema","php","php-ast","schema","value-object"],"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/open-code-modeling.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-10-23T11:04:27.000Z","updated_at":"2024-07-28T21:40:42.000Z","dependencies_parsed_at":"2022-08-23T20:50:15.471Z","dependency_job_id":null,"html_url":"https://github.com/open-code-modeling/json-schema-to-php-ast","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/open-code-modeling%2Fjson-schema-to-php-ast","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/open-code-modeling%2Fjson-schema-to-php-ast/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/open-code-modeling%2Fjson-schema-to-php-ast/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/open-code-modeling%2Fjson-schema-to-php-ast/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/open-code-modeling","download_url":"https://codeload.github.com/open-code-modeling/json-schema-to-php-ast/tar.gz/refs/heads/0.6.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247773743,"owners_count":20993633,"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":["abstract-syntax-tree","ast","code-generation","code-generator","json","json-schema","php","php-ast","schema","value-object"],"created_at":"2024-09-24T23:12:24.829Z","updated_at":"2025-04-10T06:40:05.385Z","avatar_url":"https://github.com/open-code-modeling.png","language":"PHP","readme":"# JSON Schema to PHP AST\n\nCompiles a JSON schema to PHP classes / value objects via PHP AST.\n\n## Installation\n\n```bash\n$ composer require open-code-modeling/json-schema-to-php-ast --dev\n```\n\n## Usage\n\n\u003e See unit tests in `tests` folder for comprehensive examples.\n\nYou can use each value object factory to compose your value object with PHP AST node visitors or high level builder API.\nThe easiest way to use this library is the `ValueObjectFactory`.\n\nAssume you have the following JSON schema\n```json\n{\n    \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n    \"definitions\": {\n        \"address\": {\n            \"type\": \"object\",\n            \"properties\": {\n                \"street_address\": {\n                    \"type\": \"string\"\n                },\n                \"city\": {\n                    \"type\": [\"string\", \"null\"]\n                },\n                \"federal_state\": {\n                    \"$ref\": \"#/definitions/state\"\n                }\n            },\n            \"required\": [\n                \"street_address\",\n                \"city\",\n                \"federal_state\"\n            ]\n        },\n        \"state\": {\n            \"type\": \"string\",\n            \"enum\": [\"NY\", \"DC\"]\n        }\n    },\n    \"type\": \"object\",\n    \"properties\": {\n        \"billing_address\": {\n            \"$ref\": \"#/definitions/address\"\n        },\n        \"shipping_addresses\": {\n            \"type\": \"array\",\n            \"items\": {\n                \"$ref\": \"#/definitions/address\"\n            }\n        }\n    },\n    \"required\": [\n        \"billing_address\"\n    ]\n}\n```\n\nThen you can use the `ValueObjectFactory` to generate PHP code for the following classes:\n- `Order`\n- `ShippingAddresses`\n- `Address`\n- `StreetAddress`\n- `City`\n- `State`\n\n```php\n\u003c?php\n\nuse OpenCodeModeling\\CodeAst\\Builder\\FileCollection;\nuse OpenCodeModeling\\CodeAst\\Package\\ClassInfoList;\nuse OpenCodeModeling\\CodeAst\\Package\\Psr4Info;\nuse OpenCodeModeling\\Filter\\FilterFactory;\nuse OpenCodeModeling\\JsonSchemaToPhp\\Type\\Type;\nuse OpenCodeModeling\\JsonSchemaToPhpAst\\ValueObjectFactory;\n\n$parser = (new PhpParser\\ParserFactory())-\u003ecreate(PhpParser\\ParserFactory::ONLY_PHP7);\n$printer = new PhpParser\\PrettyPrinter\\Standard(['shortArraySyntax' =\u003e true]);\n\n// configure your Composer info\n// FilterFactory of library open-code-modeling/php-filter is used for sake of brevity\n$classInfoList = new ClassInfoList(\n    ...Psr4Info::fromComposer(\n        'src/',\n        file_get_contents('composer.json'),\n        FilterFactory::directoryToNamespaceFilter(),\n        FilterFactory::namespaceToDirectoryFilter(),\n    )\n);\n\n$valueObjectFactory = new ValueObjectFactory(\n    $classInfoList,\n    $parser,\n    $printer,\n    true,\n    FilterFactory::classNameFilter(),\n    FilterFactory::propertyNameFilter(),\n    FilterFactory::methodNameFilter(),\n    FilterFactory::constantNameFilter(),\n    FilterFactory::constantValueFilter()\n);\n\n// $json contains the json string from above\n$decodedJson = \\json_decode($json, true, 512, \\JSON_BIGINT_AS_STRING | \\JSON_THROW_ON_ERROR);\n\n$typeSet = Type::fromDefinition($decodedJson);\n$srcFolder = 'tmp/';\n\n$fileCollection = FileCollection::emptyList();\n$classBuilder = ClassBuilder::fromScratch('Order', 'YourNamespaceFromComposer')-\u003esetFinal(true);\n\n$valueObjectFactory-\u003egenerateClasses($classBuilder, $fileCollection, $typeSet, $srcFolder);\n\n// $fileCollection contains 6 classes\n\n// now let's add constants and getter methods of properties for non value objects\n$valueObjectFactory-\u003eaddGetterMethodsForProperties($fileCollection, true);\n$valueObjectFactory-\u003eaddClassConstantsForProperties($fileCollection);\n\n// generate PHP code\n$files = $valueObjectFactory-\u003egenerateFiles($fileCollection);\n\nforeach ($files as $filename =\u003e $code) {\n    // store PHP code to filesystem\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopen-code-modeling%2Fjson-schema-to-php-ast","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopen-code-modeling%2Fjson-schema-to-php-ast","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopen-code-modeling%2Fjson-schema-to-php-ast/lists"}