arkye / support
Arkye Support Package
Installs: 1 540
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.1|^8.2
- illuminate/support: ^9.0|^10.0
- spatie/laravel-data: ^3.6
Requires (Dev)
- laravel/framework: ^9.0|^10.0
- phpstan/phpstan: ^1.4
- phpunit/phpunit: ^9.5
README
Requirements
Is important to know that we use PHP Attributes, so you need to work with PHP versions higher than 8.0
Install
composer require arkye/support
Documentation
DataTransferObjects (DTO)
Casters
Arkye DataTransferObject extends spatie's implementation (go to repo) so check their docs if you have any questions or issues not related with addons below.
Arkye implementation has DefaultCast to \Carbon\Carbon and \Illuminate\Support\Collection, so you don't need to do it by yourself on each of your classes.
use Carbon\Carbon; use Illuminate\Support\Collection; use Arkye\Support\DataTransferObject\DataTransferObject; class MyDTO extends DataTransferObject { public Carbon $createdAt; public Collection $tags; } $dto = new MyDTO(createdAt: '2000-01-01', tags: ['tag1', 'tag2']);
We also have an CaseTransformer class attribute to convert key case on the DTO creation or transformation.
This is useful when working with apis, sometimes we use snake case with external communication,
but camel case on our internal code.
First argument of transformer specifies the DTO properties case, and second
the case when converting into array or json.
use Arkye\Support\Data\Data\Attributes\Transformers\CaseTransformer;use Arkye\Support\DataTransferObject\DataTransferObject;use Carbon\Carbon; #[CaseTransformer('camel', 'snake')] class MyDTO extends DataTransferObject { public Carbon $createdAt; public string $fullName; } // Request came with created_at and full_name $dto = new MyDTO(request()->all()); // Do some work with DTO... // Will be converted to snake case again response()->json($dto->toArray());
If you want to convert input but maintain case on conversion to array or json, just leave second argument of CaseTransformation empty (output):
use Arkye\Support\Data\Data\Attributes\Transformers\CaseTransformer;use Arkye\Support\DataTransferObject\DataTransferObject;use Carbon\Carbon; #[CaseTransformer('camel')] class MyDTO extends DataTransferObject { public Carbon $createdAt; public string $fullName; } // Request came with created_at and full_name $dto = new MyDTO(request()->all()); // Do some work with DTO... // Will be {"createdAt": "something", "fullName": "something"} die($dto->tojson());
Contributing
Thank you for considering contributing to Arkye! You can read the contribution guide here.
Code of Conduct
Please review and abide by the Code of Conduct.
License
Arkye Support is open-sourced software licensed under the MIT license.