Skip to content

Instantly share code, notes, and snippets.

@lleitep3
Last active August 29, 2015 13:57
Show Gist options
  • Save lleitep3/9393875 to your computer and use it in GitHub Desktop.
Save lleitep3/9393875 to your computer and use it in GitHub Desktop.
<?php
class User implements IEntity
{
public $id = null;
public $name = '';
public $email = '';
public $pass = '';
public function hasId()
{
return (bool) $this->id;
}
public function getRefName()
{
return 'users_table';
}
public function getData()
{
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
'password' => $this->pass,
];
}
}
class MysqlRepository extends IRepository
{
private $conn = null;
public function __construct(\PDO $connection)
{
$this->conn = $connection;
}
/**
* @return bool
*/
protected function insert(IEnitty $entity)
{
$data = $entity->getData();
unset($data['id']);
$keys = array_keys($data);
$fields = implode('=?,', $keys) . '=?';
$sql = "INSERT INTO {$entity->getRefName} {$fields}";
$stmt = $this->conn->prepare($sql);
return (bool) $stmt->execute(array_values($data));
}
/**
* @return bool
*/
protected function update(IEnitty $entity)
{
$data = $entity->getData();
$id = array_shift($data);
$keys = array_keys($data);
$fields = implode('=?,', $keys) . '=?';
$sql = "UPDATE {$entity->getRefName} SET {$fields} WHERE id = '{$id}'";
$stmt = $this->conn->prepare($sql);
return (bool) $stmt->execute(array_values($data));
}
/**
* @return IEntity
*/
public function find($refname, $arr = [])
{
$where = [];
foreach ($arr as $key => $value) {
$where[] = "{$key} = '{$value}'"
}
$where = implode(',', $where);
$sql = "SELECT * FROM {$refname} WHERE {$where}";
return $this->conn->query($sql)->fetch(PDO::FETCH_OBJECT);
}
/**
* @return array
*/
public function findAll($refname, $arr = [])
{
$where = [];
foreach ($arr as $key => $value) {
$where[] = "{$key} = '{$value}'"
}
$where = implode(',', $where);
$sql = "SELECT * FROM {$refname} WHERE {$where}";
return $this->conn->query($sql)->fetchAll(PDO::FETCH_OBJECT);
}
/**
* @return bool
*/
public function remove(IEnitty $entity);
}
interface IEntity
{
/**
* Aqui temos o metodo que retorna se esse o objeto tem
* id ou não, ou seja, se é um novo objeto ou um que ja existe no banco
*
* @return bool
**/
public function hasId();
public function getRefName();
public function getData();
}
/**
* aqui temos uma interface de um Repositório
* o objetivo dela é só padronizar os métodos que serão utilizados para
* manutenção do objeto que terá que implementar a interface "IEntity"
**/
abstract IRepository
{
/**
* @return bool
*/
protected function insert(IEnitty $entity);
/**
* @return bool
*/
protected function update(IEnitty $entity);
/**
* @return IEntity
*/
public function find($refname, $arr = []);
/**
* @return array
*/
public function findAll($refname, $arr = []);
/**
* @return bool
*/
public function remove(IEnitty $entity);
/**
* @return bool
*/
public function save(IEnitty $entity)
{
if ($entity->hasId()) {
return $this->insert($entity);
}
return $this->update($entity);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment