Simple Slim DTO

Easily describe your data with minimal boilerplate. No magic, no transformations — just pure, readable schema

Problem

Large DTO objects often come with a lot of unnecessary functionality that isn't used or needed for the DTO itself. As a result, we end up with a "Swiss army knife" of an object. But a DTO, at its core, is simply a schema that describes an object, with no extra magic or transformations inside. This is exactly the approach I often look for in my code when managing data.

Currently, in such cases, I use the sptatie/laravel-data library, which provides a lot of functionality, but I only use about 10-15% of it. Don't get me wrong, I love the library and recommend it to everyone (it's even on my list of favorites on this site 😌). It's just that sometimes its functionality is overkill, and all I really want is to describe the object without all the extra complexity. That's where my library comes in.

Installation

composer r pepperfm/ssd-for-laravel

Usage

Extends your class from BaseDto and define your data

class ResponseWrapperDto extends BaseDto
{
    public array $data;

    public array $links;

    public array $metaData;
}

Base case

You can create the object like this:

ResponseWrapperDto::make([
    'data' => $response['data'],
    'links' => $response['links'],
    'meta_data' => $response['meta'],
])
use Pepperfm\DonationAlerts\Attributes\ToIterable;

class ResponseWrapperDto extends BaseDto
{
    #[ToIterable(ResponseDataDto::class)]
    public array $data;

    public array $links;

    public array $metaData;
}

Output:

/**
 * @var array<array-key, ResponseDataDto> $data 
 */
$data = $dto->data;

Extended cases

ResponseWrapperDto::make(literal(
    data: $response['data'],
    links: $response['links'],
    meta: $response['meta'],
))
new ResponseWrapperDto(
    data: $response['data'],
    links: $response['links'],
    meta: $response['meta'],
)
ResponseWrapperDto::make([
    'data' => $response['data'],
    'links' => $response['links'],
    'meta' => $response['meta'],
])
new ResponseWrapperDto([
    'data' => $response['data'],
    'links' => $response['links'],
    'meta' => $response['meta'],
])

Only for collections (non-native array)

use Pepperfm\DonationAlerts\Attributes\ToIterable;

class ResponseWrapperDto extends BaseDto
{
    #[ToIterable(ResponseDataDto::class, \Illuminate\Database\Eloquent\Collection::class)]
    public \Illuminate\Support\Collection $data;

    public array $links;

    public array $metaData;
}

Output:

/** @var \Illuminate\Database\Eloquent\Collection<array-key, ResponseDataDto> $data */
$data = $dto->data;

In this version this package will transform any snake_case variables to camelCase

I started with this because I like this approach