DesignPatternsPHP/More/Repository/MemoryStorage.php
Dominik Liebler 6ac29916c3 PHP7 More
2016-09-22 18:32:37 +02:00

45 lines
832 B
PHP

<?php
namespace DesignPatterns\More\Repository;
class MemoryStorage
{
/**
* @var array
*/
private $data = [];
/**
* @var int
*/
private $lastId = 0;
public function persist(array $data): int
{
$this->lastId++;
$data['id'] = $this->lastId;
$this->data[$this->lastId] = $data;
return $this->lastId;
}
public function retrieve(int $id): array
{
if (!isset($this->data[$id])) {
throw new \OutOfRangeException(sprintf('No data found for ID %d', $id));
}
return $this->data[$id];
}
public function delete(int $id)
{
if (!isset($this->data[$id])) {
throw new \OutOfRangeException(sprintf('No data found for ID %d', $id));
}
unset($this->data[$id]);
}
}