mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-01 12:40:11 +02:00
introduced PostId and PostStatus and changed naming from Storage to Persistence
This commit is contained in:
46
More/Repository/InMemoryPersistence.php
Normal file
46
More/Repository/InMemoryPersistence.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\More\Repository;
|
||||
|
||||
class InMemoryPersistence implements Persistence
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $data = [];
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $lastId = 0;
|
||||
|
||||
public function generateId(): int
|
||||
{
|
||||
$this->lastId++;
|
||||
|
||||
return $this->lastId;
|
||||
}
|
||||
|
||||
public function persist(array $data)
|
||||
{
|
||||
$this->data[$this->lastId] = $data;
|
||||
}
|
||||
|
||||
public function retrieve(int $id): array
|
||||
{
|
||||
if (!isset($this->data[$id])) {
|
||||
throw new \OutOfBoundsException(sprintf('No data found for ID %d', $id));
|
||||
}
|
||||
|
||||
return $this->data[$id];
|
||||
}
|
||||
|
||||
public function delete(int $id)
|
||||
{
|
||||
if (!isset($this->data[$id])) {
|
||||
throw new \OutOfBoundsException(sprintf('No data found for ID %d', $id));
|
||||
}
|
||||
|
||||
unset($this->data[$id]);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user