diff --git a/src/Dao/AbstractDao.php b/src/Dao/AbstractDao.php index abc29e6b..75d53e7b 100644 --- a/src/Dao/AbstractDao.php +++ b/src/Dao/AbstractDao.php @@ -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); diff --git a/src/Dao/EntityConverter.php b/src/Dao/EntityConverter.php deleted file mode 100644 index cf58b717..00000000 --- a/src/Dao/EntityConverter.php +++ /dev/null @@ -1,62 +0,0 @@ -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; - } -} diff --git a/src/Dao/EntityConverters/IEntityConverter.php b/src/Dao/EntityConverters/IEntityConverter.php new file mode 100644 index 00000000..70f28211 --- /dev/null +++ b/src/Dao/EntityConverters/IEntityConverter.php @@ -0,0 +1,9 @@ + $entity->getId(), + 'name' => $entity->getName(), + ]; + } + + public function toEntity(array $array) + { + $entity = new \Szurubooru\Entities\Post($array['id']); + $entity->setName($array['name']); + return $entity; + } +} diff --git a/src/Dao/EntityConverters/TokenEntityConverter.php b/src/Dao/EntityConverters/TokenEntityConverter.php new file mode 100644 index 00000000..4f9f9645 --- /dev/null +++ b/src/Dao/EntityConverters/TokenEntityConverter.php @@ -0,0 +1,25 @@ + $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; + } +} diff --git a/src/Dao/EntityConverters/UserEntityConverter.php b/src/Dao/EntityConverters/UserEntityConverter.php new file mode 100644 index 00000000..a6a945b9 --- /dev/null +++ b/src/Dao/EntityConverters/UserEntityConverter.php @@ -0,0 +1,39 @@ + $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; + } +} diff --git a/src/Dao/PostDao.php b/src/Dao/PostDao.php index 94681e5d..41d72490 100644 --- a/src/Dao/PostDao.php +++ b/src/Dao/PostDao.php @@ -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()); } } diff --git a/src/Dao/TokenDao.php b/src/Dao/TokenDao.php index 16534d56..32438142 100644 --- a/src/Dao/TokenDao.php +++ b/src/Dao/TokenDao.php @@ -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) diff --git a/src/Dao/UserDao.php b/src/Dao/UserDao.php index 9bef3ca1..e919b95c 100644 --- a/src/Dao/UserDao.php +++ b/src/Dao/UserDao.php @@ -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)