$number_of_songs = $this->_radio_program->getNumberOfSongs() + $this->_radio_program->getNumberOfRequestSongs(); return $this->_index < $number_of_songs; } public function next() { // $this->_indexが$this->_radio_program->getNumberOfSongs()を超えていた場合はリクエスト曲を再生する。 if ($this->_index < $this->_radio_program->getNumberOfSongs()) { $song = $this->_radio_program->getSong($this->_index); } else { $song = $this->_radio_program->getRequestSong($this->_index - $this->_radio_program->getNumberOfSongs()); } $this->_index++; return $song; } } $radio_program = new RadioProgram(); $radio_program->addRequestSong('Perfume - チョコレイト・ディスコ'); $radio_program->addRequestSong('Perfume - レーザービーム'); $radio_program->addRequestSong('Perfume - ポリリズム'); $radio_program->addRequestSong('Perfume - ナチュラルに恋して'); $radio_program->addRequestSong('Perfume - ねぇ'); $dj = $radio_program->iterator(); while ($dj->hasNext()) { echo 'Play: ' . $dj->next() . PHP_EOL; } /* echo Play: Cypress Hill - Insane In The Brain Play: Beastie Boys - So What Cha Want Play: Chingy - Right Thurr Play: Nas - Nas Is Like Play: House of Pain - Jump Around
<html> <head> <?php include "layout.php"; ?> </head> <body> <div class="container"> <div class="content"> <?php include "RadioProgram.php"; $radioProgram = new RadioProgram(); $radioProgram->name = "Henkie's radioprogramma"; $radioProgram->description = "De beste radio van het land!"; $radioProgram->addSong("liedje1"); $radioProgram->addSong("liedje2"); $radioProgram->addSong("liedje3"); echo $radioProgram->name . "<br>"; echo $radioProgram->description . "<br><br>"; echo "Liedjes:<br>"; foreach ($radioProgram->songs as $song) { echo $song . "<br>"; } ?> <br> <a href="https://github.com/jmoritdev/jmoritphp/blob/master/radiostation.php">Source code</a> </div> </div> </body> </html>
} // DJクラス class DiscJockey implements InterfaceIterator { private $_radio_program = null; private $_index = 0; public function __construct(RadioProgram $radio_program) { $this->_radio_program = $radio_program; } public function hasNext() { return $this->_index < $this->_radio_program->getNumberOfSongs(); } public function next() { return $this->_radio_program->getSong($this->_index++); } } $radio_program = new RadioProgram(); $dj = $radio_program->iterator(); while ($dj->hasNext()) { echo $dj->next() . PHP_EOL; } /* echo Cypress Hill - Insane In The Brain Beastie Boys - So What Cha Want Chingy - Right Thurr Nas - Nas Is Like House of Pain - Jump Around */