danack/typespec

该软件包已被 废弃,不再维护。作者建议使用 danack/datatype 软件包。

一个使使用数据类型更简单的库

0.6.0 2024-01-01 18:01 UTC

This package is auto-updated.

Last update: 2024-01-01 18:05:28 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 直接生成单个 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 实现完整的变异覆盖率。