mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-30 19:50:12 +02:00
removed/moved newly added patterns
This commit is contained in:
54
More/Repository/MemoryStorage.php
Normal file
54
More/Repository/MemoryStorage.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Repository;
|
||||
|
||||
/**
|
||||
* Class MemoryStorage
|
||||
* @package DesignPatterns\Repository
|
||||
*/
|
||||
class MemoryStorage implements Storage
|
||||
{
|
||||
|
||||
private $data;
|
||||
private $lastId;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->data = array();
|
||||
$this->lastId = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function persist($data)
|
||||
{
|
||||
$this->data[++$this->lastId] = $data;
|
||||
return $this->lastId;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function retrieve($id)
|
||||
{
|
||||
return isset($this->data[$id]) ? $this->data[$id] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
if(!isset($this->data[$id])){
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->data[$id] = null;
|
||||
unset($this->data[$id]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user