danack / typespec
0.6.0
2024-01-01 18:01 UTC
Requires
- php: >=8.0
- ext-mbstring: *
- danack/var-map: ^0.6.1
- seld/jsonlint: ^1.9.0
Requires (Dev)
- danack/coding-standard: 0.3.0
- danack/php-unit-helper: dev-master#7e50284d762b816acde56919f82f3431a6bad4c5 as 0.5.0
- laminas/laminas-diactoros: ^2.6
- phpstan/phpstan-strict-rules: ^1.1.0
- phpunit/phpunit: 9.5.6
- psr/http-message: ^1.0
- respect/validation: ^1.1.31
- slevomat/coding-standard: ^7.0.9
- squizlabs/php_codesniffer: ^3.6.0
- yoast/yoastcs: 1.0
README
一个用于验证输入和创建类型的库。
安装
composer require danack/datatype
示例用法
完整的文档在 DOCS.md 中,但这里有一个示例。
在你的控制器中,你可能会有一些创建类型的代码。例如,对于Symfony,你可能会有像这样的代码:
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; class SearchController { public function index(Request $request, SearchRepo $searchRepo): JsonResponse { $searchParams = SearchParams::createFromRequest($request); $data = $searchRepo->search($searchParams); return $this->json($data); } }
在你的代码中,你可能有一个表示特定概念的数据类型,例如API中使用的搜索参数
class SearchParameters implements DataType { use CreateFromRequest; use CreateFromVarMap; use GetInputTypesFromAttributes; public function __construct( #[SearchTerm('search')] public string $phrase, #[MaxItems('limit')] public int $limit, #[ArticleSearchOrdering('order')] public Ordering $ordering, ) { } }
而“搜索参数”中的每个元素都有自己的基于细节的规则
<?php use DataType\ExtractRule\GetString; use DataType\InputType; use DataType\HasInputType; use DataType\ProcessRule\MaxLength; use DataType\ProcessRule\MinLength; #[\Attribute] class SearchTerm implements HasInputType { public function __construct( private string $name ) { } public function getInputType(): InputType { return new InputType( $this->name, new GetString(), new MinLength(3), new MaxLength(200), ); } }
<?php namespace DataTypeExample\InputTypes; use DataType\InputType; use DataType\ExtractRule\GetIntOrDefault; use DataType\HasInputType; use DataType\ProcessRule\MaxIntValue; use DataType\ProcessRule\MinIntValue; #[\Attribute] class MaxItems implements HasInputType { public function __construct( private string $name ) { } public function getInputType(): InputType { return new InputType( $this->name, new GetIntOrDefault(20), new MinIntValue(1), new MaxIntValue(200), ); } }
OpenAPI 描述
可以从每个 DataType 直接生成单个 DataTypes 的 OpenAPI/Swagger 规范。例如,以下代码:
<?php $openapi_descriptions = generateOpenApiV300DescriptionForDataType(SearchParameters::class); echo json_encode($openapi_descriptions, JSON_PRETTY_PRINT);
会生成
[ { "name": "search", "required": true, "schema": { "type": "string", "minLength": 3, "maxLength": 200 } }, { "name": "limit", "required": false, "schema": { "minimum": 1, "maximum": 200, "default": 20, "type": "integer", "exclusiveMaximum": false, "exclusiveMinimum": false } }, { "name": "order", "required": false, "schema": { "default": "article_id", "type": "array" } } ]
这将由前端动态或静态地消耗,通过转换为代码。
需要注意的是,OpenAPI 生成是代码中测试较少的部分。
贡献
有几个领域将热烈欢迎贡献
-
错误信息。编写清晰的错误信息始终很难。肯定有一些地方信息可以更清晰,或者只是与其他信息更一致。所有信息都在 src/DataType/Messages.php 中。
-
文档。文档的贡献始终受欢迎。
-
更多的提取和处理规则。尽管该库目前很好地满足了我的需求,但可能还有一些尚未包含的常见规则。
代码贡献
我们有几个工具可以运行以改进代码质量。请运行 sh runTests.sh
来运行它们。
拉取请求应具有完整的单元测试覆盖率。最好也通过 infection 实现完整的变异覆盖率。