PHPackages                             zero-to-prod/data-model-generator - 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. zero-to-prod/data-model-generator

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

zero-to-prod/data-model-generator
=================================

Builds DataModels from a Schema

v2.3.7(3mo ago)11.3k[1 PRs](https://github.com/zero-to-prod/data-model-generator/pulls)2MITPHPPHP &gt;=8.1.0CI passing

Since Nov 6Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/zero-to-prod/data-model-generator)[ Packagist](https://packagist.org/packages/zero-to-prod/data-model-generator)[ Docs](https://github.com/zero-to-prod/data-model-generator)[ Fund](https://github.com/sponsors/zero-to-prod)[ RSS](/packages/zero-to-prod-data-model-generator/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (41)Used By (2)

`Zerotoprod\DataModelGenerator`
===============================

[](#zerotoproddatamodelgenerator)

[![Repo](https://camo.githubusercontent.com/9a90a3efeee26aed7d7f2feee9cd84566a26f9c362cc773b184d076210906e1c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6769746875622d677261793f6c6f676f3d676974687562)](https://github.com/zero-to-prod/data-model-generator)[![GitHub Actions Workflow Status](https://camo.githubusercontent.com/5ec207b3905cc0164dfbd41eeabea98a031b253b8de623a4da17f09924995584/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7a65726f2d746f2d70726f642f646174612d6d6f64656c2d67656e657261746f722f746573742e796d6c3f6c6162656c3d74657374)](https://github.com/zero-to-prod/data-model-generator/actions)[![GitHub Actions Workflow Status](https://camo.githubusercontent.com/2ade9d4dc961e5463ca6095d1b42a5148431609e1b474d4b1d61d75eae79eb41/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7a65726f2d746f2d70726f642f646174612d6d6f64656c2d67656e657261746f722f6261636b77617264735f636f6d7061746962696c6974792e796d6c3f6c6162656c3d6261636b77617264735f636f6d7061746962696c697479)](https://github.com/zero-to-prod/data-model-generator/actions)[![Packagist Downloads](https://camo.githubusercontent.com/cbf9bd31e50acba3d8a4353ee4ca26feb3e14c0d5164503ab6d0a1ea8390e9f1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7a65726f2d746f2d70726f642f646174612d6d6f64656c2d67656e657261746f723f636f6c6f723d626c7565)](https://packagist.org/packages/zero-to-prod/data-model-generator/stats)[![Packagist Version](https://camo.githubusercontent.com/3f19fffdfc00a1b932b95bd5035c47b3d15f25933b258ca55328882fb5e07577/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7a65726f2d746f2d70726f642f646174612d6d6f64656c2d67656e657261746f723f636f6c6f723d663238643161)](https://packagist.org/packages/zero-to-prod/data-model-generator)[![License](https://camo.githubusercontent.com/d1c5fe25cf877fa152927a5451a3b707490063efb3b48c59037ee97ddf7c2b8a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7a65726f2d746f2d70726f642f646174612d6d6f64656c2d67656e657261746f723f636f6c6f723d726564)](https://github.com/zero-to-prod/data-model-generator/blob/main/LICENSE.md)[![wakatime](https://camo.githubusercontent.com/0511e92e4093c77a21dee813d8bdb4fed6225854ee998247254c6675ecc38614/68747470733a2f2f77616b6174696d652e636f6d2f62616467652f6769746875622f7a65726f2d746f2d70726f642f646174612d6d6f64656c2d67656e657261746f722e737667)](https://wakatime.com/badge/github/zero-to-prod/data-model-generator)[![Hits-of-Code](https://camo.githubusercontent.com/65d3b0b33985d9b844bd155c2da736494fe996292cd14f036e3c64aa342cdb87/68747470733a2f2f686974736f66636f64652e636f6d2f6769746875622f7a65726f2d746f2d70726f642f646174612d6d6f64656c2d67656e657261746f723f6272616e63683d6d61696e)](https://hitsofcode.com/github/zero-to-prod/data-model-generator/view?branch=main)

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
--------

[](#contents)

- [Installation](#installation)
- [Usage](#usage)
    - [Initialize Configuration](#initialize-configuration)
    - [Generate from a Schema](#generate-from-a-schema)
    - [Programmatic Usage](#programmatic-usage)
- [Configuration](#configuration)
    - [Model Options](#model-options)
    - [Property Options](#property-options)
    - [Constant Options](#constant-options)
- [Example](#example)
- [Documentation Publishing](#documentation-publishing)
    - [Automatic Documentation Publishing](#automatic-documentation-publishing)
- [Testing](#testing)

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

[](#installation)

Install the package via composer:

```
composer require zero-to-prod/data-model-generator
```

Requires PHP 8.1 or higher.

Usage
-----

[](#usage)

### Initialize Configuration

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

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

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

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

[](#model-options)

OptionTypeDefaultDescription`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

[](#property-options)

OptionTypeDefaultDescription`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

[](#constant-options)

OptionTypeDefaultDescription`visibility``public|protected|private``public`Visibility modifier for constants`type``bool``false`Include type declarations on constants`comments``bool``false`Include constant-level docblock commentsExample
-------

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

```
