dunglas/php-to-json-schema

此包已被弃用,不再维护。作者建议使用 api-platform/core 包。

从PHP实体生成JSON模式

v0.1.0 2016-01-11 15:45 UTC

This package is auto-updated.

Last update: 2022-01-10 20:44:46 UTC


README

此库已弃用,请使用 API Platform JSON Schema 代替。

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

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

致谢

Kévin Dunglas 创建。