Last active
February 16, 2016 14:45
-
-
Save useless-stuff/daaddfb682498db274dc to your computer and use it in GitHub Desktop.
PHP - NoRewindIterator
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 Message | |
| */ | |
| abstract class Message | |
| { | |
| protected $text; | |
| /** | |
| * Message constructor. | |
| * @param $text | |
| */ | |
| public function __construct($text) | |
| { | |
| $this->text = $text; | |
| } | |
| /** | |
| * @return mixed | |
| */ | |
| public function __toString() | |
| { | |
| return $this->text; | |
| } | |
| } | |
| /** | |
| * Class Sms | |
| */ | |
| class Sms extends Message | |
| { | |
| } | |
| /** | |
| * Class SmsCollection | |
| */ | |
| class SmsCollection extends ArrayIterator | |
| { | |
| /** | |
| * @param Sms $message | |
| */ | |
| public function append(Sms $message) | |
| { | |
| parent::append($message); | |
| } | |
| } | |
| /** | |
| * Class SmsGateway | |
| */ | |
| class SmsGateway extends NoRewindIterator | |
| { | |
| } | |
| // Generate random data | |
| $smsCollection = new SmsCollection(); | |
| for ($i = 1; $i < 101; $i++) { | |
| $smsCollection->append(new Sms("Message ".$i)); | |
| } | |
| $gateway = new SmsGateway($smsCollection); | |
| echo $gateway->current(), PHP_EOL; | |
| $gateway->next(); | |
| echo $gateway->current(), PHP_EOL; | |
| $gateway->next(); | |
| echo $gateway->current(), PHP_EOL; | |
| $gateway->rewind(); | |
| echo $gateway->current(), PHP_EOL; | |
| // Output: | |
| /* | |
| Message 1 | |
| Message 2 | |
| Message 3 | |
| Message 3 | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment