Skip to content

Major refactor for v4 #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open

Major refactor for v4 #9

wants to merge 13 commits into from

Conversation

d8vjork
Copy link
Member

@d8vjork d8vjork commented May 19, 2025

RFC

<?php

use OpenSoutheners\LaravelDto\Attributes\Inject;
use Illuminate\Contracts\Auth\Authenticatable;
use App\Models\User;
use App\Models\Film;

final class FilmCreationData
{
    public function __construct(
        public readonly string $title,
        public readonly Film $film,
        public readonly string $excerpt = '',
        public string $details = '',
        #[Inject(Authenticatable::class)]
        public readonly ?User $createdBy = null,
        #[Inject('env')]
        public readonly ?string $environment = null,
    ) {
        // 
    }
}

// Setting current user so Authenticatable inject can be resolved
auth()->setUser(User::find(1));

use function OpenSoutheners\LaravelDto\map;

// Map single date value to a Carbon object, using built-in CarbonMapper
map('2025-05-22')->to(Carbon\Carbon::class);
map('2025-05-22')->to(Carbon\CarbonImmutable::class);
map('2025-05-22')->to(Illuminate\Support\Carbon::class);
map('1747939147')->to(Carbon\Carbon::class);
map(1747939147)->to(Carbon\Carbon::class);

// Map array of data to a object
$dto = map([
  'title' => 'Hello world',
  'film' => 1,
])->to(FilmCreationData::class);

// Map DTO instance with some array for more data (or default data) 
$dto = map(new FilmCreationData(
  title: 'Hello world',
  film: Film::find(1),
))->with(['details' => 'Lorem ipsum...'])->to();

// But this will throw an exception as readonly properties cannot be modified
$dto = map(new FilmCreationData(
  title: 'Hello world',
  film: Film::find(1),
))->with(['excerpt' => 'Lorem ipsum'])->to();

// Map DTO into a model creating and returning an instance with all data filled
$film = map($dto)->to(Film::class);

// Map DTO to a existing model instance updating its local properties (does not persist)
$film = map($dto)->to(Film::find(1));

Migration guide

POPO

  1. Remove all extends DataTransferObject
  2. Remove all implements ValidatedDataTransferObject and move the return content of public static function request() to a class attribute on top of this DTO class name (before) like the following #[Validate(MyFormRequest::class)], then add implements RouteTransferableObject
  3. Search for any ::fromArray or ::fromRequest() calls to these DTO classes then replace with map([])->to(MyDtoClass::class) or map($request)->to(MyDtoClass::class)

Logic separation on properties attributes

  1. Change all #[WithDefaultValue(Authenticatable::class)] with #[Inject(Authenticatable::class)]
  2. Change all #[BindModel] with #[FromRouteBinding] (and in case of having some content on the BindModel add below #[ModelWith(['relation1', 'relation2'])])

TypeScript types generation

Nothing from now...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant