Générateur de DTOs Typés Readonly en PHP 8.2+ depuis JSON
Convertissez des données JSON en DTOs modernes avec les readonly classes PHP 8.2+, promotion de constructeur et désérialiseurs 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();
}
}Classes Readonly PHP 8.2+ et Promotion de Constructeur
Tirez parti des classes readonly modernes de PHP 8.2 avec promotion de propriétés dans le constructeur et immuabilité totale pour une robustesse maximale.
Confidentialité 100% Côté Client
L'analyse JSON, la déduction d'AST et la génération de code PHP s'exécutent intégralement dans votre navigateur sans aucune transmission réseau.
Sérialisation et Désérialisation Bidirectionnelle
Génère des méthodes statiques fromArray() avec hydratation récursive des sous-DTOs, ainsi que toArray() et l'interface JsonSerializable.