arkye/support

Arkye Support Package
1,544
Install
composer require arkye/support
Latest Version:2.3.0
PHP:^8.1|^8.2
License:MIT
Last Updated:Jun 13, 2023
Links: GitHub  ·  Packagist
Maintainer: gus

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.

Related Packages

artiss/support

Support package for Laravel.

10 0
prologue/support

Prologue Support is an extension for Illuminate Support

16,764 15
michele-angioni/support

Support is a Laravel package which promotes the use of best practices and design...

1,460 18
dragon-code/support

Support package is a collection of helpers and tools for any project.

11,602,262 23