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;
|
namespace Slovocast\Domain\Entity;
|
||||||
|
|
||||||
use Slovocast\Domain\Entity;
|
use Slovocast\Domain\Entity;
|
||||||
|
use Slovocast\Domain\Record;
|
||||||
|
|
||||||
class Channel
|
class Channel
|
||||||
{
|
{
|
||||||
use Entity;
|
use Entity;
|
||||||
|
use Record;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private string $name,
|
private string $name,
|
||||||
|
@ -64,18 +64,10 @@ class User
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isset($props['createdAt'])) {
|
if (isset($props['createdAt'])) {
|
||||||
if (is_string($props['createdAt'])) {
|
|
||||||
$props['createdAt'] = new DateTimeImmutable($props['createdAt']);
|
|
||||||
}
|
|
||||||
|
|
||||||
$user->setCreatedAt($props['createdAt']);
|
$user->setCreatedAt($props['createdAt']);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($props['updatedAt'])) {
|
if (isset($props['updatedAt'])) {
|
||||||
if (is_string($props['updatedAt'])) {
|
|
||||||
$props['updatedAt'] = new DateTime($props['updatedAt']);
|
|
||||||
}
|
|
||||||
|
|
||||||
$user->setUpdatedAt($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,9 +10,13 @@ trait Record
|
|||||||
protected ?DateTimeImmutable $createdAt;
|
protected ?DateTimeImmutable $createdAt;
|
||||||
protected ?DateTime $updatedAt;
|
protected ?DateTime $updatedAt;
|
||||||
|
|
||||||
public function setCreatedAt(DateTimeImmutable $createdAt): void
|
public function setCreatedAt(DateTimeImmutable|string $createdAt): void
|
||||||
{
|
{
|
||||||
$this->createdAt = $createdAt;
|
if (is_string($createdAt)) {
|
||||||
|
$this->createdAt = new DateTimeImmutable($createdAt);
|
||||||
|
} else {
|
||||||
|
$this->createdAt = $createdAt;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCreatedAt(): DateTimeImmutable
|
public function getCreatedAt(): DateTimeImmutable
|
||||||
@ -20,9 +24,13 @@ trait Record
|
|||||||
return $this->createdAt;
|
return $this->createdAt;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setUpdatedAt(DateTime $updatedAt): void
|
public function setUpdatedAt(DateTime|string $updatedAt): void
|
||||||
{
|
{
|
||||||
$this->updatedAt = $updatedAt;
|
if (is_string($updatedAt)) {
|
||||||
|
$this->updatedAt = new DateTime($updatedAt);
|
||||||
|
} else {
|
||||||
|
$this->updatedAt = $updatedAt;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getUpdatedAt(): DateTime
|
public function getUpdatedAt(): DateTime
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Slovocast\Tests\Controller\User;
|
namespace Slovocast\Tests\Controller\User;
|
||||||
|
|
||||||
|
use Slovocast\Domain\Factory\UserFactory;
|
||||||
use Slovocast\Tests\TestCase;
|
use Slovocast\Tests\TestCase;
|
||||||
use Slovocast\Domain\Repository\User\UserRepositoryInterface;
|
use Slovocast\Domain\Repository\User\UserRepositoryInterface;
|
||||||
use Slovocast\Domain\Entity\User;
|
use Slovocast\Domain\Entity\User;
|
||||||
@ -12,7 +13,7 @@ class RegisterUserActionTest extends TestCase
|
|||||||
{
|
{
|
||||||
protected function getUser(): User
|
protected function getUser(): User
|
||||||
{
|
{
|
||||||
return User::fromArray([
|
return UserFactory::fromArray([
|
||||||
'email' => 'dave@slovocast.com',
|
'email' => 'dave@slovocast.com',
|
||||||
'name' => 'Dave SH',
|
'name' => 'Dave SH',
|
||||||
'password' => 'hashed_password'
|
'password' => 'hashed_password'
|
||||||
|
@ -12,6 +12,7 @@ use Slim\Psr7\Request as SlimRequest;
|
|||||||
use Slim\Psr7\Uri;
|
use Slim\Psr7\Uri;
|
||||||
use Slovocast\Bootstrap;
|
use Slovocast\Bootstrap;
|
||||||
use Slovocast\Domain\Entity\User;
|
use Slovocast\Domain\Entity\User;
|
||||||
|
use Slovocast\Domain\Factory\UserFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is the common test harness pattern lifted right out of the official
|
* 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