morningtrain / data-transfer-object-casters
A package containing Data Transfer Object casters, for use with spatie/data-transfer-object
Fund package maintenance!
morningtrain
Installs: 2 271
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 6
Forks: 1
Open Issues: 2
Requires
- php: ^8.0
- illuminate/contracts: ^8.37 | ^9.0 | ^10.0
- spatie/data-transfer-object: ^3.7
- spatie/laravel-package-tools: ^1.4.3
Requires (Dev)
- nunomaduro/collision: ^5.3 | ^6.1 | ^7.0
- orchestra/testbench: ^6.15 | ^7.22 | ^8.0
- pestphp/pest: ^1.22
- pestphp/pest-plugin-laravel: ^1.4
- spatie/laravel-ray: ^1.32
- vimeo/psalm: ^5.8
This package is auto-updated.
Last update: 2024-11-24 12:43:50 UTC
README
Installation
You can install the package via composer:
composer require morningtrain/data-transfer-object-casters
Usage
To use the provided casters, add an attribute CastWith, to the property you want to cast.
class DTO extends DataTransferObject { #[CastWith(BoolCaster::class)] public bool $bool; }
Bool
The Boolean caster is used to cast the provided value to a boolean.
It uses FILTER_VALIDATE_BOOL, see the php docs for more information
Date
The DateCaster will attempt to cast the provided value into a Carbon instance, using the provided format.
If no format is specified 'd.m.Y H:i:s' will be used.
use Morningtrain\DataTransferObjectCasters\Casters\DateCaster; class DTO extends DataTransferObject { #[CastWith(DateCaster::class, format: 'Y-m-d')] public Carbon $date; }
Int
The IntCaster casts the provided value to an integer using (int) cast.
Trim
The TrimCaster will trim the provided value, so no whitespace remains around the value.
UppercaseFirst
The UppercaseFirstCaster will uppercase the first letter of the provided value, similar to php's ucfirst function.
Optionally, the caster can lowercase the rest of the string, like so.
use Morningtrain\DataTransferObjectCasters\Casters\UppercaseFirstCaster; class DTO extends DataTransferObject { #[CastWith(UppercaseFirstCaster::class, lower: true)] public string $ucFirstString; }
If the provided value is not a string, it will return the provided value.
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.