} public function getNextTrack() { if ($this->hasNextElement()) { return $this->_list->getSongById(++$this->_currentTrack); } return false; } public function getCurrentTrack() { return $this->_list->getSongById($this->_currentTrack); } public function hasNextElement() { if ($this->_list->getSongById($this->_currentTrack + 1)) { return true; } return false; } } $song1 = new Song('title1', 'author1'); $song2 = new Song('title2', 'author2'); $song3 = new Song('title3', 'author3'); $playlist = new Playlist(); $playlist->addTrack($song1); $playlist->addTrack($song2); $playlist->addTrack($song3); $playlistItterator = new PlaylistItterator($playlist); while ($playlistItterator->hasNextElement()) { echo $playlistItterator->getNextTrack()->getTitle() . "<br />"; }
protected function set() { #find all the events for this day, as well as the event artists and tracks $query = $this->entityManager->createQuery('SELECT e,a FROM Event e JOIN e.artist a WHERE e.date LIKE :date'); $query->setParameter("date", "%" . $this->date->format('m-d')); $events = $query->getResult(); $artistRepository = $this->entityManager->getRepository('Artist'); $artists = $artistRepository->findBy(array("hasTracks" => NULL)); #do not show playlist while the cron hasn't set all artist tracks if (count($artists)) { return array("code" => 3, "status" => "No tracks for playlist found. Please try again later."); } #find tracks for each event artist and create a playlist with a track for each one of the artists foreach ($events as $event) { $artist = $event->getArtist() || next; #ignores events without artist #trackless artist will not enter the playlist if (!$artist->getTracks()->count()) { error_log("trackless: " . $artist->getName() . ". Not adding to playlist"); continue; } $tracks = $artist->getUnPublishedTracks(); #ignore artists with all the tracks already published if (!count($tracks)) { continue; } $track = array_shift($tracks); $event->setTrack($track); $track->assignToEvent($event); $this->entityManager->persist($track); $this->entityManager->persist($event); #add to playlist tracks array_push($this->tracks, $track); } #create new playlist from the tracks found if (count($this->tracks)) { $playlist = new \Playlist(); $playlist->setDate($this->date); foreach ($this->tracks as $track) { $playlist->addTrack($track); } $this->entityManager->persist($playlist); //update db $this->entityManager->flush(); //set the total tracks in the attribute $this->total = $this->total(); } return 1; }