https://github.com/morning-train/data-transfer-object-casters
Casters for spatie/data-transfer-object
https://github.com/morning-train/data-transfer-object-casters
Last synced: 11 months ago
JSON representation
Casters for spatie/data-transfer-object
- Host: GitHub
- URL: https://github.com/morning-train/data-transfer-object-casters
- Owner: Morning-Train
- License: mit
- Created: 2021-11-19T07:26:14.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-03-24T08:56:55.000Z (over 3 years ago)
- Last Synced: 2025-04-15T00:22:48.724Z (about 1 year ago)
- Language: PHP
- Homepage: https://morningtrain.dk/en/
- Size: 33.2 KB
- Stars: 4
- Watchers: 5
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
# A package containing DTO casters
[](https://packagist.org/packages/morningtrain/data-transfer-object-casters)

[](https://github.com/SimonJnsson/data-transfer-object-casters/actions/workflows/php-cs-fixer.yml/badge.svg)
[](https://packagist.org/packages/morningtrain/data-transfer-object-casters)
## Installation
You can install the package via composer:
```bash
composer require morningtrain/data-transfer-object-casters
```
## Usage
To use the provided casters, add an attribute CastWith, to the property you want to cast.
```php
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](https://www.php.net/manual/en/filter.filters.validate.php) 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.
```php
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](https://www.php.net/manual/en/function.ucfirst.php).
Optionally, the caster can lowercase the rest of the string, like so.
```php
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
```bash
composer test
```
## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.
## Security Vulnerabilities
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Credits
- [SimonJnsson](https://github.com/SimonJnsson)
- [All Contributors](../../contributors)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.