Download the PHP package neuron-php/dto without Composer
On this page you can find all versions of the php package neuron-php/dto. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download neuron-php/dto
More information about neuron-php/dto
Files in neuron-php/dto
Package dto
Short Description Easy, dynamic DTO creation, validation and mapping.
License MIT
Informations about the package dto
Neuron-PHP DTO
A powerful Data Transfer Object (DTO) library for PHP 8.4+ that provides dynamic DTO creation, comprehensive validation, and flexible data mapping capabilities with support for nested structures and YAML configuration.
Table of Contents
- Installation
- Quick Start
- Core Features
- DTO Configuration
- Creating DTOs
- Validation
- Data Mapping
- Property Types
- Collections
- Advanced Usage
- Testing
- Best Practices
- More Information
Installation
Requirements
- PHP 8.4 or higher
- Composer
- symfony/yaml (^6.4)
- neuron-php/validation (^0.7.0)
Install via Composer
Quick Start
1. Define Your DTO Structure
Create a YAML configuration file (user.yaml):
2. Create and Use the DTO
Core Features
- Dynamic DTO Creation: Generate DTOs from YAML configuration files
- Comprehensive Validation: Built-in validators for 20+ data types
- Nested Structures: Support for complex, hierarchical data models
- DTO Composition: Reuse DTOs by referencing existing DTO definitions
- Data Mapping: Transform external data structures to DTOs
- Type Safety: Strict type checking and validation
- Collections: Handle arrays of objects with validation
- JSON Export: Easy serialization to JSON format
- Custom Validators: Extend with custom validation logic
DTO Configuration
Basic Structure
DTOs are configured using YAML files with property definitions:
Complete Example
Creating DTOs
From YAML Configuration
Programmatic Creation
Nested Objects
DTO Composition (Reusable DTOs)
You can create reusable DTO definitions and reference them in other DTOs, making it easy to share common structures like timestamps, addresses, or user records across multiple DTOs.
Creating Reusable DTOs
First, create standalone DTO definition files:
common/timestamps.yaml
common/address.yaml
common/user.yaml
Using Referenced DTOs
Reference these DTOs in your main DTO definition using type: dto with a ref parameter:
Working with Composed DTOs
Benefits of DTO Composition
- Reusability: Define common structures once, use them everywhere
- Consistency: Ensure the same validation rules across all uses
- Maintainability: Update the definition in one place
- Performance: Referenced DTOs are cached automatically
- Type Safety: Full validation support for nested structures
Path Resolution
Referenced paths are resolved relative to the parent DTO file:
Validation
Built-in Validators
The DTO component includes comprehensive validation for each property type:
Validation Rules
Length Validation
Range Validation
Pattern Validation
Enum Validation
Custom Validation
Data Mapping
Mapper Configuration
Create a mapping configuration (mapping.yaml):
Using the Mapper
Dynamic Mapping
Property Types
Supported Types
| Type | Description | Validation |
|---|---|---|
string |
Text values | Length, pattern |
integer |
Whole numbers | Range, min, max |
float |
Decimal numbers | Range, precision |
boolean |
True/false values | Type checking |
array |
Lists of items | Item validation |
object |
Nested objects | Property validation |
dto |
Referenced DTO | Full DTO validation |
email |
Email addresses | RFC compliance |
url |
URLs | URL format |
date |
Date values | Date format |
date_time |
Date and time | DateTime format |
time |
Time values | Time format |
currency |
Money amounts | Currency format |
uuid |
UUIDs | UUID v4 format |
ip_address |
IP addresses | IPv4/IPv6 |
phone_number |
Phone numbers | International format |
name |
Person names | Name validation |
ein |
EIN numbers | US EIN format |
upc |
UPC codes | UPC-A format |
numeric |
Any number | Numeric validation |
image |
Image data | Base64/data URI, MIME type |
base64 |
Base64 encoded data | Base64 format |
Type Examples
Collections
Array of Objects
Array of Primitives
Advanced Usage
Working with Images
The image type provides validation for base64-encoded image data and data URIs. It supports common image formats including JPEG, PNG, GIF, WebP, and SVG.
Image Property Configuration
Using Image Properties in Code
Supported Image Formats
The image validator automatically detects and validates the following formats:
- JPEG/JPG - Detected by JPEG file signature
- PNG - Detected by PNG file signature
- GIF - Supports both GIF87a and GIF89a
- WebP - Modern image format
- SVG - XML-based vector graphics (disabled by default for security - see below)
Image Validation Features
- Base64 Encoding: Validates proper base64 encoding
- Data URI Support: Accepts
data:image/type;base64,format - MIME Type Detection: Automatically detects image type from file signatures
- Format Validation: Ensures the data actually contains valid image content
- Size Constraints: Can be configured with maximum file size limits (via custom validator)
- SVG Security: SVG images are disabled by default as they can contain embedded scripts (XSS risk)
Security Considerations for SVG
SVG images are disabled by default because they are XML-based and can contain:
- JavaScript code via
<script>tags - Event handlers that execute JavaScript
- External resource references
- CSS that could be used for attacks
If you need to accept SVG images, you must:
- Explicitly enable SVG support in your validator configuration
- Sanitize SVG content before storage or display
- Serve SVG files with appropriate Content Security Policy headers
- Consider using a dedicated SVG sanitization library
To enable SVG support (use with caution):
Complex DTO Example
Custom DTO Class
DTO Factory with Caching
Testing
Unit Testing DTOs
Testing Mappers
Best Practices
DTO Design
Validation Strategy
Error Handling
Reusable DTOs
Performance Optimization
Integration Examples
API Request Validation
Database Integration
More Information
- Neuron Framework: neuronphp.com
- GitHub: github.com/neuron-php/dto
- Packagist: packagist.org/packages/neuron-php/dto
License
MIT License - see LICENSE file for details
All versions of dto with dependencies
symfony/yaml Version ^6.4
neuron-php/validation Version ^0.7.0
neuron-php/logging Version 0.9.*
myclabs/deep-copy Version ^1.12