wwwision/types-jsonschema
Composer 安装命令:
composer require wwwision/types-jsonschema
包简介
Generator for JSON Schema files from PHP classes, see https://json-schema.org/
README 文档
README
Integration for the wwwision/types package that allows to generate JSON Schema files from PHP code
Usage
This package can be installed via composer:
composer require wwwision/types-jsonschema
With that, you can create JSON Schema from PHP classes:
class Contact { public function __construct(public string $name, public int $age) {} } $schema = (new JsonSchemaGenerator())->fromClass(Contact::class); $expected = <<<JSON { "type": "object", "properties": { "name": { "type": "string" }, "age": { "type": "integer" } }, "additionalProperties": false, "required": [ "name", "age" ] } JSON; assert(json_encode($schema, JSON_PRETTY_PRINT) === $expected);
Advanced schemas
All attributes of the wwwision/types package are supported in order to create sophisticated schemas.
Example: Complex composite object
#[StringBased] final class GivenName { private function __construct(public readonly string $value) {} } #[StringBased] final class FamilyName { private function __construct(public readonly string $value) {} } final class FullName { public function __construct( public readonly GivenName $givenName, public readonly FamilyName $familyName, ) {} } #[Description('honorific title of a person')] enum HonorificTitle { #[Description('for men, regardless of marital status, who do not have another professional or academic title')] case MR; #[Description('for married women who do not have another professional or academic title')] case MRS; #[Description('for girls, unmarried women and married women who continue to use their maiden name')] case MISS; #[Description('for women, regardless of marital status or when marital status is unknown')] case MS; #[Description('for any other title that does not match the above')] case OTHER; } #[Description('A contact in the system')] final class Contact { public function __construct( public readonly HonorificTitle $title, public readonly FullName $name, #[Description('Whether the contact is registered or not')] public bool $isRegistered = false, ) {} } $schema = (new JsonSchemaGenerator())->fromClass(Contact::class); $expected = <<<JSON { "type": "object", "description": "A contact in the system", "properties": { "title": { "type": "string", "description": "honorific title of a person", "enum": [ "MR", "MRS", "MISS", "MS", "OTHER" ] }, "name": { "type": "object", "properties": { "givenName": { "type": "string" }, "familyName": { "type": "string" } }, "additionalProperties": false, "required": [ "givenName", "familyName" ] }, "isRegistered": { "type": "boolean", "description": "Whether the contact is registered or not" } }, "additionalProperties": false, "required": [ "title", "name" ] } JSON; assert(json_encode($schema, JSON_PRETTY_PRINT) === $expected);
Middlewares (version 2.0)
Starting with version 2.0, middlewares can be registered to modify the schema generation process. This is useful for logging, caching, or modifying the generated schema:
// ... class LoggingMiddleware implements SchemaGeneratorMiddleware { public array $log = []; public function __invoke(Types\Schema $schema, Closure $next): JsonSchema { $this->log[] = $schema->getName(); return $next($schema); } } $middleware = new LoggingMiddleware(); $options = JsonSchemaGeneratorOptions::create()->withMiddleware($middleware); $schema = (new JsonSchemaGenerator($options))->fromClass(Contact::class); assert($middleware->log === ['Contact', 'HonorificTitle', 'FullName', 'GivenName', 'FamilyName', 'boolean']);
Contribution
Contributions in the form of issues or pull requests are highly appreciated
License
See LICENSE
wwwision/types-jsonschema 适用场景与选型建议
wwwision/types-jsonschema 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.66k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 02 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 wwwision/types-jsonschema 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wwwision/types-jsonschema 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 10.66k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2024-02-29