szurubooru/src/Dao/AbstractDao.php

157 lines
3.5 KiB
PHP
Raw Normal View History

<?php
namespace Szurubooru\Dao;
abstract class AbstractDao implements ICrudDao
{
2014-09-14 16:16:15 +02:00
protected $pdo;
protected $fpdo;
protected $tableName;
2014-08-30 17:10:45 +02:00
protected $entityConverter;
2014-08-30 17:10:45 +02:00
public function __construct(
\Szurubooru\DatabaseConnection $databaseConnection,
2014-09-14 16:16:15 +02:00
$tableName,
\Szurubooru\Dao\EntityConverters\IEntityConverter $entityConverter)
{
2014-09-21 09:35:43 +02:00
$this->setDatabaseConnection($databaseConnection);
$this->tableName = $tableName;
$this->entityConverter = $entityConverter;
$this->entityConverter->setEntityDecorator(function($entity)
{
$this->afterLoad($entity);
});
}
2014-09-21 09:35:43 +02:00
public function setDatabaseConnection(\Szurubooru\DatabaseConnection $databaseConnection)
{
$this->pdo = $databaseConnection->getPDO();
$this->fpdo = new \FluentPDO($this->pdo);
}
2014-09-14 16:16:15 +02:00
public function getTableName()
{
2014-09-14 16:16:15 +02:00
return $this->tableName;
}
public function getEntityConverter()
{
return $this->entityConverter;
}
public function save(&$entity)
{
if ($entity->getId())
{
2014-09-15 11:38:24 +02:00
$entity = $this->update($entity);
}
else
{
2014-09-15 11:38:24 +02:00
$entity = $this->create($entity);
}
2014-09-15 11:38:24 +02:00
$this->afterSave($entity);
return $entity;
}
public function findAll()
{
$entities = [];
2014-09-14 16:16:15 +02:00
$query = $this->fpdo->from($this->tableName);
foreach ($query as $arrayEntity)
{
2014-08-30 17:10:45 +02:00
$entity = $this->entityConverter->toEntity($arrayEntity);
2014-09-14 16:16:15 +02:00
$entities[$entity->getId()] = $entity;
}
return $entities;
}
public function findById($entityId)
{
2014-09-21 09:35:43 +02:00
return $this->findOneBy($this->getIdColumn(), $entityId);
}
public function findByIds($entityIds)
{
return $this->findBy($this->getIdColumn(), $entityIds);
}
public function deleteAll()
{
foreach ($this->findAll() as $entity)
{
$this->beforeDelete($entity);
}
2014-09-14 16:16:15 +02:00
$this->fpdo->deleteFrom($this->tableName)->execute();
}
public function deleteById($entityId)
{
2014-09-21 09:35:43 +02:00
return $this->deleteBy($this->getIdColumn(), $entityId);
2014-09-14 16:16:15 +02:00
}
2014-09-15 11:38:24 +02:00
protected function update(\Szurubooru\Entities\Entity $entity)
{
$arrayEntity = $this->entityConverter->toArray($entity);
2014-09-21 09:35:43 +02:00
$this->fpdo->update($this->tableName)->set($arrayEntity)->where($this->getIdColumn(), $entity->getId())->execute();
2014-09-15 11:38:24 +02:00
return $entity;
}
protected function create(\Szurubooru\Entities\Entity $entity)
{
$arrayEntity = $this->entityConverter->toArray($entity);
$this->fpdo->insertInto($this->tableName)->values($arrayEntity)->execute();
$entity->setId(intval($this->pdo->lastInsertId()));
return $entity;
}
2014-09-21 09:35:43 +02:00
protected function getIdColumn()
{
return 'id';
}
2014-09-14 16:16:15 +02:00
protected function hasAnyRecords()
{
return count(iterator_to_array($this->fpdo->from($this->tableName)->limit(1))) > 0;
}
protected function findBy($columnName, $value)
{
$entities = [];
$query = $this->fpdo->from($this->tableName)->where($columnName, $value);
foreach ($query as $arrayEntity)
{
$entity = $this->entityConverter->toEntity($arrayEntity);
$entities[$entity->getId()] = $entity;
}
return $entities;
}
2014-09-14 16:16:15 +02:00
protected function findOneBy($columnName, $value)
{
$arrayEntities = $this->findBy($columnName, $value);
if (!$arrayEntities)
2014-09-15 11:38:24 +02:00
return null;
return array_shift($arrayEntities);
2014-09-14 16:16:15 +02:00
}
protected function deleteBy($columnName, $value)
{
foreach ($this->findBy($columnName, $value) as $entity)
{
$this->beforeDelete($entity);
}
2014-09-14 16:16:15 +02:00
$this->fpdo->deleteFrom($this->tableName)->where($columnName, $value)->execute();
}
2014-09-15 11:38:24 +02:00
protected function afterLoad(\Szurubooru\Entities\Entity $entity)
{
}
protected function afterSave(\Szurubooru\Entities\Entity $entity)
{
}
protected function beforeDelete(\Szurubooru\Entities\Entity $entity)
{
}
}