Skip to content

Instantly share code, notes, and snippets.

@klimesf
Last active August 29, 2015 14:17
Show Gist options
  • Save klimesf/23fc71e9b734ce11ed39 to your computer and use it in GitHub Desktop.
Save klimesf/23fc71e9b734ce11ed39 to your computer and use it in GitHub Desktop.
Entity Visitor
class FooEntity implements Entity
{
// ... data ...
public function acceptEntityVisitor(EntityVisitor $visitor)
{
$visitor.visitFooEntity($this);
}
public function doSomething() {}
}
class BarEntity implements Entity
{
// ... data ...
public function acceptEntityVisitor(EntityVisitor $visitor)
{
$visitor.visitBarEntity($this);
}
public function doSomethingElse() {}
}
interface Entity
{
function acceptEntityVisitor(EntityVisitor $visitor);
}
class EntityService implements EntityVisitor
{
public function visitFooEntity(FooEntity $entity)
{
$entity->doSomething();
// ...
echo "Something done with a foo entity";
}
public function visitBarEntity(BarEntity $entity)
{
$entity->doSomethingElse();
// ...
echo "Something else done with a bar entity";
}
}
interface EntityVisitor
{
function visitFooEntity(FooEntity $entity);
function visitBarEntity(BarEntity $entity);
}
class Presenter extends Nette\Application\UI\Presenter
{
// ...
/**
* @inject @var EntityService
*/
public $entityService;
public function actionDoSomething($id)
{
/** @var $entity Entity */
$entity = $this->entityRepository->find($id);
$entity->acceptEntityVisitor($this->entityService);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment