arkye/support

Arkye 支持包

2.3.0 2023-06-13 20:24 UTC

This package is auto-updated.

Last update: 2024-09-13 23:44:17 UTC


README

Build Status Total Downloads Latest Stable Version License

需求

请注意,我们使用 PHP 属性,因此您需要使用高于 8.0 的 PHP 版本。

安装

composer require arkye/support

文档


数据传输对象 (DTO)

转换器

Arkye 数据传输对象扩展了 spatie 的实现(访问仓库),如果您对以下插件有任何疑问或问题,请查看他们的文档。

Arkye 的实现具有默认转换到 \Carbon\Carbon 和 \Illuminate\Support\Collection,因此您不需要在每个类上自己进行操作。

use Carbon\Carbon;
use Illuminate\Support\Collection;
use Arkye\Support\DataTransferObject\DataTransferObject;

class MyDTO extends DataTransferObject
{
    public Carbon $createdAt;
    public Collection $tags;
}

$dto = new MyDTO(createdAt: '2000-01-01', tags: ['tag1', 'tag2']);

我们还提供了一个 CaseTransformer 类属性,用于在 DTO 创建或转换时转换键的字母大小写。当与 api 一起工作时,这很有用,有时我们在与外部通信时使用蛇形大小写,但在内部代码中使用驼峰大小写。

转换器的第一个参数指定 DTO 属性的大小写,第二个参数指定将转换成数组或 JSON 时的字母大小写。

use Arkye\Support\Data\Data\Attributes\Transformers\CaseTransformer;use Arkye\Support\DataTransferObject\DataTransferObject;use Carbon\Carbon;

#[CaseTransformer('camel', 'snake')]
class MyDTO extends DataTransferObject
{
    public Carbon $createdAt;
    public string $fullName;
}

// Request came with created_at and full_name
$dto = new MyDTO(request()->all());

// Do some work with DTO...

// Will be converted to snake case again
response()->json($dto->toArray());

如果您想要转换输入但保持转换到数组或 JSON 时的字母大小写,只需将 CaseTransformation 的第二个参数留空(输出)。

use Arkye\Support\Data\Data\Attributes\Transformers\CaseTransformer;use Arkye\Support\DataTransferObject\DataTransferObject;use Carbon\Carbon;

#[CaseTransformer('camel')]
class MyDTO extends DataTransferObject
{
    public Carbon $createdAt;
    public string $fullName;
}

// Request came with created_at and full_name
$dto = new MyDTO(request()->all());

// Do some work with DTO...

// Will be {"createdAt": "something", "fullName": "something"}
die($dto->tojson());

贡献

感谢您考虑为 Arkye 贡献!您可以在这里阅读贡献指南。

行为准则

请审查并遵守行为准则

许可证

Arkye 支持是开源软件,根据MIT 许可证授权。