Created
July 1, 2011 13:18
-
-
Save redpanda/1058528 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Team | |
| { | |
| protected $name; | |
| public function __construct($name) | |
| { | |
| $this->name = $name; | |
| } | |
| public function getName() | |
| { | |
| return $this->name; | |
| } | |
| } | |
| class Request | |
| { | |
| protected $modelId; | |
| protected $modelName; | |
| public function __construct($modelId, $modelName) | |
| { | |
| $this->modelId = $modelId; | |
| $this->modelName = $modelName; | |
| } | |
| public function getModelId() | |
| { | |
| return $this->modelId; | |
| } | |
| public function getModelName() | |
| { | |
| return $this->modelName; | |
| } | |
| public function getModel() | |
| { | |
| return new Team('baz'); | |
| } | |
| } | |
| class Post | |
| { | |
| protected $name; | |
| public function __construct($name) | |
| { | |
| $this->name = $name; | |
| } | |
| public function getName() | |
| { | |
| return $this->name; | |
| } | |
| } | |
| abstract class Streamer | |
| { | |
| protected $record; | |
| protected $resolver; | |
| public function setRecord($record) | |
| { | |
| $this->record = $record; | |
| } | |
| public function setResolver(StreamerResolver $resolver) | |
| { | |
| $this->resolver = $resolver; | |
| } | |
| public function getRecord() | |
| { | |
| return $this->record; | |
| } | |
| abstract public function supports($record); | |
| abstract public function getTitle(); | |
| } | |
| class TeamStreamer extends Streamer | |
| { | |
| public function supports($record) | |
| { | |
| return $record instanceof Team; | |
| } | |
| public function getTitle() | |
| { | |
| return sprintf('Team "%s"', $this->getRecord()->getName()); | |
| } | |
| } | |
| class RequestStreamer extends Streamer | |
| { | |
| public function supports($record) | |
| { | |
| return $record instanceof Request; | |
| } | |
| public function getTitle() | |
| { | |
| return $this->resolver->resolve($this->getRecord())->getTitle(); | |
| } | |
| public function getRecord() | |
| { | |
| return $this->record->getModel(); | |
| } | |
| } | |
| class PostStreamer extends Streamer | |
| { | |
| public function supports($record) | |
| { | |
| return $record instanceof Post; | |
| } | |
| public function getTitle() | |
| { | |
| return sprintf('Post "%s"', $this->getRecord()->getName()); | |
| } | |
| } | |
| class StreamerResolver | |
| { | |
| protected $streamers; | |
| public function __construct(array $streamers = array()) | |
| { | |
| $this->streamers = array(); | |
| foreach ($streamers as $streamer) { | |
| $this->addStreamer($streamer); | |
| } | |
| } | |
| public function addStreamer(Streamer $streamer) | |
| { | |
| $this->streamers[] = $streamer; | |
| $streamer->setResolver($this); | |
| } | |
| public function getStreamers() | |
| { | |
| return $this->streamers; | |
| } | |
| public function resolve($record) | |
| { | |
| $streamer = false; | |
| foreach ($this->getStreamers() as $streamer) { | |
| if ($streamer->supports($record)) { | |
| $streamer->setRecord($record); | |
| break; | |
| } | |
| } | |
| if (false === $streamer) { | |
| throw new RuntimeException( | |
| 'There is no messenger resolver for the given record.' | |
| ); | |
| } | |
| return $streamer; | |
| } | |
| } | |
| $streamerResolver = new StreamerResolver(); | |
| $streamerResolver->addStreamer(new TeamStreamer()); | |
| $streamerResolver->addStreamer(new RequestStreamer()); | |
| $streamerResolver->addStreamer(new PostStreamer()); | |
| $team = new Team('foo'); | |
| $request = new Request(1, 'Team'); | |
| $post = new Post('bar'); | |
| echo $streamerResolver->resolve($request)->getTitle()."\n"; | |
| echo $streamerResolver->resolve($team)->getTitle()."\n"; | |
| echo $streamerResolver->resolve($post)->getTitle()."\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment