pepperfm / ssd-for-laravel
Simple Slim DTO
Requires
- php: ^8.2
- illuminate/http: >=10.0
- illuminate/support: >=10.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.59
- laravel/pint: ^1.16
- orchestra/testbench: ^v8.14|^9.11
- pestphp/pest: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpunit/phpunit: ^10.0|^11.0
- spatie/laravel-ray: ^1.37
Conflicts
- laravel/framework: <10.20.0
README
Easily describe your data with minimal boilerplate. No magic, no transformations — just pure, readable schema.
Typehint your data in camelCase. No hidden transformations or data mutations. Clean, readable, and intuitive code. Just a simple class to map your data, following the KISS💋 principle.
Tip
Installation
composer r pepperfm/ssd-for-laravel
Usage
Extends your class from BaseDto and define your data
class ResponseWrapperDto extends BaseDto { public array $data; public array $links; public array $metaData; }
Base case
You can create the object like this:
ResponseWrapperDto::make([ 'data' => $response['data'], 'links' => $response['links'], 'meta_data' => $response['meta'], ])
use Pepperfm\DonationAlerts\Attributes\ToIterable; class ResponseWrapperDto extends BaseDto { #[ToIterable(ResponseDataDto::class)] public array $data; public array $links; public array $metaData; }
Output:
/** * @var array<array-key, ResponseDataDto> $data */ $data = $dto->data;
Extended cases
ResponseWrapperDto::make(literal( data: $response['data'], links: $response['links'], meta: $response['meta'], ))
new ResponseWrapperDto( data: $response['data'], links: $response['links'], meta: $response['meta'], )
ResponseWrapperDto::make([ 'data' => $response['data'], 'links' => $response['links'], 'meta' => $response['meta'], ])
new ResponseWrapperDto([ 'data' => $response['data'], 'links' => $response['links'], 'meta' => $response['meta'], ])
Warning
Only for collections (non-native array)
use Pepperfm\DonationAlerts\Attributes\ToIterable; class ResponseWrapperDto extends BaseDto { #[ToIterable(ResponseDataDto::class, \Illuminate\Database\Eloquent\Collection::class)] public \Illuminate\Support\Collection $data; public array $links; public array $metaData; }
Output:
/** @var \Illuminate\Database\Eloquent\Collection<array-key, ResponseDataDto> $data */ $data = $dto->data;
Note
In this version this package will transform any snake_case variables to camelCase
I started with this because I like this approach
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email Damon3453@yandex.ru instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
Laravel Package Boilerplate
This package was generated using the Laravel Package Boilerplate.