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; }
public function testCreateFromPartialResponse() { $mockResponse = $this->createMockResponse('MockArtistPartialResponse'); $artist = Artist::createFromResponse($mockResponse); $this->assertInstanceOf('BinaryThinking\\LastfmBundle\\Lastfm\\Model\\Artist', $artist, 'object created is not an instance of Artist'); $this->assertNotEmpty($artist->getMbid(), 'artist mbid is empty'); $this->assertNotEmpty($artist->getName(), 'artist name is empty'); $this->assertNotEmpty($artist->getUrl(), 'artist url 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; }
/** * Search for an artist by name. Returns artist matches sorted by relevance. * * @param string $artist the artist 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 search($artist, $limit = null, $page = null) { $response = $this->call(array('method' => 'artist.search', 'artist' => $artist, 'limit' => $limit, 'page' => $page)); $artists = array(); if (!empty($response->results->artistmatches->artist)) { foreach ($response->results->artistmatches->artist as $artist) { $artists[] = LastfmModel\Artist::createFromResponse($artist); } } return $artists; }
/** * * @param string $tag the tag name * @param int $from the date at which the chart should start from. See getWeeklyChartList for more. * @param int $to the date at which the chart should end on. See getWeeklyChartList for more. * @param int $limit the number of results to fetch per page. Defaults to 50 */ public function getWeeklyArtistChart($tag, $from = null, $to = null, $limit = null) { $response = $this->call(array('method' => 'tag.getWeeklyArtistChart', 'tag' => $tag, 'from' => $from, 'to' => $to, 'limit' => $limit)); $artists = array(); if (!empty($response->weeklyartistchart->artist)) { foreach ($response->weeklyartistchart->artist as $artist) { $artists[] = LastfmModel\Artist::createFromResponse($artist); } } return $artists; }