1. Go to this page and download the library: Download event4u/data-helpers 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/ */
event4u / data-helpers example snippets
// From this messy API response...
$apiResponse = [
'data' => [
'departments' => [
['users' => [['email' => '[email protected]'], ['email' => '[email protected]']]],
['users' => [['email' => '[email protected]']]],
],
],
];
// ...to this clean result in a few lines
$accessor = new DataAccessor($apiResponse);
$emails = $accessor->get('data.departments.*.users.*.email');
// ['[email protected]', '[email protected]', '[email protected]']
// ❌ Without Data Helpers
$emails = [];
foreach ($data['departments'] ?? [] as $dept) {
foreach ($dept['users'] ?? [] as $user) {
if (isset($user['email'])) {
$emails[] = $user['email'];
}
}
}
// ✅ With Data Helpers
$emails = $accessor->get('departments.*.users.*.email');
// Works everywhere - no framework needed
$dto = UserDto::fromArray(['name' => 'John', 'email' => '[email protected]']);
$json = json_encode($dto);
// 1. Controller Injection - Automatic validation & filling
class UserController extends Controller
{
public function store(UserDto $dto): JsonResponse
{
// $dto is automatically validated and filled from request
$user = User::create($dto->toArray());
return response()->json($user, 201);
}
}
// 2. Eloquent Model Integration
$user = User::find(1);
$dto = UserDto::fromModel($user); // From Model
$dto->toModel($user); // To Model
// 3. Laravel-Specific Attributes
class UserProfileDto extends SimpleDto
{
public function __construct(
public readonly string $name,
#[WhenAuth] // Only when authenticated
public readonly ?string $email = null,
#[WhenCan('edit-posts')] // Only with permission
public readonly ?string $editUrl = null,
#[WhenRole('admin')] // Only for admins
public readonly ?array $adminPanel = null,
) {}
}
// 4. Artisan Commands
php artisan make:dto UserDto
php artisan dto:typescript
php artisan dto:migrate-spatie
// 1. Controller Injection - Automatic validation & filling
class UserController extends AbstractController
{
#[Route('/users', methods: ['POST'])]
public function create(UserDto $dto): JsonResponse
{
// $dto is automatically validated and filled from request
$user = new User();
$dto->toEntity($user);
$this->entityManager->persist($user);
$this->entityManager->flush();
return $this->json($user, 201);
}
}
// 2. Doctrine Entity Integration
$user = $this->entityManager->find(User::class, 1);
$dto = UserDto::fromEntity($user); // From Entity
$dto->toEntity($user); // To Entity
// 3. Symfony-Specific Attributes
class UserProfileDto extends SimpleDto
{
public function __construct(
public readonly string $name,
#[WhenGranted('ROLE_ADMIN')] // Only with permission
public readonly ?string $email = null,
#[WhenSymfonyRole('ROLE_MODERATOR')] // Only for moderators
public readonly ?array $moderationPanel = null,
) {}
}
// 4. Console Commands
php bin/console make:dto UserDto
php bin/console dto:typescript
skip-test
use event4u\DataHelpers\SimpleDto\Attributes\NoCasts;
// Default: Automatic type casting enabled
class ReadmeUserDto extends SimpleDto
{
public function __construct(
public readonly string $name,
public readonly string $email,
public readonly int $age,
public readonly AddressDto $address, // Nested DTO (auto-cast by default)
) {}
}
// Automatic type conversion by default
$user = ReadmeUserDto::fromArray([
'name' => 'John',
'email' => '[email protected]',
'age' => '30', // String "30" → int 30 (automatic)
'address' => ['city' => 'Berlin'], // Array → AddressDto (automatic)
]);
// Disable automatic casting for better performance
#[NoCasts]
class StrictUserDto extends SimpleDto
{
public function __construct(
public readonly string $name,
public readonly int $age, // Must be int, no conversion
public readonly AddressDto $address, // Must be AddressDto instance, no conversion
) {}
}
// Multi-format serialization
$json = $user->toJson(); // JSON
$xml = $user->toXml(); // XML
$yaml = $user->toYaml(); // YAML
$csv = $user->toCsv(); // CSV
use event4u\DataHelpers\SimpleDto;
use event4u\DataHelpers\SimpleDto\Attributes\HasObject;
use event4u\DataHelpers\SimpleDto\SimpleDtoObjectTrait;
// Plain PHP object
class Product
{
public int $id;
public string $name;
public float $price;
}
// DTO with plain object integration
#[HasObject(Product::class)]
class ProductDto extends SimpleDto
{
use SimpleDtoObjectTrait;
public function __construct(
public readonly int $id,
public readonly string $name,
public readonly float $price,
) {}
}
// Object → DTO
$product = new Product();
$product->id = 1;
$product->name = 'Laptop';
$product->price = 999.99;
$dto = ProductDto::fromObject($product);
// DTO → Object
$newProduct = $dto->toObject(); // Uses HasObject attribute
class Customer
{
private int $id;
private string $name;
public function getId(): int { return $this->id; }
public function setId(int $id): void { $this->id = $id; }
public function getName(): string { return $this->name; }
public function setName(string $name): void { $this->name = $name; }
}
// fromObject() uses getters, toObject() uses setters
$dto = CustomerDto::fromObject($customer);
$newCustomer = $dto->toObject(Customer::class);
use event4u\DataHelpers\LiteDto;
use event4u\DataHelpers\LiteDto\Attributes\MapFrom;
use event4u\DataHelpers\LiteDto\Attributes\Hidden;
class UserDto extends LiteDto
{
public function __construct(
public readonly string $name,
#[MapFrom('email_address')]
public readonly string $email,
#[Hidden]
public readonly string $password,
) {}
}
$user = UserDto::from([
'name' => 'John',
'email_address' => '[email protected]',
'password' => 'secret',
]);
$array = $user->toArray();
// ['name' => 'John', 'email' => '[email protected]']
// password is hidden