Download the PHP package helmich/schema2class without Composer
On this page you can find all versions of the php package helmich/schema2class. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download helmich/schema2class
More information about helmich/schema2class
Files in helmich/schema2class
Package schema2class
Short Description Build PHP classes from JSON schema definitions
License MIT
Informations about the package schema2class
JSONSchema to PHP class converter
Build PHP classes from JSON schemas automatically.
Example
Consider a simple JSON schema (ironically stored in YAML format), stored in a file example.yaml
:
Using this converter, you can automatically generate PHP classes from this schema with accessor and conversion functions:
This command will automatically try to infer a PHP target namespace from your composer.json
file and automatically create the appropriate PHP classes:
Then, use the classes in your code:
Compatibility
This tool requires PHP 8.2 or newer to run.
The generated code can be backwards-compatible up until PHP 5.6. Use the --target-php
flag to set the desired PHP version that the generated code should be compatible with. When using a configuration file, use the targetPHPVersion
property.
Creation result
The generated classes have these features:
- The class namespace can either be specified via command-line (
--target-namespace
), specification file (targetNamespace
). If neither is specified, the generator will inspect thecomposer.json
of your project, look for any PSR-4 configuration and infer the namespace from there. - The main object's name is defined by the command-line (
--class
) or the specification file. - Sub-object's names are taken from the property name.
- Array items are suffixed 'Item'.
OneOf
alternatives are suffixed 'AlternativeX', withX
being an incremented integer.- The constructor has arguments for all required properties in the schema.
- All properties are private, with getter methods for access, and explicit type declarations for the return value (in PHP5 mode, only PHPDoc is used).
- Static function
buildFromInput(array $data)
accepts an array (usingjson_decode('{}', true)
), validates it according to the schema and creates the full object tree as return value. An additional mapping step is not required. - Function
toJson()
returns a plain array ready forjson_encode()
. - Writing to any object's properties is done immutably by using
withX()
(orwithoutX()
for optional values). This will return a new instance of that object with the value changed.
As an example, a shortened version with all comments removed, from the above schema shows the location, only containing the city (country is behaving the same, but with a different name)
Installation
Install using Composer:
Using configuration files
In many projects, you're going to want to keep an evolving JSON schema in sync with the generated PHP classes continuously. For this reason, S2C allows you to create a configuration file .s2c.yaml
that stores the most common conversion options:
You can store your local configuration in this yaml file and start the generation process by calling
This will scan for .s2c.yaml
in the current directory and use it's parameters. If you need to have different files for multiple schemas, you can provide a config file as a parameter.
All versions of schema2class with dependencies
symfony/console Version ~3.0|~4.0|~5.0|~6.0|~7.0
symfony/yaml Version ~3.0|~4.0|~5.0|~6.0|~7.0
justinrainbow/json-schema Version ^6.0
ext-json Version *
composer/semver Version ^3.0
laminas/laminas-code Version ^4.12