soyhuce/data-transfer-object-casts 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

soyhuce/data-transfer-object-casts

最新稳定版本:0.1.1

Composer 安装命令:

composer require soyhuce/data-transfer-object-casts

包简介

Common casts for spatie/data-transfer-object

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status GitHub PHPStan Action Status Total Downloads

Common casts for spatie/data-transfer-object

Installation

You can install the package via composer:

composer require soyhuce/data-transfer-object-casts

Usage

BooleanCaster

Casts the input into boolean, if applicable.

use Soyhuce\DataTransferObjectCasts\BooleanCaster;
use Spatie\DataTransferObject\Attributes\DefaultCast;
use Spatie\DataTransferObject\DataTransferObject;

#[DefaultCast('bool', BooleanCaster::class)]
class MyDTO extends DataTransferObject
{
    public bool $bool;
}

$dto = new MyDTO(
    bool: 'true',
);

$dto->bool; // true

CarbonImmutableCaster

Cast the input into a CarbonImmutable instance. If the input is not a string, it will be returned as is.

By default, the format is '!Y-m-d H:i:s'.

use Carbon\CarbonImmutable;
use Soyhuce\DataTransferObjectCasts\CarbonImmutableCaster;
use Spatie\DataTransferObject\Attributes\CastWith;
use Spatie\DataTransferObject\Attributes\DefaultCast;
use Spatie\DataTransferObject\DataTransferObject;

#[DefaultCast(CarbonImmutable::class, CarbonImmutableCaster::class)]
class MyDTO extends DataTransferObject
{
    public CarbonImmutable $dateTime;

    #[CastWith(CarbonImmutableCaster::class, '!Y-m-d')]
    public CarbonImmutable $date;
}

$dto = new MyDTO(
    dateTime: '2022-08-11 14:44:45',
    date: '2022-08-01',
);

$dto->dateTime; // CarbonImmutable instance
$dto->date; // CarbonImmutable instance

StringEnumCaster

Cast the input into a backed string enum.

use Soyhuce\DataTransferObjectCasts\StringEnumCaster;
use Spatie\DataTransferObject\Attributes\CastWith;
use Spatie\DataTransferObject\DataTransferObject;

#[CastWith(StringEnumCaster::class)]
enum StringEnum: string
{
    case ok = 'ok';
    case nok = 'nok';
}

class MyDTO extends DataTransferObject
{
    public StringEnum $stringEnum;
}

$dto = new MyDTO(
    stringEnum: 'ok',
);

$dto->stringEnum; // StringEnum::ok

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 1.71k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-08-11