PHP code example of mackrais-organization / property-transform

1. Go to this page and download the library: Download mackrais-organization/property-transform library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mackrais-organization / property-transform example snippets


use MackRais\PropertyTransform\Transform;

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

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

$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)

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;
}

$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"

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

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

use Psr\Container\ContainerInterface;

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