PHPackages                             soyhuce/data-transfer-object-casts - 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. soyhuce/data-transfer-object-casts

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

soyhuce/data-transfer-object-casts
==================================

Common casts for spatie/data-transfer-object

0.1.1(3y ago)01.7k[2 PRs](https://github.com/Soyhuce/data-transfer-object-casts/pulls)MITPHPPHP ^8.1

Since Aug 11Pushed 2y ago3 watchersCompare

[ Source](https://github.com/Soyhuce/data-transfer-object-casts)[ Packagist](https://packagist.org/packages/soyhuce/data-transfer-object-casts)[ Docs](https://github.com/soyhuce/data-transfer-object-casts)[ GitHub Sponsors](https://github.com/soyhuce)[ RSS](/packages/soyhuce-data-transfer-object-casts/feed)WikiDiscussions main Synced today

READMEChangelog (2)Dependencies (9)Versions (5)Used By (0)

Common casts for spatie/data-transfer-object
============================================

[](#common-casts-for-spatiedata-transfer-object)

[![Latest Version on Packagist](https://camo.githubusercontent.com/03770700994d77125b445dd588998597ec05d3eaaa8b24cec152ab58fb8ba727/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736f79687563652f646174612d7472616e736665722d6f626a6563742d63617374732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/soyhuce/data-transfer-object-casts)[![GitHub Tests Action Status](https://camo.githubusercontent.com/7ecfc0b42055b58edc67ac8ce13dccb82c350a17d81117acca4bd336ea655f3b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f736f79687563652f646174612d7472616e736665722d6f626a6563742d63617374732f72756e2d74657374733f6c6162656c3d7465737473)](https://github.com/soyhuce/data-transfer-object-casts/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/02ef1b8f9ea0fc5af427ddc8c98701792515e4a37d145d021693f4222fb34833/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f736f79687563652f646174612d7472616e736665722d6f626a6563742d63617374732f466978253230504850253230636f64652532307374796c652532306973737565733f6c6162656c3d636f64652532307374796c65)](https://github.com/soyhuce/data-transfer-object-casts/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![GitHub PHPStan Action Status](https://camo.githubusercontent.com/1b985561b491e8746a4486f637b9a19ee6ef33127a9da5667de04ffa4231b2ec/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f736f79687563652f646174612d7472616e736665722d6f626a6563742d63617374732f5048505374616e3f6c6162656c3d7068707374616e)](https://github.com/soyhuce/data-transfer-object-casts/actions?query=workflow%3APHPStan+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/c1adcc2dc884acd24ec428302392e75fbae593f548361138d7cc3b40295ed3ef/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f736f79687563652f646174612d7472616e736665722d6f626a6563742d63617374732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/soyhuce/data-transfer-object-casts)

Common casts for spatie/data-transfer-object

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require soyhuce/data-transfer-object-casts
```

Usage
-----

[](#usage)

### BooleanCaster

[](#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

[](#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

[](#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
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Bastien Philippe](https://github.com/bastien-phi)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~17 days

Total

2

Last Release

1403d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/19587451?v=4)[SoyHuCe](/maintainers/SoyHuCe)[@Soyhuce](https://github.com/Soyhuce)

---

Top Contributors

[![bastien-phi](https://avatars.githubusercontent.com/u/10199039?v=4)](https://github.com/bastien-phi "bastien-phi (10 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (7 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (5 commits)")

---

Tags

laraveldata-transfer-objectsoyhuce

###  Code Quality

TestsPest

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/soyhuce-data-transfer-object-casts/health.svg)

```
[![Health](https://phpackages.com/badges/soyhuce-data-transfer-object-casts/health.svg)](https://phpackages.com/packages/soyhuce-data-transfer-object-casts)
```

###  Alternatives

[laravel/horizon

Dashboard and code-driven configuration for Laravel queues.

4.2k95.4M300](/packages/laravel-horizon)[illuminate/support

The Illuminate Support package.

630113.0M41.0k](/packages/illuminate-support)[solspace/craft-freeform

The most flexible and user-friendly form building plugin!

54681.3k17](/packages/solspace-craft-freeform)[erlandmuchasaj/laravel-gzip

Gzip your responses.

40146.5k2](/packages/erlandmuchasaj-laravel-gzip)[japanese-date/japanese-date

日本の暦、祝日を取り扱うライブラリ

1610.0k](/packages/japanese-date-japanese-date)

PHPackages © 2026

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