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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package php-json-schema-model-generator

Latest Version Minimum PHP Version Maintainability Build Status Coverage Status MIT License Documentation Status

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

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

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:

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

PHP Build Version
Package Version
Requires symfony/polyfill-php81 Version ^1.28
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 *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package wol-soft/php-json-schema-model-generator contains the following files

Loading the files please wait ....