sathyashrayan

PostRepository.php

Jul 2nd, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.37 KB | None | 0 0
  1. <?php
  2. namespace Blog\Model;
  3.  
  4. class PostRepository implements PostRepositoryInterface
  5. {
  6.     /**
  7.      * {@inheritDoc}
  8.      */
  9.    
  10.     private $data = [
  11.             1 => [
  12.                     'id'    => 1,
  13.                     'title' => 'Hello World #1',
  14.                     'text'  => 'This is our first blog post!',
  15.             ],
  16.             2 => [
  17.                     'id'     => 2,
  18.                     'title' => 'Hello World #2',
  19.                     'text'  => 'This is our second blog post!',
  20.             ],
  21.             3 => [
  22.                     'id'     => 3,
  23.                     'title' => 'Hello World #3',
  24.                     'text'  => 'This is our third blog post!',
  25.             ],
  26.             4 => [
  27.                     'id'     => 4,
  28.                     'title' => 'Hello World #4',
  29.                     'text'  => 'This is our fourth blog post!',
  30.             ],
  31.             5 => [
  32.                     'id'     => 5,
  33.                     'title' => 'Hello World #5',
  34.                     'text'  => 'This is our fifth blog post!',
  35.             ],
  36.     ];
  37.    
  38.     public function findAllPosts()
  39.     {
  40.         return array_map(function ($post) {
  41.             return new Post(
  42.                 $post['title'],
  43.                 $post['text'],
  44.                 $post['id']
  45.             );
  46.         }, $this->data);
  47.     }
  48.  
  49.     /**
  50.      * {@inheritDoc}
  51.      */
  52.     public function findPost($id)
  53.     {
  54.         if (! isset($this->data[$id])) {
  55.             throw new DomainException(sprintf('Post by id "%s" not found', $id));
  56.         }
  57.        
  58.         return new Post(
  59.                 $this->data[$id]['title'],
  60.                 $this->data[$id]['text'],
  61.                 $this->data[$id]['id']
  62.         );
  63.         }
  64.     }
Add Comment
Please, Sign In to add comment