mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-28 10:40:17 +02:00
PHP7 More
This commit is contained in:
@@ -2,47 +2,43 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\More\Repository;
|
namespace DesignPatterns\More\Repository;
|
||||||
|
|
||||||
class MemoryStorage implements Storage
|
class MemoryStorage
|
||||||
{
|
{
|
||||||
private $data;
|
/**
|
||||||
private $lastId;
|
* @var array
|
||||||
|
*/
|
||||||
public function __construct()
|
private $data = [];
|
||||||
{
|
|
||||||
$this->data = array();
|
|
||||||
$this->lastId = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* @var int
|
||||||
*/
|
*/
|
||||||
public function persist($data)
|
private $lastId = 0;
|
||||||
|
|
||||||
|
public function persist(array $data): int
|
||||||
{
|
{
|
||||||
$this->data[++$this->lastId] = $data;
|
$this->lastId++;
|
||||||
|
|
||||||
|
$data['id'] = $this->lastId;
|
||||||
|
$this->data[$this->lastId] = $data;
|
||||||
|
|
||||||
return $this->lastId;
|
return $this->lastId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function retrieve(int $id): array
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function retrieve($id)
|
|
||||||
{
|
|
||||||
return isset($this->data[$id]) ? $this->data[$id] : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function delete($id)
|
|
||||||
{
|
{
|
||||||
if (!isset($this->data[$id])) {
|
if (!isset($this->data[$id])) {
|
||||||
return false;
|
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->data[$id] = null;
|
|
||||||
unset($this->data[$id]);
|
unset($this->data[$id]);
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -5,7 +5,7 @@ namespace DesignPatterns\More\Repository;
|
|||||||
class Post
|
class Post
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var int
|
* @var int|null
|
||||||
*/
|
*/
|
||||||
private $id;
|
private $id;
|
||||||
|
|
||||||
@@ -19,92 +19,43 @@ class Post
|
|||||||
*/
|
*/
|
||||||
private $text;
|
private $text;
|
||||||
|
|
||||||
/**
|
public static function fromState(array $state): Post
|
||||||
* @var string
|
{
|
||||||
*/
|
return new self(
|
||||||
private $author;
|
$state['id'],
|
||||||
|
$state['title'],
|
||||||
|
$state['text']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \DateTime
|
* @param int|null $id
|
||||||
|
* @param string $text
|
||||||
|
* @param string $title
|
||||||
*/
|
*/
|
||||||
private $created;
|
public function __construct($id, string $title, string $text)
|
||||||
|
{
|
||||||
|
$this->id = $id;
|
||||||
|
$this->text = $text;
|
||||||
|
$this->title = $title;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
public function setId(int $id)
|
||||||
* @param int $id
|
|
||||||
*/
|
|
||||||
public function setId($id)
|
|
||||||
{
|
{
|
||||||
$this->id = $id;
|
$this->id = $id;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function getId(): int
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
public function getId()
|
|
||||||
{
|
{
|
||||||
return $this->id;
|
return $this->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function getText(): string
|
||||||
* @param string $author
|
|
||||||
*/
|
|
||||||
public function setAuthor($author)
|
|
||||||
{
|
|
||||||
$this->author = $author;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getAuthor()
|
|
||||||
{
|
|
||||||
return $this->author;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param \DateTime $created
|
|
||||||
*/
|
|
||||||
public function setCreated($created)
|
|
||||||
{
|
|
||||||
$this->created = $created;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return \DateTime
|
|
||||||
*/
|
|
||||||
public function getCreated()
|
|
||||||
{
|
|
||||||
return $this->created;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $text
|
|
||||||
*/
|
|
||||||
public function setText($text)
|
|
||||||
{
|
|
||||||
$this->text = $text;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getText()
|
|
||||||
{
|
{
|
||||||
return $this->text;
|
return $this->text;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function getTitle(): string
|
||||||
* @param string $title
|
|
||||||
*/
|
|
||||||
public function setTitle($title)
|
|
||||||
{
|
|
||||||
$this->title = $title;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getTitle()
|
|
||||||
{
|
{
|
||||||
return $this->title;
|
return $this->title;
|
||||||
}
|
}
|
||||||
|
@@ -3,7 +3,7 @@
|
|||||||
namespace DesignPatterns\More\Repository;
|
namespace DesignPatterns\More\Repository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class is between Entity layer (class Post) and access object layer (interface Storage).
|
* This class is situated between Entity layer (class Post) and access object layer (MemoryStorage).
|
||||||
*
|
*
|
||||||
* Repository encapsulates the set of objects persisted in a data store and the operations performed over them
|
* Repository encapsulates the set of objects persisted in a data store and the operations performed over them
|
||||||
* providing a more object-oriented view of the persistence layer
|
* providing a more object-oriented view of the persistence layer
|
||||||
@@ -13,67 +13,34 @@ namespace DesignPatterns\More\Repository;
|
|||||||
*/
|
*/
|
||||||
class PostRepository
|
class PostRepository
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var MemoryStorage
|
||||||
|
*/
|
||||||
private $persistence;
|
private $persistence;
|
||||||
|
|
||||||
public function __construct(Storage $persistence)
|
public function __construct(MemoryStorage $persistence)
|
||||||
{
|
{
|
||||||
$this->persistence = $persistence;
|
$this->persistence = $persistence;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function findById(int $id): Post
|
||||||
* Returns Post object by specified id.
|
|
||||||
*
|
|
||||||
* @param int $id
|
|
||||||
*
|
|
||||||
* @return Post|null
|
|
||||||
*/
|
|
||||||
public function getById($id)
|
|
||||||
{
|
{
|
||||||
$arrayData = $this->persistence->retrieve($id);
|
$arrayData = $this->persistence->retrieve($id);
|
||||||
|
|
||||||
if (is_null($arrayData)) {
|
if (is_null($arrayData)) {
|
||||||
return;
|
throw new \InvalidArgumentException(sprintf('Post with ID %d does not exist'));
|
||||||
}
|
}
|
||||||
|
|
||||||
$post = new Post();
|
return Post::fromState($arrayData);
|
||||||
$post->setId($arrayData['id']);
|
|
||||||
$post->setAuthor($arrayData['author']);
|
|
||||||
$post->setCreated($arrayData['created']);
|
|
||||||
$post->setText($arrayData['text']);
|
|
||||||
$post->setTitle($arrayData['title']);
|
|
||||||
|
|
||||||
return $post;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Save post object and populate it with id.
|
|
||||||
*
|
|
||||||
* @param Post $post
|
|
||||||
*
|
|
||||||
* @return Post
|
|
||||||
*/
|
|
||||||
public function save(Post $post)
|
public function save(Post $post)
|
||||||
{
|
{
|
||||||
$id = $this->persistence->persist(array(
|
$id = $this->persistence->persist([
|
||||||
'author' => $post->getAuthor(),
|
|
||||||
'created' => $post->getCreated(),
|
|
||||||
'text' => $post->getText(),
|
'text' => $post->getText(),
|
||||||
'title' => $post->getTitle(),
|
'title' => $post->getTitle(),
|
||||||
));
|
]);
|
||||||
|
|
||||||
$post->setId($id);
|
$post->setId($id);
|
||||||
|
|
||||||
return $post;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Deletes specified Post object.
|
|
||||||
*
|
|
||||||
* @param Post $post
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function delete(Post $post)
|
|
||||||
{
|
|
||||||
return $this->persistence->delete($post->getId());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -43,12 +43,6 @@ PostRepository.php
|
|||||||
:language: php
|
:language: php
|
||||||
:linenos:
|
:linenos:
|
||||||
|
|
||||||
Storage.php
|
|
||||||
|
|
||||||
.. literalinclude:: Storage.php
|
|
||||||
:language: php
|
|
||||||
:linenos:
|
|
||||||
|
|
||||||
MemoryStorage.php
|
MemoryStorage.php
|
||||||
|
|
||||||
.. literalinclude:: MemoryStorage.php
|
.. literalinclude:: MemoryStorage.php
|
||||||
@@ -58,4 +52,10 @@ MemoryStorage.php
|
|||||||
Test
|
Test
|
||||||
----
|
----
|
||||||
|
|
||||||
|
Tests/RepositoryTest.php
|
||||||
|
|
||||||
|
.. literalinclude:: Tests/RepositoryTest.php
|
||||||
|
:language: php
|
||||||
|
:linenos:
|
||||||
|
|
||||||
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/More/Repository
|
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/More/Repository
|
||||||
|
@@ -1,42 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DesignPatterns\More\Repository;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface Storage.
|
|
||||||
*
|
|
||||||
* This interface describes methods for accessing storage.
|
|
||||||
* Concrete realization could be whatever we want - in memory, relational database, NoSQL database and etc
|
|
||||||
*/
|
|
||||||
interface Storage
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Method to persist data
|
|
||||||
* Returns new id for just persisted data.
|
|
||||||
*
|
|
||||||
* @param array() $data
|
|
||||||
*
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
public function persist($data);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns data by specified id.
|
|
||||||
* If there is no such data null is returned.
|
|
||||||
*
|
|
||||||
* @param int $id
|
|
||||||
*
|
|
||||||
* @return array|null
|
|
||||||
*/
|
|
||||||
public function retrieve($id);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Delete data specified by id
|
|
||||||
* If there is no such data - false returns, if data has been successfully deleted - true returns.
|
|
||||||
*
|
|
||||||
* @param int $id
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function delete($id);
|
|
||||||
}
|
|
21
More/Repository/Tests/RepositoryTest.php
Normal file
21
More/Repository/Tests/RepositoryTest.php
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\More\Repository\Tests;
|
||||||
|
|
||||||
|
use DesignPatterns\More\Repository\MemoryStorage;
|
||||||
|
use DesignPatterns\More\Repository\Post;
|
||||||
|
use DesignPatterns\More\Repository\PostRepository;
|
||||||
|
|
||||||
|
class Repository extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
public function testCanPersistAndFindPost()
|
||||||
|
{
|
||||||
|
$repository = new PostRepository(new MemoryStorage());
|
||||||
|
$post = new Post(null, 'Repository Pattern', 'Design Patterns PHP');
|
||||||
|
|
||||||
|
$repository->save($post);
|
||||||
|
|
||||||
|
$this->assertEquals(1, $post->getId());
|
||||||
|
$this->assertEquals($post->getId(), $repository->findById(1)->getId());
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user