Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mvriel
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvriel/b7b0e93db6ecb04ecb0c to your computer and use it in GitHub Desktop.
Save mvriel/b7b0e93db6ecb04ecb0c to your computer and use it in GitHub Desktop.
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