Download the PHP package wol-soft/php-json-schema-model-generator without Composer
On this page you can find all versions of the php package wol-soft/php-json-schema-model-generator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download wol-soft/php-json-schema-model-generator
More information about wol-soft/php-json-schema-model-generator
Files in wol-soft/php-json-schema-model-generator
Package php-json-schema-model-generator
Short Description Creates (immutable) PHP model classes from JSON-Schema files including all validation rules as PHP code
License MIT
Homepage https://github.com/wol-soft/php-json-schema-model-generator
Informations about the package php-json-schema-model-generator
php-json-schema-model-generator
Generates PHP model classes from JSON-Schema files including validation and providing a fluent auto completion for the generated classes.
Table of Contents
- Motivation
- Requirements
- Installation
- Basic usage
- Configuring using the GeneratorConfiguration
- Examples
- How the heck does this work?
- Tests
- Docs
Motivation
Simple example from a PHP application: you define and document an API with swagger annotations and JSON-Schema models. Now you want to use models in your controller actions instead of manually accessing the request data (eg. array stuff). Additionally your schema already defines the validation rules for the models. Why duplicate this rules into your manually written code? Instead you can set up a middleware which instantiates models generated with this library and feed the model with the request data. Now you have a validated model which you can use in your controller action. With full auto completion when working with nested objects. Yay!
Requirements
- Requires at least PHP 8.0
- Requires the PHP extensions ext-json and ext-mbstring
Installation
The recommended way to install php-json-schema-model-generator is through Composer:
To avoid adding all dependencies of the php-json-schema-model-generator to your production dependencies it's recommended to add the library as a dev-dependency and include the wol-soft/php-json-schema-model-generator-production library. The production library provides all classes to run the generated code. Generating the classes should either be a step done in the development environment or as a build step of your application (which is the recommended workflow).
Basic usage
Check out the docs for more details.
The base object for generating models is the ModelGenerator. After you have created a Generator you can use the object to generate your model classes without any further configuration:
The first parameter of the generateModels method must be a class implementing the SchemaProviderInterface. The provider fetches the JSON schema files and provides them for the generator. The following providers are available:
Provider | Description |
---|---|
RecursiveDirectoryProvider | Fetches all *.json files from the given source directory. Each file must contain a JSON Schema object definition on the top level |
OpenAPIv3Provider | Fetches all objects defined in the #/components/schemas section of an Open API v3 spec file |
The second parameter must point to an existing and empty directory (you may use the generateModelDirectory
helper method to create your destination directory). This directory will contain the generated PHP classes after the generator is finished.
As an optional parameter you can set up a GeneratorConfiguration object (check out the docs for all available options) to configure your Generator and/or use the method generateModelDirectory to generate your model directory (will generate the directory if it doesn't exist; if it exists, all contained files and folders will be removed for a clean generation process):
The generator will check the given source directory recursive and convert all found *.json files to models. All JSON-Schema files inside the source directory must provide a schema of an object.
Examples
The directory ./tests/manual
contains some easy examples which show the usage. After installing the dependencies of the library via composer update
you can execute php ./tests/manual/test.php
to generate the examples and play around with some JSON-Schema files to explore the library.
Let's have a look into an easy example. We create a simple model for a person with a name and an optional age. Our resulting JSON-Schema:
After generating a class with this JSON-Schema our class with the name Person
will provide the following interface:
Now let's have a look at the behaviour of the generated model:
More complex exception messages eg. from a allOf composition may look like:
How the heck does this work?
The class generation process basically splits up into three to four steps:
- Scan the given source directory to find all *.json files which should be processed.
- Loop over all schemas which should be generated. This is the main step of the class generation. Now each schema is parsed and a Schema model class which holds the properties for the generated model is populated. All validation rules defined in the JSON-Schema are translated into plain PHP code. After the model is finished a RenderJob is generated and added to the RenderQueue. If a JSON-Schema contains nested objects or references multiple RenderJobs may be added to the RenderQueue for a given schema file.
- If post processors are defined for the generation process the post processors will be applied.
- After all schema files have been parsed without an error the RenderQueue will be worked off. All previous added RenderJobs will be executed and the PHP classes will be saved to the filesystem at the given destination directory.
Tests
The library is tested via PHPUnit.
After installing the dependencies of the library via composer update
you can execute the tests with ./vendor/bin/phpunit
(Linux) or vendor\bin\phpunit.bat
(Windows). The test names are optimized for the usage of the --testdox
output. Most tests are atomic integration tests which will set up a JSON-Schema file and generate a class from the schema and test the behaviour of the generated class afterwards.
During the execution the tests will create a directory PHPModelGeneratorTest in tmp where JSON-Schema files and PHP classes will be written to.
If a test which creates a PHP class from a JSON-Schema fails the JSON-Schema and the generated class(es) will be dumped to the directory ./failed-classes
Docs
The docs for the library is generated with Sphinx.
To generate the documentation install Sphinx, enter the docs directory and execute make html
(Linux) or make.bat html
(Windows). The generated documentation will be available in the directory ./docs/build
.
The documentation hosted at Read the Docs is updated on each push.
All versions of php-json-schema-model-generator with dependencies
wol-soft/php-json-schema-model-generator-production Version ^0.19.0
wol-soft/php-micro-template Version ^1.9.0
php Version >=8.0
ext-json Version *
ext-mbstring Version *