uuf6429/phpdoc-to-jsonschema
Composer 安装命令:
composer require uuf6429/phpdoc-to-jsonschema
包简介
A PHPDoc to JsonSchema converter.
README 文档
README
Convert PHPStan-style PHPDoc to JSON Schema.
💾 Installation
This package can be installed with Composer, simply run the following:
composer require uuf6429/phpdoc-to-jsonschema
Consider using --dev if you intend to use this library during development only.
🚀 Usage
The following code:
<?php namespace MyApp; // Define an example class to be featured in the json schema class Person { public function __construct( public readonly string $name, public readonly int $height, ) { } } // Load a PHPDoc block that should return an instance of the Person class $docblock = \uuf6429\PHPStanPHPDocTypeResolver\PhpDoc\Factory::createInstance() ->createFromComment('/** @return \MyApp\Person */'); // Retrieve the @return tag for that docblock. /** @var \PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode $returnTag */ $returnTag = $docblock->getTag('@return'); // Convert that @return tag to JSON Schema // (note that convertTag() takes typed tags, for example: @param, @var, @property[-read/-write] and of course @return) $converter = new \uuf6429\PHPDocToJSONSchema\Converter(); $result = $converter->convertType($returnTag->type, null); // Export the schema and print it out as json echo json_encode(\Swaggest\JsonSchema\Schema::export($result), JSON_PRETTY_PRINT);
...results in something like:
{
"definitions": {
"MyApp.Person": {
"required": [
"name",
"height"
],
"properties": {
"name": {
"type": "string",
"readOnly": true
},
"height": {
"type": "integer",
"readOnly": true
}
},
"type": "object"
}
},
"$ref": "#\/definitions\/MyApp.Person"
}
See also ExampleTest for a more complex example.
📖 Documentation
The \uuf6429\PHPDocToJSONSchema\Converter class exposes the following:
-
function convertType(\phpDocumentor\Reflection\Type $type, ?string $currentClass): \Swaggest\JsonSchema\Schema
Converts the provided PHPDoc type and returns its schema.$typeThe PHPDoc type to be converted.$currentClassThe fully-qualified class name of the class where that type appeared, or null if wasn't a class (e.g. for functions).
uuf6429/phpdoc-to-jsonschema 适用场景与选型建议
uuf6429/phpdoc-to-jsonschema 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 08 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 uuf6429/phpdoc-to-jsonschema 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 uuf6429/phpdoc-to-jsonschema 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-08-15