Last active
August 29, 2015 14:13
-
-
Save mvriel/b7b0e93db6ecb04ecb0c to your computer and use it in GitHub Desktop.
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
interface MapperInterface | |
{ | |
public function map(); | |
} | |
abstract class BaseUserMapper | |
{ | |
public function __construct($userEntity) | |
{ | |
$this->user = $userEntity; | |
} | |
abstract public function map(); | |
public function toArray() | |
{ | |
return [ | |
'id' => $this->comment->getId(), | |
'username' => $this->comment->getUsername() | |
]; | |
} | |
} | |
abstract class BaseCommentMapper | |
{ | |
public function __construct($commentEntity) | |
{ | |
$this->comment = $commentEntity; | |
} | |
abstract public function map(); | |
public function toArray() | |
{ | |
return [ | |
'id' => $this->comment->getId(), | |
'text' => $this->comment->getText() | |
]; | |
} | |
} | |
class JsonUserMapper extends BaseUserMapper implements MapperInterface | |
{ | |
public function map() | |
{ | |
// do user specific mapping stuff for json files | |
return json_decode($this->toArray()); | |
} | |
} | |
class JsonCommentMapper extends BaseCommentMapper implements MapperInterface | |
{ | |
public function map() | |
{ | |
// do comment specific mapping stuff for json files | |
return json_decode($this->toArray()); | |
} | |
} | |
class Writer | |
{ | |
public function write(MapperInterface $mapper) | |
{ | |
file_put_contents('output.file', $this->mapper->map()); | |
} | |
} | |
$writer = new Writer(); | |
// does matter which mapper you use; the MapperInterface makes sure there is always a map method | |
$writer->write(new JsonCommentMapper($comment)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment