mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-02-24 09:42:24 +01:00
45 lines
832 B
PHP
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]);
|
|
}
|
|
}
|