GithubHelp home page GithubHelp logo

geekwolverine / laravel-data-object-tools Goto Github PK

View Code? Open in Web Editor NEW

This project forked from juststeveking/laravel-data-object-tools

0.0 1.0 0.0 19 KB

A set of tools to make working with Data Transfer Objects easier in Laravel

License: MIT License

PHP 100.00%

laravel-data-object-tools's Introduction

Laravel Data Object Tools

Latest Version PHP Version Tests Total Downloads

This package is aimed to be a suite of tools to help working with DTOs even easier. It includes an artisan command to generate DTOs as well as a facade to help you hydrate them.

Installation

composer require juststeveking/laravel-data-object-tools

Usage

To generate a new DTO all you need to do is run the following artisan command:

php artisan make:dto MyDto

This will generate the following class: app/DataObjects/MyDto.php. By default this class will be a final class that implements a DataObjectContract which enforces a method toArray so that you can easily cast your DTOs to arrays.

If you are using PHP 8.2 however, you will by default get a readonly class generated, so that you do not have to declare each property as readonly.

To work with the hydration functionality you can either use Laravels DI container, or the ready made facade.

Using the container:

class StoreController
{
    public function __construct(
        private readonly HydratorContract $hydrator,
    ) {}
    
    public function __invoke(StoreRequest $request)
    {
        $model = Model::query()->create(
            attributes: $this->hydrator->fill(
                class: ModelObject::class,
                parameters: $request->validated(),
            )->toArray(),
        );
    }
}

To work with the facade, you can do something very similar:

class StoreController
{
    public function __invoke(StoreRequest $request)
    {
        $model = Model::query()->create(
            attributes: Hydrator::fill(
                class: ModelObject::class,
                parameters: $request->validated(),
            )->toArray(),
        );
    }
}

Object Hydration

Under the hood this package uses an EventSauce package, created by Frank de Jonge. It is possibly the best package I have found to hydrate objects nicely in PHP. You can find the documentation here if you would like to see what else you are able to do with the package to suit your needs.

Testing

To run the test suite:

composer run test

Credits

LICENSE

The MIT License (MIT). Please see License File for more information.

laravel-data-object-tools's People

Contributors

frankdejonge avatar juststeveking avatar ludoguenet avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.