/** * Get the top tracks tagged by this tag, ordered by tag count. * * @param string $tag the tag name * @param int $limit the number of results to fetch per page. Defaults to 50 * @param int $page the page number to fetch. Defaults to first page */ public function getTopTracks($tag, $limit = null, $page = null) { $response = $this->call(array('method' => 'tag.getTopTracks', 'tag' => $tag, 'limit' => $limit, 'page' => $page)); $tracks = array(); if (!empty($response->toptracks->track)) { foreach ($response->toptracks->track as $track) { $tracks[(int) $track->attributes()->rank] = LastfmModel\Track::createFromResponse($track); } } return $tracks; }
public function testCreateFromPartialResponse() { $mockResponse = $this->createMockResponse('MockTrackPartialResponse'); $track = Track::createFromResponse($mockResponse); $this->assertInstanceOf('BinaryThinking\\LastfmBundle\\Lastfm\\Model\\Track', $track, 'object created is not an instance of Track'); $this->assertNotEmpty($track->getMbid(), 'track mbid is empty'); $this->assertNotEmpty($track->getName(), 'track name is empty'); $this->assertNotEmpty($track->getUrl(), 'track url is empty'); $this->assertNotEmpty($track->getArtist(), 'track artist is empty'); $this->assertInstanceOf('BinaryThinking\\LastfmBundle\\Lastfm\\Model\\Artist', $track->getArtist(), 'object created is not an instance of Artist'); $this->assertNotEmpty($track->getDuration(), 'track duration is empty'); $this->assertNotEmpty($track->getNumber(), 'track number is empty'); $this->assertNotEmpty($track->getStreamable(), 'track streamable is empty'); }
public static function createFromResponse(\SimpleXMLElement $response) { $album = new Album(); $album->setRawResponse($response); $album->setId((int) $response->id); $album->setName((string) $response->name); $artisNodeCount = count($response->artist->children()); if (!empty($artisNodeCount)) { $artist = Artist::createFromResponse($response->artist); } else { $artist = new Artist(); $artist->setName((string) $response->artist); } $album->setArtist($artist); $album->setUrl((string) $response->url); $images = array(); foreach ($response->image as $image) { $images[] = (string) $image; } $album->setImages($images); $album->setStreamable((bool) $response->streamable); $album->setMbid((string) $response->mbid); $album->setReleaseDate((string) $response->releasedate); $album->setListeners((int) $response->listeners); $album->setPlayCount((int) $response->playcount); $album->setStreamable((int) $response->streamable); $topTags = array(); foreach ($response->toptags as $topTag) { $topTags[] = $topTag; } $album->setTopTags($topTags); $tracks = array(); if (!empty($response->tracks->track)) { foreach ($response->tracks->track as $track) { $tracks[] = Track::createFromResponse($track); } $album->setTracks($tracks); } return $album; }
/** * Get the top tracks by an artist on Last.fm, ordered by popularity * * @param string $artist the artist name * @param string $mbid the musicbrainz id for the artist * @param int $limit the number of results to fetch per page. Defaults to 50 * @param int $page the page number to fetch. Defaults to first page * @param bool $autocorrect transform misspelled artist names into correct artist names * @param string $saveXmlFilePath path to save raw xml response */ public function getTopTracks($artist, $mbid = null, $limit = null, $page = null, $autocorrect = true, $saveXmlFilePath = '') { $response = $this->call(array('method' => 'artist.getTopTracks', 'artist' => $artist, 'mbid' => $mbid, 'autocorrect' => $autocorrect, 'limit' => $limit, 'page' => $page)); $tracks = array(); if (!empty($response->toptracks->track)) { if (!empty($saveXmlFilePath)) { try { $response->saveXML($saveXmlFilePath); } catch (\Exception $e) { throw new \Exception('Unable to save xml response'); } } foreach ($response->toptracks->track as $topTrack) { $topTrackAttribues = $topTrack->attributes(); $topTrack = LastfmModel\Track::createFromResponse($topTrack); $tracks[(int) $topTrackAttribues->rank] = $topTrack; } } return $tracks; }
public static function createFromResponse(\SimpleXMLElement $response) { $track = new Track(); $track->setRawResponse($response->asXML()); $trackAttributes = $response->attributes(); if (isset($trackAttributes->rank)) { $track->setNumber((int) $trackAttributes->rank); } $track->setName((string) $response->name); $track->setDuration((int) $response->duration); $track->setMbid((string) $response->mbid); $track->setUrl((string) $response->url); $track->setStreamable((int) $response->streamable); if (!empty($response->artist)) { $track->setArtist(Artist::createFromResponse($response->artist)); } $track->setPlaycount((int) $response->playcount); $track->setListeners((int) $response->listeners); $images = array(); foreach ($response->image as $image) { $imageAttributes = $image->attributes(); if (!empty($imageAttributes->size)) { $images[(string) $imageAttributes->size] = (string) $image; } } $track->setImages($images); return $track; }