zero-to-prod / data-model-generator
Builds DataModels from a Schema
Fund package maintenance!
Github
Installs: 1 349
Dependents: 2
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 1
pkg:composer/zero-to-prod/data-model-generator
Requires
- php: >=8.1.0
- zero-to-prod/data-model: ^81.12
- zero-to-prod/data-model-factory: ^71.3.2
- zero-to-prod/data-model-helper: ^82.0.4
- zero-to-prod/file: ^1.0.2
- zero-to-prod/package-helper: ^1.1.3
Requires (Dev)
- phpunit/phpunit: ^10.0
Suggests
- zero-to-prod/data-model: Transform Data to Type-Safe DTOs
- zero-to-prod/data-model-helper: Helpers for a DataModel.
- dev-main
- v2.3.7
- v2.3.6
- v2.3.5
- v2.3.4
- v2.3.3
- v2.3.2
- v2.3.1
- v2.3.0
- v2.2.3
- v2.2.2
- v2.2.1
- v2.2.0
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.x-dev
- v0.1.18
- v0.1.17
- v0.1.16
- v0.1.15
- v0.1.14
- v0.1.13
- v0.1.12
- v0.1.11
- v0.1.10
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- dev-bugfix/add-data-model-adapter-swagger-dep
This package is auto-updated.
Last update: 2026-02-10 19:13:29 UTC
README
Generates PHP classes and enums from OpenAPI 3.0 and Swagger 2.0 schemas.
Reads your API schema and a configuration file, then outputs fully-typed PHP classes with configurable visibility, readonly modifiers, constants, comments, and type mappings.
Contents
Installation
Install the package via composer:
composer require zero-to-prod/data-model-generator
Requires PHP 8.1 or higher.
Usage
Initialize Configuration
Generate a data-model.json configuration file in your project root:
vendor/bin/data-model-generator init
This copies a default configuration template that you can customize.
Generate from a Schema
Pass a path or URL to an OpenAPI 3.0 or Swagger 2.0 schema:
vendor/bin/data-model-generator generate /path/to/openapi.json
The CLI detects the schema type automatically and generates PHP files based on your data-model.json configuration.
Programmatic Usage
You can also call the engine directly:
use Zerotoprod\DataModelGenerator\Engine; use Zerotoprod\DataModelGenerator\Models\Components; use Zerotoprod\DataModelGenerator\Models\Config; Engine::generate( Components::from($componentsArray), Config::from($configArray) );
Configuration
The data-model.json file controls how classes and enums are generated.
{
"$schema": "./vendor/zero-to-prod/data-model-generator/src/Schema/data_model.json",
"model": {
"directory": "./app/DataModels",
"namespace": "App\\DataModels",
"imports": [
"use Zerotoprod\\DataModel;"
],
"comments": true,
"readonly": false,
"use_statements": [
"use DataModel;"
],
"properties": {
"comments": true,
"visibility": "public",
"readonly": true,
"types": {
"integer": "int",
"boolean": "bool",
"number": "float"
},
"nullable": true
},
"constants": {
"comments": true,
"type": true,
"visibility": "public"
}
}
}
Model Options
| Option | Type | Default | Description |
|---|---|---|---|
directory |
string |
null |
Output directory for generated files |
namespace |
string |
null |
PHP namespace for generated classes |
imports |
array |
[] |
use import statements added to every file |
comments |
bool |
false |
Include class-level docblock comments |
readonly |
bool |
false |
Apply the readonly modifier to classes |
use_statements |
array |
[] |
Traits added inside every class body |
Property Options
| Option | Type | Default | Description |
|---|---|---|---|
visibility |
public|protected|private |
public |
Visibility modifier for properties |
readonly |
bool |
false |
Apply readonly to all properties |
comments |
bool |
false |
Include property-level docblock comments |
types |
object |
{} |
Type mapping (e.g., "integer": "int" maps integer types to int) |
nullable |
bool |
false |
Append null to all property types and default to null |
Constant Options
| Option | Type | Default | Description |
|---|---|---|---|
visibility |
public|protected|private |
public |
Visibility modifier for constants |
type |
bool |
false |
Include type declarations on constants |
comments |
bool |
false |
Include constant-level docblock comments |
Example
Given a schema that defines a User model and a Role enum, and using the default configuration above, the generator produces:
app/DataModels/User.php
<?php namespace App\DataModels; use Zerotoprod\DataModel; /** Represents a user */ class User { use DataModel; /** The user's name */ public const string name = 'name'; /** The user's age */ public const string age = 'age'; /** The user's name */ public readonly string|null $name = null; /** The user's age */ public readonly int|null $age = null; }
app/DataModels/Role.php
<?php namespace App\DataModels; use Zerotoprod\DataModel; enum Role: string { use DataModel; case Admin = 'admin'; case User = 'user'; }
Documentation Publishing
You can publish this README to your local documentation directory.
This can be useful for providing documentation for AI agents.
This can be done using the included script:
# Publish to default location (./docs/zero-to-prod/data-model-generator) vendor/bin/zero-to-prod-data-model-generator # Publish to custom directory vendor/bin/zero-to-prod-data-model-generator /path/to/your/docs
Automatic Documentation Publishing
You can automatically publish documentation by adding the following to your composer.json:
{
"scripts": {
"post-install-cmd": [
"zero-to-prod-data-model-generator"
],
"post-update-cmd": [
"zero-to-prod-data-model-generator"
]
}
}
Testing
./vendor/bin/phpunit