Ejemplo n.º 1
0
 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');
 }
Ejemplo n.º 3
0
 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;
 }
 public static function createFromResponse(\SimpleXMLElement $response)
 {
     $artist = new Artist();
     $artist->setRawResponse($response->asXML());
     $artist->setName((string) $response->name);
     $artist->setMbid((string) $response->mbid);
     $artist->setUrl((string) $response->url);
     $images = array();
     if (!empty($response->image)) {
         foreach ($response->image as $image) {
             $imageAttributes = $image->attributes();
             if (!empty($imageAttributes->size)) {
                 $images[(string) $imageAttributes->size] = (string) $image;
             }
         }
     }
     $artist->setImages($images);
     $artist->setStreamable((int) $response->streamable);
     if (!empty($response->stats)) {
         $artist->setListeners((int) $response->stats->listeners);
         $artist->setPlayCount((int) $response->stats->playcount);
     } elseif (isset($response->listeners)) {
         $artist->setListeners((int) $response->listeners);
     }
     $similar = array();
     if (!empty($response->similar->artist)) {
         foreach ($response->similar->artist as $similarArtistXML) {
             $similarArtist = self::createFromResponse($similarArtistXML);
             if (!empty($similarArtist)) {
                 $similar[$similarArtist->getName()] = $similarArtist;
             }
         }
     }
     $artist->setSimilar($similar);
     $tags = array();
     if (!empty($response->tags->tag)) {
         foreach ($response->tags->tag as $tag) {
             $tags[] = Tag::createFromResponse($tag);
         }
     }
     $artist->setTags($tags);
     $bio = array();
     if (!empty($response->bio)) {
         $bio['published'] = (string) $response->bio->published;
         $bio['summary'] = (string) $response->bio->summary;
         $bio['content'] = (string) $response->bio->content;
     }
     $artist->setBio($bio);
     $artist->setWeight((int) $response->weight);
     return $artist;
 }