33namespace DesignPatterns \More \Repository ;
44
55use DesignPatterns \More \Repository \Domain \Post ;
6+ use DesignPatterns \More \Repository \Domain \PostId ;
67
78/**
8- * This class is situated between Entity layer (class Post) and access object layer (MemoryStorage ).
9+ * This class is situated between Entity layer (class Post) and access object layer (Persistence ).
910 *
1011 * Repository encapsulates the set of objects persisted in a data store and the operations performed over them
1112 * providing a more object-oriented view of the persistence layer
1617class PostRepository
1718{
1819 /**
19- * @var Storage
20+ * @var Persistence
2021 */
2122 private $ persistence ;
2223
23- public function __construct (Storage $ persistence )
24+ public function __construct (Persistence $ persistence )
2425 {
2526 $ this ->persistence = $ persistence ;
2627 }
2728
28- public function generateId (): int
29+ public function generateId (): PostId
2930 {
30- return $ this ->persistence ->generateId ();
31+ return PostId:: fromInt ( $ this ->persistence ->generateId () );
3132 }
3233
33- public function findById (int $ id ): Post
34+ public function findById (PostId $ id ): Post
3435 {
3536 try {
36- $ arrayData = $ this ->persistence ->retrieve ($ id );
37+ $ arrayData = $ this ->persistence ->retrieve ($ id-> toInt () );
3738 } catch (\OutOfBoundsException $ e ) {
38- throw new \OutOfBoundsException (sprintf ('Post with id %d does not exist ' , $ id ), 0 , $ e );
39+ throw new \OutOfBoundsException (sprintf ('Post with id %d does not exist ' , $ id-> toInt () ), 0 , $ e );
3940 }
4041
4142 return Post::fromState ($ arrayData );
@@ -44,7 +45,8 @@ public function findById(int $id): Post
4445 public function save (Post $ post )
4546 {
4647 $ this ->persistence ->persist ([
47- 'id ' => $ post ->getId (),
48+ 'id ' => $ post ->getId ()->toInt (),
49+ 'statusId ' => $ post ->getStatus ()->toInt (),
4850 'text ' => $ post ->getText (),
4951 'title ' => $ post ->getTitle (),
5052 ]);
0 commit comments