hpatoio / jsg
Generate an object from a json schema definition and vice versa
0.1.1
2020-01-09 09:29 UTC
Requires
- php: ^7.2
- jms/serializer: ^3.1
Requires (Dev)
- phpstan/phpstan: ^0.12.3
- phpunit/phpunit: ^8.1.0
- symplify/easy-coding-standard: ^5.4
This package is auto-updated.
Last update: 2024-11-17 22:14:03 UTC
README
This package let you represent a JSON Schema as a PHP object and dump it as a JSON and vice versa.
Usage
Create a JSON from a model
-
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
Create a model from a JSON
- Generate the model from a JSON
$hydratedSchema = JsonSchemaHydrator::hydrate("/path/to/the/file.json");
- Access elements
$hydratedSchema->getProperties()['id_of_the_property'];
Installation
Install the package in your project with
composer require hpatoio/jsg
Create dev environment
- 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
Run tests
docker run --rm -v `pwd`:/var/jsg jsg vendor/bin/phpunit
Run mutation tests
This project uses PHP Mutation.
To run mutation tests use:
docker run --rm -v `pwd`:/var/jsg jsg ./infection.phar run
Apply CS
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
License
This package is licensed using the MIT License.