PHPackages                             guuzen/json-schema-codegen - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. guuzen/json-schema-codegen

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

guuzen/json-schema-codegen
==========================

Project to generate PHP DTOs from JSON Schema

0.3.0(1w ago)01↓87.5%MITPHPPHP &gt;=8.4 &lt;8.6CI passing

Since Apr 17Pushed 4d agoCompare

[ Source](https://github.com/Guuzen/json-schema-codegen)[ Packagist](https://packagist.org/packages/guuzen/json-schema-codegen)[ RSS](/packages/guuzen-json-schema-codegen/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (15)Versions (5)Used By (0)

The Problem
-----------

[](#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
------------

[](#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`
- `"type": ["string", "null"]` → `@var string|null`
- `"type": "array", "items": {"type": "string"}` → `@var list`
- `"oneOf": [{"$ref": "A.json"}, {"$ref": "B.json"}]` → `@var A|B`
- `"$ref": "Other.json"` → `@var Other`

Requirements
------------

[](#requirements)

- PHP 8.4+
- `nette/php-generator: ^4.2` - used by the built-in generator implementation
- `symfony/yaml: ^8.0` - optional, required only for YAML schema support

Installation
------------

[](#installation)

```
composer require guuzen/json-schema-codegen
composer require nette/php-generator
```

Quick Start
-----------

[](#quick-start)

Create a PHP script that configures and runs the generator:

```
