codememory/dto
Auto-hydrate PHP/Symfony DTOs from request/array data using rules and decorators. Supports name conversion (e.g., snake_case), enum casting via attributes, and event hooks during processing. Build a manager with caching and reflection for fast mapping.
#[Attribute]) requires PHP 8.0+. This could introduce minor version compatibility risks if using older Laravel versions.AfterProcessedTypeDecoratorsEvent) allows for custom validation logic, which can integrate with Laravel’s validation pipeline (e.g., Illuminate\Validation\Validator).Illuminate\Http\Request validation by providing fine-grained control over DTO hydration (e.g., nested objects, enum mapping, custom type casting).App\Http\Resources.FormRequest classes by adding decorator-based validation (e.g., @SymfonyValidation) without mixing concerns with Laravel’s validation rules.SymfonyValidation) introduce indirect dependencies. A Laravel TPM must evaluate whether these are acceptable or if alternatives (e.g., Laravel’s Illuminate\Validation) should be prioritized.ReflectorManager) could introduce runtime latency if overused. Caching (via FilesystemAdapter) mitigates this but adds storage dependencies.Use Case Fit:
Alternatives:
SymfonyValidation) a blocker for adoption?Maintenance:
Performance:
Team Skills:
#[Attribute], Illuminate\Validation).SymfonyValidation), ensure Symfony’s validator component is installed (symfony/validator). For pure Laravel, these can be replaced with custom decorators.hydrate() calls.#[Rule] or #[Validated] with #[Property\SymfonyValidation] (or custom decorators).// Before (Laravel Form Request)
public function rules(): array { return ['email' => 'required|email']; }
// After (DTO with Decorator)
#[Property\SymfonyValidation([
new Assert\NotBlank(),
new Assert\Email()
])]
public string $email;
FormRequest::failedValidation() with custom AfterProcessedTypeDecoratorsEvent listeners for validation.$eventDispatcher->addListener(
AfterProcessedTypeDecoratorsEvent::class,
fn($event) => throw_if_invalid($event->data)
);
Illuminate\Http\Request to use the DTO manager:
public function hydrateRequest(array $data): MyDto {
return $this->dtoManager->hydrate(MyDto::class, $data);
}
DataTransferObjectManager can be registered as a Laravel service provider:
public function register(): void {
$this->app->singleton(DataTransferObjectManager::class, fn($app) => new DataTransferObjectManager(
new ReflectorManager(new FilesystemAdapter('codememory')),
// ... other dependencies
));
}
validator alongside Laravel’s Illuminate\Validation for hybrid validation.#[Property\LaravelValidation]).new MyDto($data) with hydrate(MyDto::class, $data).ToEnum, ToDateTime).FormRequest to DTO + event-driven validation.hydrate() method across the codebase.How can I help you explore Laravel packages today?