Generador de DTOs Tipados Readonly en PHP 8.2+ desde JSON
Convierte datos JSON en modernos DTOs con clases readonly de PHP 8.2+, promoción de constructor y deserializadores fromArray.
<?php
declare(strict_types=1);
namespace App\DTO;
/**
* @param string[] $tags
*/
final readonly class UserDto implements \JsonSerializable
{
public function __construct(
public string $id,
public string $email,
public string $username,
public string $displayName,
public string $role,
public bool $isActive,
public TenantDto $tenant,
public PreferencesDto $preferences,
/** @var string[] */
public array $tags = [],
public \DateTimeImmutable $lastLoginAt,
public \DateTimeImmutable $createdAt,
) {}
public static function fromArray(array $data): self
{
return new self(
id: (string) ($data['id'] ?? $data['id'] ?? null ?? ''),
email: (string) ($data['email'] ?? $data['email'] ?? null ?? ''),
username: (string) ($data['username'] ?? $data['username'] ?? null ?? ''),
displayName: (string) ($data['display_name'] ?? $data['displayName'] ?? null ?? ''),
role: (string) ($data['role'] ?? $data['role'] ?? null ?? ''),
isActive: (bool) ($data['is_active'] ?? $data['isActive'] ?? null ?? false),
tenant: TenantDto::fromArray((array) ($data['tenant'] ?? [])),
preferences: PreferencesDto::fromArray((array) ($data['preferences'] ?? [])),
tags: array_map(fn($item) => (string) $item, (array) ($data['tags'] ?? [])),
lastLoginAt: isset($data['last_login_at']) ? new \DateTimeImmutable((string) $data['last_login_at']) : null,
createdAt: isset($data['created_at']) ? new \DateTimeImmutable((string) $data['created_at']) : null,
);
}
public function toArray(): array
{
return [
'id' => $this->id,
'email' => $this->email,
'username' => $this->username,
'display_name' => $this->displayName,
'role' => $this->role,
'is_active' => $this->isActive,
'tenant' => $this->tenant->toArray(),
'preferences' => $this->preferences->toArray(),
'tags' => $this->tags,
'last_login_at' => $this->lastLoginAt?->format(\DateTimeInterface::ATOM),
'created_at' => $this->createdAt?->format(\DateTimeInterface::ATOM),
];
}
public function jsonSerialize(): array
{
return $this->toArray();
}
}
final readonly class TenantDto implements \JsonSerializable
{
public function __construct(
public string $id,
public string $name,
public string $tier,
public int $activeSeats,
public string $billingContact,
) {}
public static function fromArray(array $data): self
{
return new self(
id: (string) ($data['id'] ?? $data['id'] ?? null ?? ''),
name: (string) ($data['name'] ?? $data['name'] ?? null ?? ''),
tier: (string) ($data['tier'] ?? $data['tier'] ?? null ?? ''),
activeSeats: (int) ($data['active_seats'] ?? $data['activeSeats'] ?? null ?? 0),
billingContact: (string) ($data['billing_contact'] ?? $data['billingContact'] ?? null ?? ''),
);
}
public function toArray(): array
{
return [
'id' => $this->id,
'name' => $this->name,
'tier' => $this->tier,
'active_seats' => $this->activeSeats,
'billing_contact' => $this->billingContact,
];
}
public function jsonSerialize(): array
{
return $this->toArray();
}
}
final readonly class PreferencesDto implements \JsonSerializable
{
public function __construct(
public string $theme,
public bool $notificationsEnabled,
public string $locale,
public string $timezone,
) {}
public static function fromArray(array $data): self
{
return new self(
theme: (string) ($data['theme'] ?? $data['theme'] ?? null ?? ''),
notificationsEnabled: (bool) ($data['notifications_enabled'] ?? $data['notificationsEnabled'] ?? null ?? false),
locale: (string) ($data['locale'] ?? $data['locale'] ?? null ?? ''),
timezone: (string) ($data['timezone'] ?? $data['timezone'] ?? null ?? ''),
);
}
public function toArray(): array
{
return [
'theme' => $this->theme,
'notifications_enabled' => $this->notificationsEnabled,
'locale' => $this->locale,
'timezone' => $this->timezone,
];
}
public function jsonSerialize(): array
{
return $this->toArray();
}
}Clases Readonly de PHP 8.2+ y Promoción de Constructor
Aprovecha las modernas clases readonly de PHP 8.2 con promoción de propiedades en constructor e inmutabilidad profunda para máxima seguridad de tipos.
Garantía de Privacidad 100% en Cliente
Todo el análisis JSON, deducción de AST y generación de PHP se ejecutan estrictamente en la memoria de su navegador sin enviar datos a servidores externos.
Serialización y Deserialización Bidireccional
Genera métodos estáticos fromArray() con hidratación recursiva de DTOs anidados, además de implementaciones toArray() y JsonSerializable.