Download the PHP package fedejuret/dto-builder without Composer
On this page you can find all versions of the php package fedejuret/dto-builder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download fedejuret/dto-builder
More information about fedejuret/dto-builder
Files in fedejuret/dto-builder
Package dto-builder
Short Description PHP Library to build DTO's using attributes
License MIT
Informations about the package dto-builder
DTO Builder
DTO Builder is a lightweight PHP library that streamlines the process of creating and populating Data Transfer Objects (DTOs) using PHP 8+ attributes. It enables dynamic property hydration, automatic validation, and conversion to arrays with minimal boilerplate.
โจ Features
- ๐ฆ Instantiate DTOs from arrays
- ๐งช Attribute-based property validation
- ๐๏ธ Convert DTOs to arrays effortlessly
- โ PHP 8 attributes for configuration
- ๐งน Extendable and easy to integrate
๐ฆ Installation
Install the package via Composer:
๐ Getting Started
1. Define Your DTO
This requires calling setters manually for each property. With DTO Builder, you can simplify this dramatically.
2. Use Attributes for Property Mapping and Validation
๐งฐ Traits
Loadable
Enables the loadFromArray(array $data): self
method, which automatically maps input arrays to DTO properties based on defined attributes.
Arrayable
Adds a toArray(): array
method, which serializes the DTO to a key-value array using the attribute-defined property names.
๐ท๏ธ Attributes
#[Property]
Defines mapping behavior for each DTO property.
Attribute | Description |
---|---|
name |
Overrides the array key name for hydration and serialization. |
setter |
Specifies a custom setter method. |
getter |
Specifies a custom getter method. |
โ Built-in Validations
Use validation attributes to ensure the integrity of your DTO before hydration.
Attribute | Description |
---|---|
#[Required] |
Ensures the value is present in the input. |
#[IsString] |
Must be of type string . |
#[IsEmail] |
Must be a valid email address. |
#[IsDate] |
Must be a valid date string. |
#[IsBoolean] |
Must be a boolean (true /false ). |
#[IsNumeric] |
Must be numeric. |
#[IsInteger] |
Must be an integer. |
#[IsDouble] |
Must be a double/float. |
#[IsArray] |
Must be an array. |
#[IsDomain] |
Must be a valid domain name. |
#[IsIPAddress] |
Must be a valid IP address. |
#[Length(min, max)] |
Validates the string length. |
#[IsIn(availableValues)] |
Validates if value is in array. |
#[IsRegex(pattern)] |
Validates if values match with regex. |
#[IsUuid] |
Validates if values is a valid UUID. |
๐งผ Built-in Sanitizations
DTO Builder allows you to sanitize each property before validation and usage.
#[HtmlSanitization]
Removes all HTML tags from a string, while optionally keeping specific safe tags.
- Uses
strip_tags()
internally. - Useful for allowing basic formatting while stripping harmful tags like
<script>
.
#[SecureSanitization]
A defensive sanitization designed to help mitigate OWASP Top 10 vulnerabilities like XSS.
- Removes all tags unless specific ones are whitelisted.
- Escapes special characters (
<
,>
,'
,"
,&
) usinghtmlspecialchars
. - Removes invisible characters and normalizes whitespace.
- Optional parameter lets you keep specific HTML tags:
๐ ๏ธ Custom Sanitizations
You can create your own sanitizers by implementing the SanitizationInterface
and marking them as attributes.
Usage:
Custom sanitizations allow you to apply reusable transformations to your data before validation or further processing.
๐ฅช Running Tests
Clone the repository and install dependencies:
๐ค Contributing
Contributions are welcome! To get started:
- Fork the repository
- Create a new branch (
git checkout -b feature/your-feature
) - Commit your changes (
git commit -m 'Add new feature'
) - Push to the branch (
git push origin feature/your-feature
) - Open a Pull Request
Please follow the PSR-12 coding standard when contributing.
๐ License
This project is licensed under the MIT License. See the LICENSE file for more information.
๐ข Contact
- Email: [email protected]
- Website: federicojuretich.com
- LinkedIn: @federicojuretich