soyuka/php-to-json-schema

Generate a JSON schema from a PHP entity

1.0.1 2017-01-31 16:19 UTC

This package is auto-updated.

Last update: 2024-11-19 20:53:11 UTC


README

Creates a JSON Schema from a PHP entity. Useful to ensure that a given JSON document will be deserialized properly in an object graph.

Build Status Scrutinizer Code Quality SensioLabsInsight

Installation

Use https://getcomposer.org to install the library:

composer require dunglas/php-to-json-schema

Usage

use Dunglas\PhpToJsonSchema\Generator;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;

class MyClass
{
    private $foo;
    private $bar;

    public function setFoo(string $foo)
    {
        $this->foo = $foo;
    }

    public function setBar(float $bar = null)
    {
        $this->bar = $bar;
    }

    // ...
}


$reflectionExtractor = new ReflectionExtractor();
$propertyInfoExtractor = new PropertyInfoExtractor([$reflectionExtractor], [$reflectionExtractor], [], [$reflectionExtractor]);

$this->generator = new Generator($propertyInfoExtractor);
echo json_encode($generator->generate(MyClass::class));

Credits

Created by Kévin Dunglas.