PHPackages                             hashwallet/dto - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. hashwallet/dto

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

hashwallet/dto
==============

php hyperf dto

3.1.x-dev(2y ago)01.5k2MITPHPPHP &gt;=8.0

Since Aug 24Pushed 2y ago1 watchersCompare

[ Source](https://github.com/olsai/dto)[ Packagist](https://packagist.org/packages/hashwallet/dto)[ RSS](/packages/hashwallet-dto/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (13)Versions (3)Used By (2)

PHP Swagger Api Docs
--------------------

[](#php-swagger-api-docs)

基于 [Hyperf](https://github.com/hyperf/hyperf) 框架的 DTO 类映射

##### 优点

[](#优点)

- 请求参数映射到PHP类
- 代码可维护性好，扩展性好
- 支持数组，递归，嵌套
- 支持框架数据验证器

##### 缺点

[](#缺点)

- 模型类需要手工编写

注意
--

[](#注意)

> php &gt;= 8.0

安装
--

[](#安装)

```
composer require hashwallet/dto

```

使用
--

[](#使用)

### 1. 使用

[](#1-使用)

注解
--

[](#注解)

> 命名空间:`Hyperf\DTO\Annotation\Contracts`

#### RequestBody

[](#requestbody)

- 获取Body参数

```
public function add(#[RequestBody] DemoBodyRequest $request){}
```

### RequestQuery

[](#requestquery)

- 获取GET参数

```
public function add(#[RequestQuery] DemoQuery $request){}
```

### RequestFormData

[](#requestformdata)

- 获取表单请求

```
public function fromData(#[RequestFormData] DemoFormData $formData){}
```

- 获取文件(和表单一起使用)

```
#[ApiFormData(name: 'photo', type: 'file')]
```

- 获取Body参数和GET参数

```
public function add(#[RequestBody] DemoBodyRequest $request, #[RequestQuery] DemoQuery $query){}
```

> 注意: 一个方法，不能同时注入RequestBody和RequestFormData

示例
--

[](#示例)

### 控制器

[](#控制器)

```
#[Controller(prefix: '/demo')]
#[Api(tags: 'demo管理', position: 1)]
class DemoController extends AbstractController
{
    #[ApiOperation(summary: '查询')]
    #[PostMapping(path: 'index')]
    public function index(#[RequestQuery] #[Valid] DemoQuery $request): Contact
    {
        $contact = new Contact();
        $contact->name = $request->name;
        var_dump($request);
        return $contact;
    }

    #[PutMapping(path: 'add')]
    public function add(#[RequestBody] DemoBodyRequest $request, #[RequestQuery] DemoQuery $query)
    {
        var_dump($query);
        return json_encode($request, JSON_UNESCAPED_UNICODE);
    }

    #[PostMapping(path: 'fromData')]
    public function fromData(#[RequestFormData] DemoFormData $formData): bool
    {
        $file = $this->request->file('photo');
        var_dump($file);
        var_dump($formData);
        return true;
    }

    #[GetMapping(path: 'find/{id}/and/{in}')]
    public function find(int $id, float $in): array
    {
        return ['$id' => $id, '$in' => $in];
    }

}
```

验证器
---

[](#验证器)

### 基于框架的验证

[](#基于框架的验证)

> 安装hyperf框架验证器[hyperf/validation](https://github.com/hyperf/validation), 并配置(已安装忽略)

- 注解 `Required` `Between` `Date` `Email` `Image` `Integer` `Nullable` `Numeric` `Url` `Validation`
- 校验生效

> 只需在控制器方法中加上 #\[Valid\] 注解

```
public function index(#[RequestQuery] #[Valid] DemoQuery $request){}
```

```
class DemoQuery
{
    public string $name;

    #[Required]
    #[Integer]
    #[Between(1,5)]
    public int $num;
}
```

- Validation

> rule 支持框架所有验证

- 自定义验证注解

> 只需继承`Hyperf\DTO\Annotation\Validation\BaseValidation`即可

```
#[Attribute(Attribute::TARGET_PROPERTY)]
class Image extends BaseValidation
{
    protected $rule = 'image';
}
```

注意
--

[](#注意-1)

```
    /**
     * 需要绝对路径.
     * @var \App\DTO\Address[]
     */
    #[ApiModelProperty('地址')]
    public array $addressArr;
```

- 映射数组类时,`@var`需要写绝对路径
- 控制器中使用了框架`AutoController`注解,只收集了`POST`方法

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity39

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~322 days

Total

2

Last Release

1038d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f46846684bab03363b0ecf80901ccb7faf57be102edbedfc45809adcbe25620c?d=identicon)[olsai](/maintainers/olsai)

---

Top Contributors

[![olsai](https://avatars.githubusercontent.com/u/73751177?v=4)](https://github.com/olsai "olsai (10 commits)")

---

Tags

dtohyperfhyperf dtohyperf swagger

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/hashwallet-dto/health.svg)

```
[![Health](https://phpackages.com/badges/hashwallet-dto/health.svg)](https://phpackages.com/packages/hashwallet-dto)
```

###  Alternatives

[tangwei/dto

php hyperf dto

21131.5k2](/packages/tangwei-dto)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
