danack/datatype

一个简化数据类型使用的库

0.6.0 2024-01-01 18:01 UTC

This package is auto-updated.

Last update: 2024-08-30 19:44:39 UTC


README

一个用于验证输入和创建类型的库。

Actions Status

安装

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直接生成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实现完整的突变覆盖率。