Download the PHP package fab2s/dt0 without Composer
On this page you can find all versions of the php package fab2s/dt0. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package dt0
Short Description Immutable DTOs with bidirectional casting. No framework required. 8x faster than the alternative.
License MIT
Homepage https://github.com/fab2s/dt0
Informations about the package dt0
Dt0
Immutable PHP DTOs with bidirectional casting. No framework required. ~8x faster than the alternative.
Dt0 (DeeTO) is a PHP 8.1+ Data Transfer Object implementation that uses native readonly properties for true immutability. One #[Cast] attribute handles input transformation, output formatting, defaults, and property renaming. Compiled once per class, fast always.
Quick Start
Installation
For Laravel model casting integration, see laravel-dt0.
Why Dt0
- Real immutability, enforced by PHP. Native
readonlyproperties — accidental writes cause fatal errors, not silent bugs. - One attribute to rule them all.
#[Cast]handles input transformation, output formatting, defaults, and property renaming in a single, composable attribute. - Framework-agnostic. Use anywhere PHP runs — including standalone validation powered by Laravel's validation engine, without the framework.
- Compiled once, fast always. Reflection and metadata processed once per class, then cached. ~8x faster than spatie/laravel-data for typical operations.
Creating Instances
| Method | Input | On Failure |
|---|---|---|
make(...$args) |
Named/positional args | Throws |
fromArray(array) |
Associative array | Throws |
fromJson(string) |
JSON string | Throws |
fromGz(string) |
Gzipped JSON | Throws |
from(mixed) |
Array, JSON, or Dt0 | Throws |
tryFrom(mixed) |
Array, JSON, or Dt0 | Returns null |
Custom constructors with promoted properties are supported. See Creating Instances for constructors, new vs factory methods, and edge cases.
Casting
Dt0 supports bidirectional casting: transform values on the way in (hydration) and out (serialization).
Class-level casting with #[Casts]:
Built-in types — Enums and nested Dt0 classes are handled automatically, no caster needed:
Available Casters
| Caster | Description |
|---|---|
ScalarCaster |
Cast to int, float, bool, string |
JsonCaster |
Decode JSON on input, encode on output |
TrimCaster |
Trim strings (ltrim, rtrim, custom characters) |
Base64Caster |
Decode base64 on input, encode on output |
DateTimeCaster |
Parse to DateTime or DateTimeImmutable |
DateTimeFormatCaster |
Format DateTime for output |
CarbonCaster |
Parse to Carbon (requires nesbot/carbon) |
Dt0Caster |
Cast to nested Dt0 instances |
ArrayOfCaster |
Cast typed arrays (Dt0, Enum, or scalar) |
ClassCaster |
Instantiate arbitrary classes |
MathCaster |
Precision numbers (requires fab2s/math) |
CasterCollection |
Chain multiple casters in a pipeline |
See Casters Documentation for detailed usage, bidirectional casting, and custom casters.
Validation
Dt0 provides standalone validation powered by Laravel's validation engine — no Laravel framework required. In Laravel applications, it auto-detects the framework and uses Laravel's validator transparently.
Requires illuminate/validation and illuminate/translation (v11+):
Rules can be defined at three levels with clear priority: property #[Rule] > class #[Rules] > #[Validate] rules. The ValidatorInterface is open for custom implementations.
See Validation Documentation for locale configuration, custom translations, and custom validators.
Output
Output Filtering
Declarative output control with #[With] is also supported. See Output Documentation for details.
Immutable Operations
Property Renaming
Map between external names (APIs, databases) and internal property names:
All renameTo values are automatically added to renameFrom, ensuring round-trip consistency.
Default Values
Resolution order: provided value > Cast default > nullable default > promoted parameter default.
Attribute Inheritance
Dt0 resolves attributes up the parent class chain — both property-level and class-level:
See Inheritance Documentation for multi-level inheritance, class attribute inheritance, and override patterns.
Performance
Reflection and attribute metadata compiled once per class, per process. Subsequent instantiations reuse cached data with zero reflection overhead.
Benchmarks
Dt0 vs spatie/laravel-data (PHP 8.4, 10,000 iterations)
| Operation | Dt0 | spatie/laravel-data | Speedup |
|---|---|---|---|
| Simple DTO (8 props, 5 casts) | 141.6 µs | 1,158 µs | ~8.2x faster |
| Complex DTO (nested + arrays) | 741.9 µs | 3,628 µs | ~4.9x faster |
| Round-trip (json->dto->json) | 248.4 µs | 2,004 µs | ~8.1x faster |
Repeated serialization (same instance):
| Operation | Dt0 | spatie/laravel-data | Speedup |
|---|---|---|---|
| toArray() (simple) | 3.6 µs | 679.4 µs | ~188.7x faster |
| toArray() (nested) | 3.6 µs | 2,056 µs | ~571.1x faster |
| toJson() | 2.8 µs | 681.8 µs | ~243.5x faster |
Output caching delivers 188-571x improvements when serializing the same instance multiple times (API + logging, event sourcing, queue + monitoring, caching layers).
Extending
Dt0's attributes, casters, and validators are extensible. See Extending Documentation for interfaces, abstract classes, and compiled metadata access.
Exceptions
All exceptions extend ContextException with structured context for debugging:
| Exception | Usage |
|---|---|
Dt0Exception |
General DTO errors (missing properties, invalid input) |
CasterException |
Casting failures |
AttributeException |
Attribute configuration errors |
Requirements
- PHP 8.1, 8.2, 8.3, or 8.4
Dependencies
fab2s/context-exception- Contextual exceptionsfab2s/enumerate- Enum utilities
Optional
illuminate/validation+illuminate/translation- For standaloneValidatornesbot/carbon- ForCarbonCasterfab2s/math- ForMathCaster
Contributing
Contributions are welcome. Please open issues and submit pull requests.
License
Dt0 is open-sourced software licensed under the MIT license.
All versions of dt0 with dependencies
ext-zlib Version *
fab2s/context-exception Version ^2.0|^3.0
fab2s/enumerate Version ^0.0.1