pepperfm/ssd-for-laravel

Simple Slim DTO

Maintainers

Package info

github.com/pepperfm/ssd-for-laravel

pkg:composer/pepperfm/ssd-for-laravel

Transparency log

Statistics

Installs: 33 801

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.5 2026-07-03 14:23 UTC

This package is auto-updated.

Last update: 2026-07-03 14:24:02 UTC


README

Latest Version on Packagist Total Downloads on Packagist GitHub Actions

Simple Slim DTO helps you describe small DTO classes for Laravel responses, array payloads, and typed data wrappers.

The package maps array-like input into public typed properties, supports snake_case and camelCase keys, casts nested DTOs, converts iterable items, and serializes DTOs back to arrays or JSON.

Tip

Full Package Description

Requirements

  • PHP ^8.4

Installation

composer require pepperfm/ssd-for-laravel

Basic Usage

Extend BaseDto and describe the payload with public typed properties.

use Pepperfm\Ssd\BaseDto;

class ResponseWrapperDto extends BaseDto
{
    public array $data;

    public LinksDto $links;

    public MetaDto $meta;
}

Create DTOs from arrays, objects, Arrayable, Traversable, or named arguments.

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

Nested DTO properties are cast automatically.

use Pepperfm\Ssd\BaseDto;

class LinksDto extends BaseDto
{
    public ?string $first = null;

    public ?string $last = null;

    public ?string $prev = null;

    public ?string $next = null;
}

class MetaDto extends BaseDto
{
    public int $currentPage;

    public int $total;
}

Iterable Casting

Use ToIterable when a property contains a list of items.

The first argument is the collection type. Use ToIterable::ARRAY for a native array. The second argument is optional and defines the item type.

use Pepperfm\Ssd\Attributes\ToIterable;
use Pepperfm\Ssd\BaseDto;

class ResponseDataDto extends BaseDto
{
    public string $id;

    public string $name;
}

class ResponseWrapperDto extends BaseDto
{
    #[ToIterable(ToIterable::ARRAY, ResponseDataDto::class)]
    public array $data = [];
}
/** @var array<array-key, ResponseDataDto> $items */
$items = $dto->data;

For Laravel collections, pass the collection class as the first argument.

use Illuminate\Support\Collection;
use Pepperfm\Ssd\Attributes\ToIterable;
use Pepperfm\Ssd\BaseDto;

class ResponseWrapperDto extends BaseDto
{
    #[ToIterable(Collection::class, ResponseDataDto::class)]
    public Collection $data;
}
/** @var Collection<array-key, ResponseDataDto> $items */
$items = $dto->data;

If the item type is omitted, iterable items are kept as-is.

use Pepperfm\Ssd\Attributes\ToIterable;
use Pepperfm\Ssd\BaseDto;

class TagsDto extends BaseDto
{
    #[ToIterable(ToIterable::ARRAY)]
    public array $tags = [];
}

The item type may be another BaseDto class or any class that can be instantiated from one value.

Key Mapping

Input keys can be camelCase or snake_case. Output keys are camelCase by default. Use MapName when the external key must be explicit.

use Pepperfm\Ssd\Attributes\MapName;
use Pepperfm\Ssd\BaseDto;

class UserDto extends BaseDto
{
    #[MapName('external_name')]
    public string $displayName;
}
$dto = UserDto::make([
    'external_name' => 'Ada',
]);

$dto->displayName; // Ada
$dto->external_name; // Ada
$dto->toArray(); // ['external_name' => 'Ada']

Collections and Helpers

Convert many payloads into DTO instances with collect().

$items = ResponseDataDto::collect($response['data']);

Use only() and except() on serialized DTO output.

$dto->only('data', 'meta');
$dto->except('links');

Serialization

$dto->toArray();
$dto->jsonSerialize();
json_encode($dto);

Testing

composer test
composer lint

Changelog

Please see CHANGELOG for more information about what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email Damon3453@yandex.ru instead of using the issue tracker.

Credits

License

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

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.