Download the PHP package ufo-tech/dto-transformer without Composer
On this page you can find all versions of the php package ufo-tech/dto-transformer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ufo-tech/dto-transformer
More information about ufo-tech/dto-transformer
Files in ufo-tech/dto-transformer
Package dto-transformer
Short Description The library provides tools for two-way transformation of DTO objects ⇄ arrays, respecting typing, contracts, and flexible transformation logic.
License MIT
Homepage https://docs.ufo-tech.space/bin/view/docs/DTOTransformer/?language=en
Informations about the package dto-transformer
🧩 ufo/dto-transformer
A PHP library that provides tools for bidirectional transformation between DTO objects ⇄ arrays, with full type safety, contracts, and flexible conversion logic. Ideal for JSON-RPC, REST APIs, CLI tools, and any context where data is passed as arrays.
📦 Core Components:
🔁 DTOTransformer
Central service for:
- transforming arrays into DTOs via
fromArray(...)
; - serializing DTOs to arrays via
toArray(...)
.
⚙️ IDTOFromArrayTransformer
+ IDTOToArrayTransformer
Interfaces for custom transformers that encapsulate specific logic for unpacking/packing particular DTOs.
🧱 BaseDTOFromArrayTransformer
Base class with a default fromArray()
implementation that includes:
- support check via
supportsClass(...)
; - key renaming and data normalization;
- constructor argument resolution and instantiation.
🚨 NotSupportDTOException
Thrown when a transformer does not support the provided DTO class.
🧬 Contracts & Traits:
IArrayConstructible
+ ArrayConstructibleTrait
For DTO classes that support construction from arrays:
- Maps constructor arguments automatically;
- Works via
ReflectionParameter
.
IArrayConvertible
+ ArrayConvertibleTrait
For DTO classes that can be serialized to arrays:
- Automatically serializes public and readonly properties;
- Supports field aliasing and
#[DTOAttributesEnum::Hidden]
.
🔌 Usage Example:
🔧 Custom Transformer Example
This is a sample custom transformer implementing IDTOFromArrayTransformer
for transforming an OrderDTO
where amount
must be cast to float and createdAt
to DateTimeImmutable
.
🧩 With attribute-based transformer:
This transformer:
- strictly follows
IDTOFromArrayTransformer
; - encapsulates complex conversion logic;
- delegates array-to-object conversion to the core transformer.
🧠 Library Advantages
- Full support for PHP 8.3 type system;
- Flexible logic via pluggable custom transformers;
- Type-safe, self-descriptive, and composable architecture;
- Simple attribute-based field control without code duplication;
- Standardized DTO handling for SOA and microservices environments.