承接 danack/datatype 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

danack/datatype

最新稳定版本:0.9.1

Composer 安装命令:

composer require danack/datatype

包简介

A library that makes using data types easier

README 文档

README

A library for validating input and creating types.

Actions Status

Installation

composer require danack/datatype

Example usage

The full documentation is in DOCS.md, but here is an example usage.

In your controller, you would have some code to create the type. e.g. for Symfony you would have something like:

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);
    }
}

In your code you would have a data type that represents a particular concept, e.g. the search parameter used in the 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,
    ) {
    }
}

And each of the elements in the 'search parameters' would have their own rule based based details:

<?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 descriptions

The OpenAPI/Swagger specification for individual DataTypes can be generated directly from each DataType. e.g. This code:

<?php

$openapi_descriptions = generateOpenApiV300DescriptionForDataType(SearchParameters::class);

echo json_encode($openapi_descriptions, JSON_PRETTY_PRINT);

Would generate:

[
    {
        "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"
        }
    }
]

which would be consumed by the front-end, either dynamically or statically through being converted to code.

To note, the OpenAPI generation is one of the less tested part of the code.

Contributing

There are a few areas where contributions will be warmly welcomed:

  • Error messages. Writing clear error messages is always hard. There are almost certainly some areas where the message could be clearer, or just more consistent with other messages. All the messages are in src/DataType/Messages.php

  • Documentation. Contributions to documentation are always welcome.

  • More extraction and processing rules. Although the library currently fits my needs well, it is likely there are common rules that are not currently included.

Contributing code

We have several tools that are run to improve code quality. Please run sh runTests.sh to run them.

Pull requests should have full unit test coverage. Preferably also full mutation coverage through infection.

统计信息

  • 总下载量: 117
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-01-05

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固