create-dto

hieupvXmasEve's avatarfrom hieupvXmasEve

Generates a Data Transfer Object (DTO) class. Use when defining structured data for Contracts, API responses, or complex Action inputs.

⭐0stars🔀0forks📁View on GitHub🕐Updated Jan 6, 2026

When & Why to Use This Skill

This Claude skill automates the generation of PHP Data Transfer Objects (DTOs) to ensure type safety and structural consistency across application layers. It leverages modern PHP 8.2+ features like readonly classes and static factories to streamline data handling for APIs, contracts, and internal module communication, significantly reducing boilerplate code and manual errors.

Use Cases

  • Standardizing API responses by defining structured DTOs that ensure consistent data delivery to client applications and external consumers.
  • Enforcing type safety in complex business logic by using DTOs as validated inputs for Actions and Services within modular architectures.
  • Facilitating seamless data mapping from HTTP requests or raw arrays into immutable objects using built-in static factory methods like fromArray.
  • Improving code maintainability and IDE autocompletion by replacing loosely typed associative arrays with strictly typed, readonly DTO classes.
  • Defining clear data contracts between different microservices or modules to prevent integration errors and simplify debugging.
namecreate-dto
descriptionGenerates a Data Transfer Object (DTO) class. Use when defining structured data for Contracts, API responses, or complex Action inputs.
allowed-toolsWrite, Read

Create DTO

This skill generates a Data Transfer Object (DTO) to ensure type safety for data moving between Modules or Layers.

Instructions

  1. Identify Parameters:

    • Module: (e.g., Identity, Academic) or Shared if used across modules.
    • Purpose: What data does it hold? (e.g., StudentRegistrationData, CourseSummary).
    • Path: app/Modules/{Module}/DTO/{ClassName}.php or app/Shared/DTO/{ClassName}.php.
  2. Generate Code:

    • Namespace: App\Modules\{Module}\DTO or App\Shared\DTO.
    • Use readonly class (PHP 8.2+).
    • Public typed properties.
    • Optional: fromRequest or fromArray static factories.

Template

<?php

namespace App\Shared\DTO;

use Illuminate\Http\Request;

readonly class StudentGpaDTO
{
    public function __construct(
        public int $studentId,
        public string $studentName,
        public float $gpa,
        public int $totalCredits,
        public ?string $rank = null,
    ) {}

    public static function fromArray(array $data): self
    {
        return new self(
            studentId: $data['student_id'],
            studentName: $data['student_name'],
            gpa: (float) $data['gpa'],
            totalCredits: (int) $data['total_credits'],
            rank: $data['rank'] ?? null,
        );
    }
}

Best Practices

  • Immutability: Always use readonly classes.
  • Strict Typing: All properties must have types.
  • Shared vs Module: If it's the return type of a Shared Contract, put it in app/Shared/DTO. If it's internal to a module (e.g. Action input), put it in app/Modules/{Module}/DTO.