Jane is a library to generate, in PHP, a model and a serializer from a JSON Schema.
The generated code may contain bug or incorrect behavior, use it as a base for your application but you should never trust it as is.
# jane [schema-path] [root name] [namespace] [destination]
php vendor/bin/jane generate json-schema.json Classname Namespace\\Prefix src/Namespace/Prefix
This will generate, in the src/Namespace/Prefix
, a Model
and a Normalizer
directory.
Model
directory will contain a Namespace\Prefix\Model\Classname
, which correspond to the root Schema
of the json schema file, and all the subclasses discovered through the parsing of the Schema.
Normalizer
directory will contain a normalizer service class for each of the model class generated.
Use composer for installation
composer require jane/jane
Here is a recommended workflow when dealing with the generated code:
- Start from a clean revision on your project (no modified files);
- Update your Json Schema file (edit or download new version);
- Generate the new code;
- Check the generated code with a diff tool:
git diff
for example; - If all is well commit modifications.
An optional and recommanded practice is to separate the generated code in a specific directory
like creating a generated
directory in your project and using jane inside. This allows other developers
to be aware that this part of the project is generated and must not be updated manually.
See this library (jane-swagger) for an example on how to achieve that.
Here is a quick presentation on how this library transforms a Json Schema file into models and normalizers:
- First step is to read and parse the Json Schema file;
- Second step is to guess classes and their associated properties and types;
- Once all things are guessed, classes and their properties are transformed into an AST (by using the PHP-Parser library from nikic);
- Then the AST is written into PHP files.
View the LICENSE file attach to this project.