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

v2.3.7 2026-02-10 19:13 UTC

README

Repo GitHub Actions Workflow Status GitHub Actions Workflow Status Packagist Downloads Packagist Version License wakatime Hits-of-Code

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