Download the PHP package spiral/json-schema-generator without Composer
On this page you can find all versions of the php package spiral/json-schema-generator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download spiral/json-schema-generator
More information about spiral/json-schema-generator
Files in spiral/json-schema-generator
Package json-schema-generator
Short Description Provides the ability to generate JSON schemas from Data Transfer Object (DTO) classes
License MIT
Homepage https://github.com/spiral/json-schema-generator
Informations about the package json-schema-generator
JSON Schema Generator
The JSON Schema Generator is a PHP package that simplifies the generation of JSON 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:
Usage
To generate a schema for a DTO, instantiate the Spiral\JsonSchemaGenerator\Generator
and call the generate method,
passing the DTO class as an argument (fully qualified class name or reflection). The method will return an instance of
Spiral\JsonSchemaGenerator\Schema
.
Let's create a simple data transfer object:
This DTO has a releaseStatus, which is an enum. Let's create it:
Now, let's generate a schema for this DTO:
Note Additionally, the package provides the
Spiral\JsonSchemaGenerator\GeneratorInterface,
which can be integrated into your application's dependency container for further customization and flexibility.
The Spiral\JsonSchemaGenerator\Schema
object implements the JsonSerializable interface, allowing easy conversion
of the schema into either JSON or a PHP array.
Example array output:
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:
In this example, we use a PHPDoc block to indicate that the property $movies contains an array of Movie objects.
Note Various 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:
Example array output:
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
License
The MIT License (MIT). Please see License File for more information.
All versions of json-schema-generator with dependencies
symfony/property-info Version ^6.3 || ^7.0
phpstan/phpdoc-parser Version ^1.24
phpdocumentor/reflection-docblock Version ^5.3