Download the PHP package guuzen/json-schema-codegen without Composer
On this page you can find all versions of the php package guuzen/json-schema-codegen. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download guuzen/json-schema-codegen
More information about guuzen/json-schema-codegen
Files in guuzen/json-schema-codegen
Package json-schema-codegen
Short Description Project to generate PHP DTOs from JSON Schema
License MIT
Informations about the package json-schema-codegen
The Problem
When your application communicates with external systems - APIs, message queues, event streams - you typically define data contracts using JSON Schema. On the PHP side, you represent those contracts as DTO classes with typed properties.
The problem is keeping both in sync. Every time the schema changes, you have to manually update the corresponding PHP class: rename properties, adjust types, add or remove fields. This is repetitive, error-prone, and easy to forget. The schema and the code silently drift apart, and type mismatches only surface at runtime.
The Solution
JSON Schema Codegen generates PHP DTO classes directly from your JSON Schema files. Run the generator after any schema change and your PHP classes are always up to date - no manual editing required.
The generator maps JSON Schema types to precise PHPDoc annotations that static analysis tools like PHPStan understand:
"type": "string"→@var string"type": "string", "minLength": 1→@var non-empty-string"type": "integer", "minimum": 0, "maximum": 100"→@var int<0, 100>"type": ["string", "null"]→@var string|null"type": "array", "items": {"type": "string"}→@var list<string>"oneOf": [{"$ref": "A.json"}, {"$ref": "B.json"}]→@var A|B"$ref": "Other.json"→@var Other
Requirements
- PHP 8.4+
nette/php-generator: ^4.2- used by the built-in generator implementationsymfony/yaml: ^8.0- optional, required only for YAML schema support
Installation
Quick Start
Create a PHP script that configures and runs the generator:
Run it with php generate.php whenever your schemas change.
Configuration
| Option | Type | Description |
|---|---|---|
baseNamespace |
string |
Root namespace for all generated classes |
schemaPath |
string |
Absolute path to the directory containing your schema files |
outputPath |
string |
Absolute path to the directory where generated PHP files are written |
schemaSuffix |
string |
File extension used to identify schema files; also selects the decoder (.json or .yaml) |
undefinedPath |
string |
Schema path (relative to schemaPath) representing an absent value, referenced by non-required properties so absence is distinguishable from null |
typeMappings |
array<string, class-string> |
Optional. Maps a schema path (relative to schemaPath) to an existing class to reference instead of generating, e.g. ['DateTimeImmutable.json' => DateTimeImmutable::class] |
Schema files in subdirectories of schemaPath produce classes in sub-namespaces. For example, schemas/billing/Address.json generates App\Dto\billing\Address.
Example
Given this schema at schemas/Product.json:
The generator produces output/Product.php:
Cross-file references via $ref are also resolved. Given schemas/Order.json:
The generator produces:
YAML Support
YAML schema files are supported out of the box. Install symfony/yaml and set schemaSuffix to .yaml - create() selects the matching decoder from the suffix automatically:
For full control, use NetteFilesGeneratorFactory::assemble(...) directly.