mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-26 01:31:20 +02:00
PHP7 More
This commit is contained in:
@@ -2,47 +2,43 @@
|
||||
|
||||
namespace DesignPatterns\More\Repository;
|
||||
|
||||
class MemoryStorage implements Storage
|
||||
class MemoryStorage
|
||||
{
|
||||
private $data;
|
||||
private $lastId;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->data = array();
|
||||
$this->lastId = 0;
|
||||
}
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $data = [];
|
||||
|
||||
/**
|
||||
* {@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;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function retrieve($id)
|
||||
{
|
||||
return isset($this->data[$id]) ? $this->data[$id] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function delete($id)
|
||||
public function retrieve(int $id): array
|
||||
{
|
||||
if (!isset($this->data[$id])) {
|
||||
return false;
|
||||
throw new \OutOfRangeException(sprintf('No data found for ID %d', $id));
|
||||
}
|
||||
|
||||
$this->data[$id] = null;
|
||||
unset($this->data[$id]);
|
||||
return $this->data[$id];
|
||||
}
|
||||
|
||||
return true;
|
||||
public function delete(int $id)
|
||||
{
|
||||
if (!isset($this->data[$id])) {
|
||||
throw new \OutOfRangeException(sprintf('No data found for ID %d', $id));
|
||||
}
|
||||
|
||||
unset($this->data[$id]);
|
||||
}
|
||||
}
|
||||
|
@@ -5,7 +5,7 @@ namespace DesignPatterns\More\Repository;
|
||||
class Post
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
* @var int|null
|
||||
*/
|
||||
private $id;
|
||||
|
||||
@@ -19,92 +19,43 @@ class Post
|
||||
*/
|
||||
private $text;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $author;
|
||||
public static function fromState(array $state): Post
|
||||
{
|
||||
return new self(
|
||||
$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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
*/
|
||||
public function setId($id)
|
||||
public function setId(int $id)
|
||||
{
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId()
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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()
|
||||
public function getText(): string
|
||||
{
|
||||
return $this->text;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $title
|
||||
*/
|
||||
public function setTitle($title)
|
||||
{
|
||||
$this->title = $title;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTitle()
|
||||
public function getTitle(): string
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
|
@@ -3,7 +3,7 @@
|
||||
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
|
||||
* providing a more object-oriented view of the persistence layer
|
||||
@@ -13,67 +13,34 @@ namespace DesignPatterns\More\Repository;
|
||||
*/
|
||||
class PostRepository
|
||||
{
|
||||
/**
|
||||
* @var MemoryStorage
|
||||
*/
|
||||
private $persistence;
|
||||
|
||||
public function __construct(Storage $persistence)
|
||||
public function __construct(MemoryStorage $persistence)
|
||||
{
|
||||
$this->persistence = $persistence;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Post object by specified id.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return Post|null
|
||||
*/
|
||||
public function getById($id)
|
||||
public function findById(int $id): Post
|
||||
{
|
||||
$arrayData = $this->persistence->retrieve($id);
|
||||
|
||||
if (is_null($arrayData)) {
|
||||
return;
|
||||
throw new \InvalidArgumentException(sprintf('Post with ID %d does not exist'));
|
||||
}
|
||||
|
||||
$post = new Post();
|
||||
$post->setId($arrayData['id']);
|
||||
$post->setAuthor($arrayData['author']);
|
||||
$post->setCreated($arrayData['created']);
|
||||
$post->setText($arrayData['text']);
|
||||
$post->setTitle($arrayData['title']);
|
||||
|
||||
return $post;
|
||||
return Post::fromState($arrayData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save post object and populate it with id.
|
||||
*
|
||||
* @param Post $post
|
||||
*
|
||||
* @return Post
|
||||
*/
|
||||
public function save(Post $post)
|
||||
{
|
||||
$id = $this->persistence->persist(array(
|
||||
'author' => $post->getAuthor(),
|
||||
'created' => $post->getCreated(),
|
||||
$id = $this->persistence->persist([
|
||||
'text' => $post->getText(),
|
||||
'title' => $post->getTitle(),
|
||||
));
|
||||
]);
|
||||
|
||||
$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
|
||||
:linenos:
|
||||
|
||||
Storage.php
|
||||
|
||||
.. literalinclude:: Storage.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
MemoryStorage.php
|
||||
|
||||
.. literalinclude:: MemoryStorage.php
|
||||
@@ -58,4 +52,10 @@ MemoryStorage.php
|
||||
Test
|
||||
----
|
||||
|
||||
Tests/RepositoryTest.php
|
||||
|
||||
.. literalinclude:: Tests/RepositoryTest.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
.. _`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