Files
DesignPatternsPHP/More/Repository/InMemoryPersistence.php
Mario Simão 56970cc221 style: Adopt PSR12
Dev dependency flyeralarm/php-code-validator has been removed.

Closes #444
2021-10-01 10:26:04 -03:00

44 lines
865 B
PHP

<?php
declare(strict_types=1);
namespace DesignPatterns\More\Repository;
use OutOfBoundsException;
class InMemoryPersistence implements Persistence
{
private array $data = [];
private int $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]);
}
}