This package let you represent a JSON Schema as a PHP object and dump it as a JSON and vice versa.
-
Define the model representing the schema.
$myJsonSchema = new JsonSchema('foo', 'My schema', "Test schema description", ...[ new OptionalObjectProperty(new TypeBoolean("top", "Is a top developer ?")), new RequiredObjectProperty(new TypeDate("birthdate", "Birthdate ?")), new OptionalObjectProperty(new TypeDateTime("interview_at", "Interview planned for ?")), new RequiredObjectProperty(new TypeEmail("email", "Email address ?")), new RequiredObjectProperty(TypeInteger::from(100, "minimum_salary", "Minimum salary ?")), new OptionalObjectProperty(TypeInteger::to(200, "max_days_in_office", "Max number of days in office ?")), new OptionalObjectProperty(new TypeObject("address", "Address", ...[ new OptionalObjectProperty(TypeString::withMinLength("street", "Street", 0)), new OptionalObjectProperty(TypeString::withMinLength("city", "City", 0)) ])) ]);
-
Serialize the object into a JSON.
echo JsonSchemaGenerator::generate($myJsonSchema);
the result will be a JSON like https://github.com/hpatoio/jsg/blob/develop/test/fixtures/all-types-schema.json
- Generate the model from a JSON
$hydratedSchema = JsonSchemaHydrator::hydrate("/path/to/the/file.json");
- Access elements
$hydratedSchema->getProperties()['id_of_the_property'];
Install the package in your project with
composer require hpatoio/jsg
- Clone the repo
git clone git@github.com:hpatoio/jsg.git
cd jsg
- Build the image
docker build -t jsg .docker
- Run
composer install
to get dependencies
docker run --rm -v `pwd`:/var/jsg jsg composer install
docker run --rm -v `pwd`:/var/jsg jsg vendor/bin/phpunit
This project uses PHP Mutation.
To run mutation tests use:
docker run --rm -v `pwd`:/var/jsg jsg ./infection.phar run
Before push please fix apply coding standards
docker run --rm -v `pwd`:/var/jsg jsg vendor/bin/ecs check src --no-progress-bar -vvv --fix
This package is licensed using the MIT License.