Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Sep 25, 2025. It is now read-only.

Provides the ability to generate JSON schemas from Data Transfer Object (DTO) classes

License

NotificationsYou must be signed in to change notification settings

context-hub/json-schema-generator

 
 

PHP Version RequireLatest Stable VersionphpunitpsalmTotal Downloadspsalm-level

The JSON Schema Generator is a PHP package that simplifies the generation ofJSON schemas from Data Transfer Object (DTO) classes.It supports PHP enumerations and generic type annotations for arrays and provides an attribute for specifying title, description, and default value.

Main use case - structured output definition for LLMs.

Requirements

Make sure that your server is configured with the following PHP versions and extensions:

  • PHP >=8.1

Installation

You can install the package via Composer:

composer require context-hub/json-schema-generator

Usage

To generate a schema for a DTO, instantiate theSpiral\JsonSchemaGenerator\Generator and call thegenerate method,passing the DTO class as an argument (fully qualified class name or reflection). The method will return an instance ofSpiral\JsonSchemaGenerator\Schema.

Let's create a simple data transfer object:

namespaceApp\DTO;useSpiral\JsonSchemaGenerator\Attribute\Field;class Movie{publicfunction__construct(        #[Field(title:'Title', description:'The title of the movie')]publicreadonlystring$title,        #[Field(title:'Year', description:'The year of the movie')]publicreadonlyint$year,        #[Field(title:'Description', description:'The description of the movie')]publicreadonly ?string$description =null,publicreadonly ?string$director =null,        #[Field(title:'Release Status', description:'The release status of the movie')]publicreadonly ?ReleaseStatus$releaseStatus =null,    ) {    }}

This DTO has areleaseStatus, which is an enum. Let's create it:

namespaceApp\DTO;enum ReleaseStatus:string{case Released ='Released';case Rumored ='Rumored';case PostProduction ='Post Production';case InProduction ='In Production';case Planned ='Planned';case Canceled ='Canceled';}

Now, let's generate a schema for this DTO:

useSpiral\JsonSchemaGenerator\Generator;useApp\DTO\Movie;$generator =newGenerator();$schema =$generator->generate(Movie::class);

NoteAdditionally, the package provides theSpiral\JsonSchemaGenerator\GeneratorInterface, which can be integrated into your application's dependency container for further customization and flexibility.

TheSpiral\JsonSchemaGenerator\Schema object implements theJsonSerializable interface, allowing easy conversionof the schema into either JSON or a PHP array.

Example array output:

['properties'  => ['title'         => ['title'       =>'Title','description' =>'The title of the movie','type'        =>'string',        ],'year'          => ['title'       =>'Year','description' =>'The year of the movie','type'        =>'integer',        ],'description'   => ['title'       =>'Description','description' =>'The description of the movie','type'        =>'string',        ],'director'      => ['type' =>'string',        ],'releaseStatus' => ['title'       =>'Release Status','description' =>'The release status of the movie','allOf'       => [                ['$ref' =>'#/definitions/ReleaseStatus',                ],            ],        ],    ],'required'    => ['title','year',    ],'definitions' => ['ReleaseStatus' => ['title' =>'ReleaseStatus','type'  =>'string','enum'  => ['Released','Rumored','Post Production','In Production','Planned','Canceled',            ],        ],    ],];

Class properties can be arrays, and the type of elements within the array can be specified using PHPDoc annotations.

For example, we have a DTO with an array of objects:

namespaceApp\DTO;useSpiral\JsonSchemaGenerator\Attribute\Field;finalclass Actor{publicfunction__construct(publicreadonlystring$name,/**         * @var array<Movie>         */publicreadonlyarray$movies = [],    ) {    }}

In this example, we use a PHPDoc block to indicate that the property$movies contains an array ofMovie objects.

NoteVarious documentation type annotations are supported, including@var array<Movie>,@var Movie[],and@var list<Movie>. For promoted properties, you can use annotations like@param array<Movie> $movies,@param Movie[] $movies, and@param list<Movie> $movies.

Now, let's generate a schema for this DTO:

useSpiral\JsonSchemaGenerator\Generator;useApp\DTO\Actor;$generator =newGenerator();$schema =$generator->generate(Actor::class);

Example array output:

['properties' => ['name'   => ['type' =>'string',        ],'movies' => ['type'  =>'array','items' => ['$ref' =>'#/definitions/Movie',            ],'default' => [],        ],    ],'required'   => ['name',    ],'definitions' => ['Movie'         => ['title'      =>'Movie','type'       =>'object','properties' => ['title'         => ['title'       =>'Title','description' =>'The title of the movie','type'        =>'string',                ],'year'          => ['title'       =>'Year','description' =>'The year of the movie','type'        =>'integer',                ],'description'   => ['title'       =>'Description','description' =>'The description of the movie','type'        =>'string',                ],'director'      => ['type' =>'string',                ],'releaseStatus' => ['title'       =>'Release Status','description' =>'The release status of the movie','allOf'       => [                        ['$ref' =>'#/definitions/ReleaseStatus',                        ],                    ],                ],            ],'required'   => ['title','year',            ],        ],'ReleaseStatus' => ['title' =>'ReleaseStatus','type'  =>'string','enum'  => ['Released','Rumored','Post Production','In Production','Planned','Canceled',            ],       ]    ],];

Testing

composertest

Changelog

Please seeCHANGELOG for more information on what has changed recently.

Contributing

Please seeCONTRIBUTING for details.

License

The MIT License (MIT). Please seeLicense File for more information.

About

Provides the ability to generate JSON schemas from Data Transfer Object (DTO) classes

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Languages

  • PHP100.0%

[8]ページ先頭

©2009-2025 Movatter.jp