48 lines
1.2 KiB
PHP
48 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace Slovocast\Domain\Factory;
|
|
|
|
use Slovocast\Domain\Entity\User;
|
|
use Slovocast\Domain\FactoryInterface;
|
|
use DateTimeImmutable;
|
|
use DateTime;
|
|
|
|
class UserFactory implements FactoryInterface
|
|
{
|
|
public static function fromArray(array $props): User
|
|
{
|
|
$user = new User($props['email'], $props['password'], $props['name']);
|
|
|
|
if (isset($props['id'])) {
|
|
$user->setId($props['id']);
|
|
}
|
|
|
|
if (isset($props['createdAt'])) {
|
|
if (is_string($props['createdAt'])) {
|
|
$props['createdAt'] = new DateTimeImmutable($props['createdAt']);
|
|
}
|
|
|
|
$user->setCreatedAt($props['createdAt']);
|
|
}
|
|
|
|
if (isset($props['updatedAt'])) {
|
|
if (is_string($props['updatedAt'])) {
|
|
$props['updatedAt'] = new DateTime($props['updatedAt']);
|
|
}
|
|
|
|
$user->setUpdatedAt($props['updatedAt']);
|
|
}
|
|
|
|
return $user;
|
|
}
|
|
|
|
public static function toArray(User $user): array
|
|
{
|
|
return [
|
|
'name' => $user->getName(),
|
|
'email' => $user->getEmail(),
|
|
'id' => $user->getId() ?? null,
|
|
];
|
|
}
|
|
}
|