<?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);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.