From 6ac29916c34f8a206f3673fb866ea4a54aa35655 Mon Sep 17 00:00:00 2001 From: Dominik Liebler Date: Thu, 22 Sep 2016 18:32:37 +0200 Subject: [PATCH] PHP7 More --- More/Repository/MemoryStorage.php | 52 ++++++------- More/Repository/Post.php | 93 ++++++------------------ More/Repository/PostRepository.php | 55 +++----------- More/Repository/README.rst | 12 +-- More/Repository/Storage.php | 42 ----------- More/Repository/Tests/RepositoryTest.php | 21 ++++++ 6 files changed, 84 insertions(+), 191 deletions(-) delete mode 100644 More/Repository/Storage.php create mode 100644 More/Repository/Tests/RepositoryTest.php diff --git a/More/Repository/MemoryStorage.php b/More/Repository/MemoryStorage.php index d970773..f0fd69a 100644 --- a/More/Repository/MemoryStorage.php +++ b/More/Repository/MemoryStorage.php @@ -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]); } } diff --git a/More/Repository/Post.php b/More/Repository/Post.php index 6fdcfc0..44aaefa 100644 --- a/More/Repository/Post.php +++ b/More/Repository/Post.php @@ -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; } diff --git a/More/Repository/PostRepository.php b/More/Repository/PostRepository.php index 2f92dd3..991a398 100644 --- a/More/Repository/PostRepository.php +++ b/More/Repository/PostRepository.php @@ -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()); } } diff --git a/More/Repository/README.rst b/More/Repository/README.rst index 48ac018..d8e1c72 100644 --- a/More/Repository/README.rst +++ b/More/Repository/README.rst @@ -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 diff --git a/More/Repository/Storage.php b/More/Repository/Storage.php deleted file mode 100644 index a730f09..0000000 --- a/More/Repository/Storage.php +++ /dev/null @@ -1,42 +0,0 @@ -save($post); + + $this->assertEquals(1, $post->getId()); + $this->assertEquals($post->getId(), $repository->findById(1)->getId()); + } +}