open-code-modeling/json-schema-to-php
最新稳定版本:0.3.0
Composer 安装命令:
composer require open-code-modeling/json-schema-to-php
包简介
Parses JSON schema files and provides an API to easily generate code from JSON schema.
README 文档
README
Parses JSON schema and provides an API to easily generate code from JSON schema.
Installation
$ composer require open-code-modeling/json-schema-to-php --dev
Usage
Consider you have this JSON schema.
{
"type": "object",
"required": ["buildingId", "name"],
"additionalProperties": false,
"definitions": {
"name": {
"type": ["string", "null"]
}
},
"properties": {
"buildingId": {
"type": "string",
"pattern": "^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$"
},
"name": {
"$ref": "#/definitions/name"
}
}
}
Create a TypeSet definition from parsed JSON:
$decodedJson = \json_decode($jsonSchema, true); $typeSet = Type::fromDefinition($decodedJson); /** @var ObjectType $type */ $type = $typeSet->first(); $type->additionalProperties(); // false $properties = $type->properties(); /** @var TypeSet $buildingIdTypeSet */ $buildingIdTypeSet = $properties['buildingId']; /** @var StringType $buildingId */ $buildingId = $buildingIdTypeSet->first(); $buildingId->name(); // buildingId $buildingId->type(); // string $buildingId->pattern(); // ^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$ $buildingId->isRequired(); // true $buildingId->isNullable(); // false /** @var TypeSet $nameTypeSet */ $nameTypeSet = $properties['name']; /** @var ReferenceType $name */ $name = $nameTypeSet->first(); $resolvedTypeSet = $name->resolvedType(); /** @var StringType $resolvedType */ $resolvedType = $resolvedTypeSet->first(); $resolvedType->name(); // name $resolvedType->type(); // string $resolvedType->isRequired(); // true $resolvedType->isNullable(); // true // ...
See OpenCodeModeling\JsonSchemaToPhp\Type classes and tests for more information.
统计信息
- 总下载量: 1.02k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 11
- 点击次数: 1
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-10-22