Last active
October 28, 2019 15:52
-
-
Save afranioce/7d2f7cecfca7588fd4eaa68c530fd945 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface ModelInterface | |
{ | |
public function fields(): array; | |
} | |
class Item implements ModelInterface | |
{ | |
private $value; | |
private $quantity; | |
public function __construct($value, $quantity) | |
{ | |
if ($value === null) | |
{ | |
throw new Exception('Valor não pode ser nulo'); | |
} | |
if ($quantity === null) | |
{ | |
throw new Exception('Valor não pode ser nulo'); | |
} | |
$this->value = $value; | |
$this->quantity = $quantity; | |
} | |
public function fields(): array | |
{ | |
return [ | |
'value' => 'valor', | |
'quantity' => 'quantidade', | |
]; | |
} | |
public function getValue(): string { | |
$this->value; | |
} | |
public function getQuantity(): string { | |
$this->quantity; | |
} | |
} | |
interface EntityManagerInterface | |
{ | |
public function getRepository(string $modelName); | |
} | |
class DbManager implements EntityManagerInterface | |
{ | |
public function getRepository(string $modelName): EntityRepository { | |
return new EntityRepository($modelName); | |
} | |
} | |
class EntityRepository | |
{ | |
private $reflectionClass; | |
public function __construct(string $modelName) | |
{ | |
if (!class_exists($modelName)) { | |
throw new Exception("class not exists"); | |
} | |
$reflectionClass = new ReflectionClass($modelName); | |
if (!$reflectionClass->implementsInterface(ModelInterface::class)) { | |
throw new Exception("class not instance of ModelInterface"); | |
} | |
$this->reflectionClass = $reflectionClass; | |
} | |
public function fromRows(array $rows) { | |
$instance = $this->getNewInstance(); | |
foreach($rows as $row) { | |
$this->setValues($instance, $row); | |
} | |
return $instance; | |
} | |
public function fromRow(array $row) { | |
$instance = $this->getNewInstance(); | |
$this->setValues($instance, $row); | |
return $instance; | |
} | |
private function getNewInstance(): ModelInterface { | |
return $this->reflectionClass->newInstanceWithoutConstructor(); | |
} | |
private function setValues($instance, $row) { | |
foreach ($instance->fields() as $propertyName => $columnName) { | |
if (!isset($row[$columnName])) { | |
continue; | |
} | |
$this->setValue($instance, $propertyName, $row[$columnName]); | |
} | |
} | |
private function setValue($object, string $propertyName, $value) { | |
if (!$this->reflectionClass->hasProperty($propertyName)) { | |
return; | |
} | |
$property = $this->reflectionClass->getProperty($propertyName); | |
$property->setAccessible(true); | |
$property->setValue($object, $value); | |
} | |
} | |
class ItemRepository { | |
private $repository; | |
public function __construct(EntityManagerInterface $entityManager) { | |
$this->repository = $entityManager->getRepository(Item::class); | |
} | |
public function getOne(): Item { | |
$row = ['valor' => '123', 'quantidade' => 'teste de valor', 'not_exists' => 'werer']; | |
return $this->repository->fromRow($row); | |
} | |
public function getAll() { | |
$rows = [['valor' => '123', 'quantidade' => null, 'not_exists' => 'werer']]; | |
return $this->repository->fromRows($rows); | |
} | |
} | |
$db = new DbManager(); | |
$repository = new ItemRepository($db); | |
var_dump($repository->getOne()); | |
var_dump($repository->getAll()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment