There is no license information available for the latest version (dev-master) of this package.

This package is intended for the adoption of the DTO schema in Laravel, which is a system for controlling incoming and outgoing data, in collaboration with the Laravel formRequest, or the Eloquent model

dev-master 2024-07-03 09:10 UTC

This package is auto-updated.

Last update: 2024-09-03 09:33:26 UTC


README

The project

This package is intended for the adoption of the DTO schema in Laravel, which is a system for controlling incoming and outgoing data, in collaboration with the Laravel formRequest, or the Eloquent model.

Installation

To install the project, you should install it from composer :

composer require masterskill/dto

After that, you should publish the config file.

The DTO class

The Dto class has a different properties to handle. Exemple of the DTO :

<?php

namespace App\Http\Dto;


class UserDto {
    public function __construct(
        public readonly string $name, public readonly string $email
    ) {}
}

Or, eventially, the DTO can implements the IRequestBasedDto or IModelBasedDto interfaces to add some functionnality.

<?php

namespace App\Http\Dto;
use Masterskill\Dto\Interfaces\IRequestBasedDto;
use Masterskill\Dto\Interfaces\IModelBasedDto;
use Illuminate\Http\Request;
use App\Models\User;

class UserDto implements IRequestBased, IModelBasedDto {
    public function __construct(
        public readonly string $name, public readonly string $email
    ) {}

    public static function fromRequest(Request $request) {
        return new static("John Doe" , "john.doe@example.com")
    }

    public static function fromModel(User $user) {
        return new static($user->name , $user->email)
    }
}