Split EntityConverter to separate strategies

Reflection turned out to be bad, since I cannot implement my own method
in entities, like Post::setUser() instead of Post::setUserId().
This commit is contained in:
Marcin Kurczewski 2014-09-15 09:25:11 +02:00
parent b53e2752ca
commit f6df009085
9 changed files with 111 additions and 72 deletions

View file

@ -6,17 +6,15 @@ abstract class AbstractDao implements ICrudDao
protected $pdo;
protected $fpdo;
protected $tableName;
protected $entityName;
protected $entityConverter;
public function __construct(
\Szurubooru\DatabaseConnection $databaseConnection,
$tableName,
$entityName)
\Szurubooru\Dao\EntityConverters\IEntityConverter $entityConverter)
{
$this->entityConverter = new EntityConverter($entityName);
$this->entityName = $entityName;
$this->tableName = $tableName;
$this->entityConverter = $entityConverter;
$this->pdo = $databaseConnection->getPDO();
$this->fpdo = new \FluentPDO($this->pdo);

View file

@ -1,62 +0,0 @@
<?php
namespace Szurubooru\Dao;
final class EntityConverter
{
private $entityName;
private $reflectionClass;
private $getterMethods = [];
private $setterMethods = [];
public function __construct($entityName)
{
$this->entityName = $entityName;
$this->reflectionClass = new \ReflectionClass($this->entityName);
$reflectionMethods = $this->reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC);
foreach ($reflectionMethods as $reflectionMethod)
{
if (preg_match('/^(get|is)(\w+)$/', $reflectionMethod->getName(), $matches))
{
$columnName = lcfirst($matches[2]);
$this->getterMethods[] = [$reflectionMethod, $columnName];
}
else if (preg_matcH('/^set(\w+)$/', $reflectionMethod->getName(), $matches))
{
$columnName = lcfirst($matches[1]);
$this->setterMethods[] = [$reflectionMethod, $columnName];
}
}
}
public function toArray(\Szurubooru\Entities\Entity $entity)
{
$arrayEntity = [];
foreach ($this->getterMethods as $kv)
{
list ($reflectionMethod, $columnName) = $kv;
$value = $reflectionMethod->invoke($entity);
$arrayEntity[$columnName] = $value;
}
return $arrayEntity;
}
public function toEntity($arrayEntity)
{
if ($arrayEntity === null)
return null;
$args = [$arrayEntity['id']];
$entity = $this->reflectionClass->newInstanceArgs($args);
foreach ($this->setterMethods as $kv)
{
list ($reflectionMethod, $columnName) = $kv;
$value = $arrayEntity[$columnName];
$reflectionMethod->invoke($entity, $value);
}
return $entity;
}
}

View file

@ -0,0 +1,9 @@
<?php
namespace Szurubooru\Dao\EntityConverters;
interface IEntityConverter
{
public function toArray(\Szurubooru\Entities\Entity $entity);
public function toEntity(array $array);
}

View file

@ -0,0 +1,21 @@
<?php
namespace Szurubooru\Dao\EntityConverters;
class PostEntityConverter implements IEntityConverter
{
public function toArray(\Szurubooru\Entities\Entity $entity)
{
return
[
'id' => $entity->getId(),
'name' => $entity->getName(),
];
}
public function toEntity(array $array)
{
$entity = new \Szurubooru\Entities\Post($array['id']);
$entity->setName($array['name']);
return $entity;
}
}

View file

@ -0,0 +1,25 @@
<?php
namespace Szurubooru\Dao\EntityConverters;
class TokenEntityConverter implements IEntityConverter
{
public function toArray(\Szurubooru\Entities\Entity $entity)
{
return
[
'id' => $entity->getId(),
'name' => $entity->getName(),
'purpose' => $entity->getPurpose(),
'additionalData' => $entity->getAdditionalData(),
];
}
public function toEntity(array $array)
{
$entity = new \Szurubooru\Entities\Token($array['id']);
$entity->setName($array['name']);
$entity->setPurpose($array['purpose']);
$entity->setAdditionalData($array['additionalData']);
return $entity;
}
}

View file

@ -0,0 +1,39 @@
<?php
namespace Szurubooru\Dao\EntityConverters;
class UserEntityConverter implements IEntityConverter
{
public function toArray(\Szurubooru\Entities\Entity $entity)
{
return
[
'id' => $entity->getId(),
'name' => $entity->getName(),
'email' => $entity->getEmail(),
'emailUnconfirmed' => $entity->getEmailUnconfirmed(),
'passwordHash' => $entity->getPasswordHash(),
'accessRank' => $entity->getAccessRank(),
'registrationTime' => $entity->getRegistrationTime(),
'lastLoginTime' => $entity->getLastLoginTime(),
'avatarStyle' => $entity->getAvatarStyle(),
'browsingSettings' => $entity->getBrowsingSettings(),
'accountConfirmed' => $entity->isAccountConfirmed(),
];
}
public function toEntity(array $array)
{
$entity = new \Szurubooru\Entities\User($array['id']);
$entity->setName($array['name']);
$entity->setEmail($array['email']);
$entity->setEmailUnconfirmed($array['emailUnconfirmed']);
$entity->setPasswordHash($array['passwordHash']);
$entity->setAccessRank($array['accessRank']);
$entity->setRegistrationTime($array['registrationTime']);
$entity->setLastLoginTime($array['lastLoginTime']);
$entity->setAvatarStyle($array['avatarStyle']);
$entity->setBrowsingSettings($array['browsingSettings']);
$entity->setAccountConfirmed($array['accountConfirmed']);
return $entity;
}
}

View file

@ -3,8 +3,12 @@ namespace Szurubooru\Dao;
final class PostDao extends AbstractDao implements ICrudDao
{
public function __construct(\Szurubooru\DatabaseConnection $databaseConnection)
public function __construct(
\Szurubooru\DatabaseConnection $databaseConnection)
{
parent::__construct($databaseConnection, 'posts', \Szurubooru\Entities\Post::class);
parent::__construct(
$databaseConnection,
'posts',
new \Szurubooru\Dao\EntityConverters\PostEntityConverter());
}
}

View file

@ -5,7 +5,10 @@ class TokenDao extends AbstractDao
{
public function __construct(\Szurubooru\DatabaseConnection $databaseConnection)
{
parent::__construct($databaseConnection, 'tokens', \Szurubooru\Entities\Token::class);
parent::__construct(
$databaseConnection,
'tokens',
new \Szurubooru\Dao\EntityConverters\TokenEntityConverter());
}
public function findByName($tokenName)

View file

@ -3,10 +3,12 @@ namespace Szurubooru\Dao;
class UserDao extends AbstractDao implements ICrudDao
{
public function __construct(
\Szurubooru\DatabaseConnection $databaseConnection)
public function __construct(\Szurubooru\DatabaseConnection $databaseConnection)
{
parent::__construct($databaseConnection, 'users', \Szurubooru\Entities\User::class);
parent::__construct(
$databaseConnection,
'users',
new \Szurubooru\Dao\EntityConverters\UserEntityConverter());
}
public function findByName($userName)