diff --git a/More/Repository/Domain/Post.php b/More/Repository/Domain/Post.php index 25cc604..ca67c3b 100644 --- a/More/Repository/Domain/Post.php +++ b/More/Repository/Domain/Post.php @@ -5,10 +5,15 @@ namespace DesignPatterns\More\Repository\Domain; class Post { /** - * @var int + * @var PostId */ private $id; + /** + * @var PostStatus + */ + private $status; + /** * @var string */ @@ -19,10 +24,11 @@ class Post */ private $text; - public static function draft(int $id, string $title, string $text): Post + public static function draft(PostId $id, string $title, string $text): Post { return new self( $id, + PostStatus::fromString(PostStatus::STATE_DRAFT), $title, $text ); @@ -31,29 +37,37 @@ class Post public static function fromState(array $state): Post { return new self( - $state['id'], + PostId::fromInt($state['id']), + PostStatus::fromInt($state['statusId']), $state['title'], $state['text'] ); } /** - * @param int $id - * @param string $text + * @param PostId $id + * @param PostStatus $status * @param string $title + * @param string $text */ - private function __construct(int $id, string $title, string $text) + private function __construct(PostId $id, PostStatus $status, string $title, string $text) { $this->id = $id; + $this->status = $status; $this->text = $text; $this->title = $title; } - public function getId(): int + public function getId(): PostId { return $this->id; } + public function getStatus(): PostStatus + { + return $this->status; + } + public function getText(): string { return $this->text; diff --git a/More/Repository/Domain/PostId.php b/More/Repository/Domain/PostId.php new file mode 100644 index 0000000..ceadc78 --- /dev/null +++ b/More/Repository/Domain/PostId.php @@ -0,0 +1,42 @@ +id = $id; + } + + public function toInt(): int + { + return $this->id; + } + + private static function ensureIsValid(int $id) + { + if ($id <= 0) { + throw new \InvalidArgumentException('Invalid PostId given'); + } + } +} diff --git a/More/Repository/Domain/PostStatus.php b/More/Repository/Domain/PostStatus.php new file mode 100644 index 0000000..90ef81b --- /dev/null +++ b/More/Repository/Domain/PostStatus.php @@ -0,0 +1,80 @@ + self::STATE_DRAFT, + self::STATE_PUBLISHED_ID => self::STATE_PUBLISHED, + ]; + + /** + * @var int + */ + private $id; + + /** + * @var string + */ + private $name; + + public static function fromInt(int $statusId) + { + self::ensureIsValidId($statusId); + + return new self($statusId, self::$validStates[$statusId]); + } + + public static function fromString(string $status) + { + self::ensureIsValidName($status); + + return new self(array_search($status, self::$validStates), $status); + } + + private function __construct(int $id, string $name) + { + $this->id = $id; + $this->name = $name; + } + + public function toInt(): int + { + return $this->id; + } + + /** + * there is a reason that I avoid using __toString() as it operates outside of the stack in PHP + * and is therefor not able to operate well with exceptions + */ + public function toString(): string + { + return $this->name; + } + + private static function ensureIsValidId(int $status) + { + if (!in_array($status, array_keys(self::$validStates), true)) { + throw new \InvalidArgumentException('Invalid status id given'); + } + } + + + private static function ensureIsValidName(string $status) + { + if (!in_array($status, self::$validStates, true)) { + throw new \InvalidArgumentException('Invalid status name given'); + } + } +} diff --git a/More/Repository/MemoryStorage.php b/More/Repository/InMemoryPersistence.php similarity index 94% rename from More/Repository/MemoryStorage.php rename to More/Repository/InMemoryPersistence.php index 6463b21..b378321 100644 --- a/More/Repository/MemoryStorage.php +++ b/More/Repository/InMemoryPersistence.php @@ -2,7 +2,7 @@ namespace DesignPatterns\More\Repository; -class MemoryStorage implements Storage +class InMemoryPersistence implements Persistence { /** * @var array diff --git a/More/Repository/Storage.php b/More/Repository/Persistence.php similarity index 90% rename from More/Repository/Storage.php rename to More/Repository/Persistence.php index 1bbd1ed..eed76b9 100644 --- a/More/Repository/Storage.php +++ b/More/Repository/Persistence.php @@ -2,7 +2,7 @@ namespace DesignPatterns\More\Repository; -interface Storage +interface Persistence { public function generateId(): int; diff --git a/More/Repository/PostRepository.php b/More/Repository/PostRepository.php index e559492..d06da37 100644 --- a/More/Repository/PostRepository.php +++ b/More/Repository/PostRepository.php @@ -3,9 +3,10 @@ namespace DesignPatterns\More\Repository; use DesignPatterns\More\Repository\Domain\Post; +use DesignPatterns\More\Repository\Domain\PostId; /** - * This class is situated between Entity layer (class Post) and access object layer (MemoryStorage). + * This class is situated between Entity layer (class Post) and access object layer (Persistence). * * 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 @@ -16,26 +17,26 @@ use DesignPatterns\More\Repository\Domain\Post; class PostRepository { /** - * @var Storage + * @var Persistence */ private $persistence; - public function __construct(Storage $persistence) + public function __construct(Persistence $persistence) { $this->persistence = $persistence; } - public function generateId(): int + public function generateId(): PostId { - return $this->persistence->generateId(); + return PostId::fromInt($this->persistence->generateId()); } - public function findById(int $id): Post + public function findById(PostId $id): Post { try { - $arrayData = $this->persistence->retrieve($id); + $arrayData = $this->persistence->retrieve($id->toInt()); } catch (\OutOfBoundsException $e) { - throw new \OutOfBoundsException(sprintf('Post with id %d does not exist', $id), 0, $e); + throw new \OutOfBoundsException(sprintf('Post with id %d does not exist', $id->toInt()), 0, $e); } return Post::fromState($arrayData); @@ -44,7 +45,8 @@ class PostRepository public function save(Post $post) { $this->persistence->persist([ - 'id' => $post->getId(), + 'id' => $post->getId()->toInt(), + 'statusId' => $post->getStatus()->toInt(), 'text' => $post->getText(), 'title' => $post->getTitle(), ]); diff --git a/More/Repository/README.rst b/More/Repository/README.rst index fb27585..202d74f 100644 --- a/More/Repository/README.rst +++ b/More/Repository/README.rst @@ -33,7 +33,19 @@ You can also find this code on `GitHub`_ Post.php -.. literalinclude:: Post.php +.. literalinclude:: Domain/Post.php + :language: php + :linenos: + +PostId.php + +.. literalinclude:: Domain/PostId.php + :language: php + :linenos: + +PostStatus.php + +.. literalinclude:: Domain/PostStatus.php :language: php :linenos: @@ -43,18 +55,24 @@ PostRepository.php :language: php :linenos: -MemoryStorage.php +Persistence.php -.. literalinclude:: MemoryStorage.php +.. literalinclude:: InMemoryPersistence.php + :language: php + :linenos: + +InMemoryPersistence.php + +.. literalinclude:: InMemoryPersistence.php :language: php :linenos: Test ---- -Tests/RepositoryTest.php +Tests/PostRepositoryTest.php -.. literalinclude:: Tests/RepositoryTest.php +.. literalinclude:: Tests/PostRepositoryTest.php :language: php :linenos: diff --git a/More/Repository/Tests/PostRepositoryTest.php b/More/Repository/Tests/PostRepositoryTest.php index b1b85c3..fec6ef1 100644 --- a/More/Repository/Tests/PostRepositoryTest.php +++ b/More/Repository/Tests/PostRepositoryTest.php @@ -2,7 +2,9 @@ namespace DesignPatterns\More\Repository\Tests; -use DesignPatterns\More\Repository\MemoryStorage; +use DesignPatterns\More\Repository\Domain\PostId; +use DesignPatterns\More\Repository\Domain\PostStatus; +use DesignPatterns\More\Repository\InMemoryPersistence; use DesignPatterns\More\Repository\Domain\Post; use DesignPatterns\More\Repository\PostRepository; use PHPUnit\Framework\TestCase; @@ -16,12 +18,12 @@ class PostRepositoryTest extends TestCase protected function setUp() { - $this->repository = new PostRepository(new MemoryStorage()); + $this->repository = new PostRepository(new InMemoryPersistence()); } public function testCanGenerateId() { - $this->assertEquals(1, $this->repository->generateId()); + $this->assertEquals(1, $this->repository->generateId()->toInt()); } /** @@ -30,7 +32,7 @@ class PostRepositoryTest extends TestCase */ public function testThrowsExceptionWhenTryingToFindPostWhichDoesNotExist() { - $this->repository->findById(42); + $this->repository->findById(PostId::fromInt(42)); } public function testCanPersistPostDraft() @@ -39,6 +41,9 @@ class PostRepositoryTest extends TestCase $post = Post::draft($postId, 'Repository Pattern', 'Design Patterns PHP'); $this->repository->save($post); + $this->repository->findById($postId); + $this->assertEquals($postId, $this->repository->findById($postId)->getId()); + $this->assertEquals(PostStatus::STATE_DRAFT, $post->getStatus()->toString()); } } diff --git a/More/Repository/uml/Repository.uml b/More/Repository/uml/Repository.uml index 84dd0e4..6b02568 100644 --- a/More/Repository/uml/Repository.uml +++ b/More/Repository/uml/Repository.uml @@ -3,20 +3,24 @@ PHP \DesignPatterns\More\Repository\PostRepository - \DesignPatterns\More\Repository\Storage - \DesignPatterns\More\Repository\MemoryStorage - \DesignPatterns\More\Repository\Post - \DesignPatterns\More\Repository\PostRepository + \DesignPatterns\More\Repository\InMemoryPersistence + \DesignPatterns\More\Repository\Domain\PostStatus + \DesignPatterns\More\Repository\Domain\PostId + \DesignPatterns\More\Repository\Domain\Post + \DesignPatterns\More\Repository\Persistence + \DesignPatterns\More\Repository\PostRepository - - - + + + - - + + + \DesignPatterns\More\Repository\PostRepository + Fields Constants diff --git a/More/Repository/uml/uml.png b/More/Repository/uml/uml.png index d22ede8..ce0c5e6 100644 Binary files a/More/Repository/uml/uml.png and b/More/Repository/uml/uml.png differ diff --git a/More/Repository/uml/uml.svg b/More/Repository/uml/uml.svg index 8b9987c..97e0a5c 100644 --- a/More/Repository/uml/uml.svg +++ b/More/Repository/uml/uml.svg @@ -1,687 +1,113 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - persist(data) - - - - - - - - - retrieve(id) - - - - - - - - - delete(id) - - - - - - - - - - - - - Storage - - - Storage - - - - - - - - - - - - - - - - - - - data - - - - - - - - - - lastId - - - - - - - - - - - - __construct() - - - - - - - - - - - - persist(data) - - - - - - - - - retrieve(id) - - - - - - - - - delete(id) - - - - - - - - - - - - - MemoryStorage - - - MemoryStorage - - - - - - - - - - - - - - - - - - - id - - - - - - - - - - title - - - - - - - - - - text - - - - - - - - - - author - - - - - - - - - - created - - - - - - - - - - - - setId(id) - - - - - - - - - getId() - - - - - - - - - setAuthor(author) - - - - - - - - - getAuthor() - - - - - - - - - setCreated(created) - - - - - - - - - getCreated() - - - - - - - - - setText(text) - - - - - - - - - getText() - - - - - - - - - setTitle(title) - - - - - - - - - getTitle() - - - - - - - - - - - - - Post - - - Post - - - - - - - - - - - - - - - - - - - persistence - - - - - - - - - - - - __construct(persistence) - - - - - - - - - - - - getById(id) - - - - - - - - - save(post) - - - - - - - - - delete(post) - - - - - - - - - - - - - PostRepository - - - PostRepository - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/locale/ca/LC_MESSAGES/More/Repository/README.po b/locale/ca/LC_MESSAGES/More/Repository/README.po index 5a37043..319ace4 100644 --- a/locale/ca/LC_MESSAGES/More/Repository/README.po +++ b/locale/ca/LC_MESSAGES/More/Repository/README.po @@ -64,11 +64,11 @@ msgid "PostRepository.php" msgstr "" #: ../../More/Repository/README.rst:46 -msgid "Storage.php" +msgid "Persistence.php" msgstr "" #: ../../More/Repository/README.rst:52 -msgid "MemoryStorage.php" +msgid "InMemoryPersistence.php" msgstr "" #: ../../More/Repository/README.rst:59 diff --git a/locale/de/LC_MESSAGES/More/Repository/README.po b/locale/de/LC_MESSAGES/More/Repository/README.po index 152a3e7..f4e077a 100644 --- a/locale/de/LC_MESSAGES/More/Repository/README.po +++ b/locale/de/LC_MESSAGES/More/Repository/README.po @@ -75,12 +75,12 @@ msgid "PostRepository.php" msgstr "PostRepository.php" #: ../../More/Repository/README.rst:46 -msgid "Storage.php" -msgstr "Storage.php" +msgid "Persistence.php" +msgstr "Persistence.php" #: ../../More/Repository/README.rst:52 -msgid "MemoryStorage.php" -msgstr "MemoryStorage.php" +msgid "InMemoryPersistence.php" +msgstr "InMemoryPersistence.php" #: ../../More/Repository/README.rst:59 msgid "Test" diff --git a/locale/es/LC_MESSAGES/More/Repository/README.po b/locale/es/LC_MESSAGES/More/Repository/README.po index 569d52b..1ca95fe 100644 --- a/locale/es/LC_MESSAGES/More/Repository/README.po +++ b/locale/es/LC_MESSAGES/More/Repository/README.po @@ -64,11 +64,11 @@ msgid "PostRepository.php" msgstr "" #: ../../More/Repository/README.rst:46 -msgid "Storage.php" +msgid "Persistence.php" msgstr "" #: ../../More/Repository/README.rst:52 -msgid "MemoryStorage.php" +msgid "InMemoryPersistence.php" msgstr "" #: ../../More/Repository/README.rst:59 diff --git a/locale/pl/LC_MESSAGES/More/Repository/README.po b/locale/pl/LC_MESSAGES/More/Repository/README.po index 577c9dd..b1faa49 100644 --- a/locale/pl/LC_MESSAGES/More/Repository/README.po +++ b/locale/pl/LC_MESSAGES/More/Repository/README.po @@ -72,12 +72,12 @@ msgid "PostRepository.php" msgstr "PostRepository.php" #: ../../More/Repository/README.rst:46 -msgid "Storage.php" -msgstr "Storage.php" +msgid "Persistence.php" +msgstr "Persistence.php" #: ../../More/Repository/README.rst:52 -msgid "MemoryStorage.php" -msgstr "MemoryStorage.php" +msgid "InMemoryPersistence.php" +msgstr "InMemoryPersistence.php" #: ../../More/Repository/README.rst:59 msgid "Test" diff --git a/locale/pt_BR/LC_MESSAGES/More/Repository/README.po b/locale/pt_BR/LC_MESSAGES/More/Repository/README.po index 0fe32e4..f9a9d36 100644 --- a/locale/pt_BR/LC_MESSAGES/More/Repository/README.po +++ b/locale/pt_BR/LC_MESSAGES/More/Repository/README.po @@ -72,11 +72,11 @@ msgid "PostRepository.php" msgstr "" #: ../../More/Repository/README.rst:46 -msgid "Storage.php" +msgid "Persistence.php" msgstr "" #: ../../More/Repository/README.rst:52 -msgid "MemoryStorage.php" +msgid "InMemoryPersistence.php" msgstr "" #: ../../More/Repository/README.rst:59 diff --git a/locale/ru/LC_MESSAGES/More/Repository/README.po b/locale/ru/LC_MESSAGES/More/Repository/README.po index 0a09340..29c5d4e 100644 --- a/locale/ru/LC_MESSAGES/More/Repository/README.po +++ b/locale/ru/LC_MESSAGES/More/Repository/README.po @@ -73,12 +73,12 @@ msgid "PostRepository.php" msgstr "PostRepository.php" #: ../../More/Repository/README.rst:46 -msgid "Storage.php" -msgstr "Storage.php" +msgid "Persistence.php" +msgstr "Persistence.php" #: ../../More/Repository/README.rst:52 -msgid "MemoryStorage.php" -msgstr "MemoryStorage.php" +msgid "InMemoryPersistence.php" +msgstr "InMemoryPersistence.php" #: ../../More/Repository/README.rst:59 msgid "Test" diff --git a/locale/zh_CN/LC_MESSAGES/More/Repository/README.po b/locale/zh_CN/LC_MESSAGES/More/Repository/README.po index 5a37043..319ace4 100644 --- a/locale/zh_CN/LC_MESSAGES/More/Repository/README.po +++ b/locale/zh_CN/LC_MESSAGES/More/Repository/README.po @@ -64,11 +64,11 @@ msgid "PostRepository.php" msgstr "" #: ../../More/Repository/README.rst:46 -msgid "Storage.php" +msgid "Persistence.php" msgstr "" #: ../../More/Repository/README.rst:52 -msgid "MemoryStorage.php" +msgid "InMemoryPersistence.php" msgstr "" #: ../../More/Repository/README.rst:59