arkye/support

Arkye Support Package

2.3.0 2023-06-13 20:24 UTC

This package is auto-updated.

Last update: 2024-04-13 22:25:11 UTC


README

Build Status Total Downloads Latest Stable Version License

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.