Last active
August 29, 2015 14:27
-
-
Save chukShirley/d590587951346576702b to your computer and use it in GitHub Desktop.
Persistence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class PostMapper | |
{ | |
public function objectToDb(Post $post) | |
{ | |
return [ | |
'title' => $post->getTitle(), | |
'content' => $post->getContent() | |
]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class PostRepository | |
{ | |
private $postMapper; | |
private $postTableGateway; | |
public function __construct(PostMapper $postMapper, PostTableGateway $postTableGateway) | |
{ | |
$this->postMapper = $postMapper; | |
$this->postTableGateway = $postTableGateway; | |
} | |
public function save(Post $post) | |
{ | |
// Convert from domain object to array | |
$post = $this->postMapper->objectToDb($post); | |
// Save to database | |
$this->postTableGateway->create($post); | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class PostService | |
{ | |
private $postRepository; | |
public function __construct(PostRepository $postRepository) | |
{ | |
$this->postRepository = $postRepository; | |
} | |
public function publish(Post $post) | |
{ | |
$this->postRepository->save($post); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment