2014-03-12 13:11:49 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace DesignPatterns\Repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class MemoryStorage
|
|
|
|
* @package DesignPatterns\Repository
|
|
|
|
*/
|
2014-04-22 15:15:52 +03:00
|
|
|
class MemoryStorage implements Storage
|
2014-03-12 13:11:49 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2014-11-05 10:24:19 +01:00
|
|
|
if (!isset($this->data[$id])) {
|
2014-03-12 13:11:49 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->data[$id] = null;
|
|
|
|
unset($this->data[$id]);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|