soyuka/php-to-json-schema

从PHP实体生成JSON模式

1.0.1 2017-01-31 16:19 UTC

This package is auto-updated.

Last update: 2024-09-19 20:33:38 UTC


README

从PHP实体创建JSON模式。有助于确保给定的JSON文档在对象图中正确反序列化。

Build Status Scrutinizer Code Quality SensioLabsInsight

安装

使用 https://getcomposer.org.cn 安装库

composer require dunglas/php-to-json-schema

用法

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));

致谢

Kévin Dunglas 创建。