Last active
August 29, 2015 14:17
-
-
Save klimesf/23fc71e9b734ce11ed39 to your computer and use it in GitHub Desktop.
Entity Visitor
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
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() {} | |
} |
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 Entity | |
{ | |
function acceptEntityVisitor(EntityVisitor $visitor); | |
} |
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
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"; | |
} | |
} |
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 EntityVisitor | |
{ | |
function visitFooEntity(FooEntity $entity); | |
function visitBarEntity(BarEntity $entity); | |
} |
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
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