Skip to content

mackrais-organization/property-transform

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

PHP Property Transform

Scrutinizer Quality Score Build Status License Latest Stable Version Latest Unstable Version CodeCov StyleCI Gitter Total Downloads Monthly Downloads Daily Downloads PHP Version Require

Overview

The Property Transform library provides a powerful way to automatically transform DTO properties using PHP attributes.
This allows you to apply transformations such as trimming, formatting, and sanitization directly on properties, while also supporting nested object transformation and dependency injection.

πŸ”₯ Features

  • βœ… Transform DTO properties using PHP attributes
  • βœ… Supports multiple transformations per property (e.g., trim + lowercase)
  • βœ… Applies transformations to nested objects automatically
  • βœ… Supports callable PHP functions (trim, strtolower, etc.)
  • βœ… Works with Dependency Injection (DI) for custom transformations
  • βœ… Supports class methods as transformers ([ClassName::class, 'method'])
  • βœ… Lightweight & efficient – no runtime overhead

πŸ“Œ Table of Contents


πŸ›  Installation

Install via Composer:

composer require mackrais-organization/property-transform

πŸš€ Usage

1️⃣ Basic DTO Transformation

You can use built-in PHP functions as transformers by adding attributes:

use MackRais\PropertyTransform\Transform;

class UserDto
{
    #[Transform('trim')]
    #[Transform('strtolower')]
    public string $name;

    #[Transform('intval')]
    public string $age;
}

Then, apply transformations:

$dto = new UserDto();
$dto->name = '  John Doe ';
$dto->age = '25';

$dataTransformer = new DataTransformer(new TransformerFactory($container));
$dataTransformer->transform($dto);

echo $dto->name; // Output: "john doe"
echo $dto->age; // Output: 25 (converted to integer)

🎯 Examples

2️⃣ Nested DTO Transformation

If a DTO contains another DTO, transformations will apply recursively:

class AddressDto
{
    #[Transform('trim')]
    #[Transform('strtolower')]
    public string $city;
}

class UserDto
{
    #[Transform('trim')]
    #[Transform('strtolower')]
    public string $name;

    #[Transform] // Required for nested transformation
    public AddressDto $address;
}

Usage:

$address = new AddressDto();
$address->city = '  New York ';

$dto = new UserDto();
$dto->name = '  Jane Doe ';
$dto->address = $address;

$dataTransformer->transform($dto);

echo $dto->name; // Output: "jane doe"
echo $dto->address->city; // Output: "new york"

3️⃣ Using Class Methods as Transformers

You can also define a custom transformer method inside a class:

class SecuritySanitizer
{
    public function sanitize(?string $input): string
    {
        return strip_tags((string) $input);
    }
}

And use it as a transformer:

class UserDto
{
    #[Transform([SecuritySanitizer::class, 'sanitize'])]
    public string $bio;
}

πŸ— Dependency Injection Support

Transformers can be registered as public services in Symfony or any DI container.

Example using PSR-11 Container:

use Psr\Container\ContainerInterface;

$container = new SomePsr11Container();
$factory = new TransformerFactory($container);
$dataTransformer = new DataTransformer($factory);

Now, any service-based transformer (like SecuritySanitizer) will be automatically resolved.


πŸ“œ License

Property Transform is released under the MIT License. See the LICENSE.md file for details.

About

The Property Transform library provides a powerful way to automatically transform DTO properties using PHP attributes. This allows you to apply transformations such as trimming, formatting, and sanitization directly on properties, while also supporting nested object transformation and dependency injection.

Resources

License

Contributing

Stars

Watchers

Forks

Sponsor this project

Packages

 
 
 

Contributors

Languages