Add FactoryInterface and work on factory classes for entities.
This commit is contained in:
parent
0a1077c2dc
commit
75e4f5f808
@ -3,10 +3,12 @@
|
||||
namespace Slovocast\Domain\Entity;
|
||||
|
||||
use Slovocast\Domain\Entity;
|
||||
use Slovocast\Domain\Record;
|
||||
|
||||
class Channel
|
||||
{
|
||||
use Entity;
|
||||
use Record;
|
||||
|
||||
public function __construct(
|
||||
private string $name,
|
||||
|
@ -64,18 +64,10 @@ class User
|
||||
}
|
||||
|
||||
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']);
|
||||
}
|
||||
|
||||
|
41
app/src/Domain/Factory/ChannelFactory.php
Normal file
41
app/src/Domain/Factory/ChannelFactory.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Slovocast\Domain\Factory;
|
||||
|
||||
use Slovocast\Domain\Entity\Channel;
|
||||
use Slovocast\Domain\FactoryInterface;
|
||||
|
||||
class ChannelFactory implements FactoryInterface
|
||||
{
|
||||
public static function fromArray(array $props): Channel
|
||||
{
|
||||
$channel = new Channel(
|
||||
$props['name'],
|
||||
$props['slug'] ?? '',
|
||||
$props['description'] ?? '',
|
||||
$props['link'] ?? '',
|
||||
$props['language'] ?? '',
|
||||
$props['copyright'] ?? '',
|
||||
$props['explicit'] ?? false
|
||||
);
|
||||
|
||||
if (isset($props['id'])) {
|
||||
$channel->setId($props['id']);
|
||||
}
|
||||
|
||||
if (isset($props['createdAt'])) {
|
||||
$channel->setCreatedAt($props['createdAt']);
|
||||
}
|
||||
|
||||
if (isset($props['updatedAt'])) {
|
||||
$channel->setUpdatedAt($props['updatedAt']);
|
||||
}
|
||||
|
||||
return $channel;
|
||||
}
|
||||
|
||||
public static function toArray(Channel $channel): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
47
app/src/Domain/Factory/UserFactory.php
Normal file
47
app/src/Domain/Factory/UserFactory.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?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,
|
||||
];
|
||||
}
|
||||
}
|
21
app/src/Domain/FactoryInterface.php
Normal file
21
app/src/Domain/FactoryInterface.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace Slovocast\Domain;
|
||||
|
||||
use ReturnTypeWillChange;
|
||||
|
||||
interface FactoryInterface
|
||||
{
|
||||
/**
|
||||
* @param array $props Properties required to build the object
|
||||
* @return object The object to be built
|
||||
*/
|
||||
#[ReturnTypeWillChange]
|
||||
public static function fromArray(array $props);
|
||||
|
||||
/**
|
||||
* @param object $obj
|
||||
* @return array
|
||||
*/
|
||||
public static function toArray($obj): array;
|
||||
}
|
@ -10,20 +10,28 @@ trait Record
|
||||
protected ?DateTimeImmutable $createdAt;
|
||||
protected ?DateTime $updatedAt;
|
||||
|
||||
public function setCreatedAt(DateTimeImmutable $createdAt): void
|
||||
public function setCreatedAt(DateTimeImmutable|string $createdAt): void
|
||||
{
|
||||
if (is_string($createdAt)) {
|
||||
$this->createdAt = new DateTimeImmutable($createdAt);
|
||||
} else {
|
||||
$this->createdAt = $createdAt;
|
||||
}
|
||||
}
|
||||
|
||||
public function getCreatedAt(): DateTimeImmutable
|
||||
{
|
||||
return $this->createdAt;
|
||||
}
|
||||
|
||||
public function setUpdatedAt(DateTime $updatedAt): void
|
||||
public function setUpdatedAt(DateTime|string $updatedAt): void
|
||||
{
|
||||
if (is_string($updatedAt)) {
|
||||
$this->updatedAt = new DateTime($updatedAt);
|
||||
} else {
|
||||
$this->updatedAt = $updatedAt;
|
||||
}
|
||||
}
|
||||
|
||||
public function getUpdatedAt(): DateTime
|
||||
{
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace Slovocast\Tests\Controller\User;
|
||||
|
||||
use Slovocast\Domain\Factory\UserFactory;
|
||||
use Slovocast\Tests\TestCase;
|
||||
use Slovocast\Domain\Repository\User\UserRepositoryInterface;
|
||||
use Slovocast\Domain\Entity\User;
|
||||
@ -12,7 +13,7 @@ class RegisterUserActionTest extends TestCase
|
||||
{
|
||||
protected function getUser(): User
|
||||
{
|
||||
return User::fromArray([
|
||||
return UserFactory::fromArray([
|
||||
'email' => 'dave@slovocast.com',
|
||||
'name' => 'Dave SH',
|
||||
'password' => 'hashed_password'
|
||||
|
@ -12,6 +12,7 @@ use Slim\Psr7\Request as SlimRequest;
|
||||
use Slim\Psr7\Uri;
|
||||
use Slovocast\Bootstrap;
|
||||
use Slovocast\Domain\Entity\User;
|
||||
use Slovocast\Domain\Factory\UserFactory;
|
||||
|
||||
/**
|
||||
* This is the common test harness pattern lifted right out of the official
|
||||
@ -78,6 +79,6 @@ class TestCase extends PHPUnit_TestCase
|
||||
];
|
||||
}
|
||||
|
||||
return User::fromArray($user);
|
||||
return UserFactory::fromArray($user);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user