Source for file Artist.php

Documentation is available at Artist.php

  1. <?
  2.  
  3. /** Represents an artist and provides different methods to query artist information.
  4.  *
  5.  * @package    php-lastfm-api
  6.  * @author  Felix Bruns <felixbruns@web.de>
  7.  * @version    1.0
  8.  */
  9. class Artist extends Media {
  10.     /** Artist is streamable.
  11.      *
  12.      * @var        boolean 
  13.      * @access    private
  14.      */
  15.     private $streamable;
  16.  
  17.     /** Similar artists.
  18.      *
  19.      * @var        array 
  20.      * @access    private
  21.      */
  22.     private $similar;
  23.  
  24.     /** Artist tags.
  25.      *
  26.      * @var        array 
  27.      * @access    private
  28.      */
  29.     private $tags;
  30.  
  31.     /** The artists biography.
  32.      *
  33.      * @var        array 
  34.      * @access    private
  35.      */
  36.     private $biography;
  37.  
  38.     /** Stores a similarity value.
  39.      *
  40.      * @var        float 
  41.      * @access    private
  42.      */
  43.     private $match;
  44.  
  45.     /** Create an Artist object.
  46.      *
  47.      * @param string    $name        Name of this artist.
  48.      * @param string    $mbid        MusicBrainz ID of this artist.
  49.      * @param string    $url        Last.fm URL of this artist.
  50.      * @param array        $images        An array of cover art images of different sizes.
  51.      * @param boolean    $streamable    Is this artist streamable?
  52.      * @param integer    $listeners    Number of listeners of this artist.
  53.      * @param integer    $playCount    Play count of this artist.
  54.      * @param array        $tags        An array of tags of this artist.
  55.      * @param array        $similar    An array of similar artists.
  56.      * @param string    $biography    Biography of this artist.
  57.      * @param float        $match        Similarity value.
  58.      *
  59.      * @access    public
  60.      */
  61.     public function __construct($name$mbid$urlarray $images$streamable,
  62.                                 $listeners$playCountarray $tags,
  63.                                 array $similar$biography$match){
  64.         parent::__construct($name$mbid$url$images$listeners$playCount);
  65.  
  66.         $this->streamable $streamable;
  67.         $this->tags       $tags;
  68.         $this->similar    $similar;
  69.         $this->biography  $biography;
  70.         $this->match      $match;
  71.     }
  72.  
  73.     /** Returns if this artists is streamable.
  74.      *
  75.      * @return    boolean    true if this artist is streamable, otherwise false.
  76.      * @access    public
  77.      */
  78.     public function isStreamable(){
  79.         return $this->streamable;
  80.     }
  81.  
  82.     /** Returns similar artists.
  83.      *
  84.      * @return    array    An array of similar artists.
  85.      * @access    public
  86.      * @see        getSimilar
  87.      */
  88.     public function getSimilarArtists(){
  89.         return $this->similar;
  90.     }
  91.  
  92.     /** Returns artist tags.
  93.      *
  94.      * @return    array    An array of Tag objects.
  95.      * @access    public
  96.      * @see        Tag
  97.      */
  98.     public function getArtistTags(){
  99.         return $this->tags;
  100.     }
  101.  
  102.     /** Returns the artists biography.
  103.      *
  104.      * @return    string    A biography text.
  105.      * @access    public
  106.      */
  107.     public function getBiography(){
  108.         return $this->biography;
  109.     }
  110.  
  111.     /** Returns similarity value.
  112.      *
  113.      * @return    float    A floating-point value from 0.0 to 1.0.
  114.      * @access    public
  115.      */
  116.     public function getMatch(){
  117.         return $this->match;
  118.     }
  119.  
  120.     /** Tag an artist with one or more user supplied tags.
  121.      *
  122.      * @param    string    $artist        The artist name in question. (Required)
  123.      * @param    array    $tags        An array of user supplied tags to apply to this artist. Accepts a maximum of 10 tags. (Required)
  124.      * @param    Session    $session    A session obtained by {@link de.felixbruns.lastfm.Auth#getSession Auth::getSession} or {@link de.felixbruns.lastfm.Auth#getMobileSession Auth::getMobileSession}. (Required)
  125.      *
  126.      * @static
  127.      * @access    public
  128.      * @throws    Error
  129.      */
  130.     public static function addTags($artistarray $tagsSession $session){
  131.         CallerFactory::getDefaultCaller()->signedCall('artist.addTags'array(
  132.             'artist' => $artist,
  133.             'tags'   => implode(','$tags)
  134.         )$session'POST');
  135.     
  136.  
  137.     /** Get a list of upcoming events for this artist. Easily integratable into calendars, using the ical standard (see feeds section below).
  138.      *
  139.      * @param    string    $artist    The artist name in question. (Required)
  140.      * @return    array            An array of Event objects.
  141.      * @see        Event
  142.      *
  143.      * @static
  144.      * @access    public
  145.      * @throws    Error
  146.      */
  147.     public static function getEvents($artist){
  148.         $xml CallerFactory::getDefaultCaller()->call('artist.getEvents'array(
  149.             'artist' => $artist
  150.         ));
  151.  
  152.         $events array();
  153.  
  154.         foreach($xml->children(as $event){
  155.             $events[Event::fromSimpleXMLElement($event);
  156.         }
  157.  
  158.         return $events;
  159.     }
  160.  
  161.     /** Get the metadata for an artist on last.fm. Includes biography.
  162.      *
  163.      * @param    string    $artist    The artist name in question. (Optional)
  164.      * @param    string    $mbid    The MusicBrainz ID for the artist. (Optional)
  165.      * @param    string    $lang    The language to return the biography in, expressed as an ISO 639 alpha-2 code. (Optional)
  166.      * @return    Artist            An Artist object.
  167.      *
  168.      * @static
  169.      * @access    public
  170.      * @throws    Error
  171.      */
  172.     public static function getInfo($artist$mbid null$lang null){
  173.         $xml CallerFactory::getDefaultCaller()->call('artist.getInfo'array(
  174.             'artist' => $artist,
  175.             'mbid'   => $mbid,
  176.             'lang'   => $lang
  177.         ));
  178.  
  179.         return Artist::fromSimpleXMLElement($xml);
  180.     }
  181.  
  182.     /** Get shouts for this artist.
  183.      *
  184.      * @param    string    $artist    The artist name in question. (Required)
  185.      * @return    array            An array of Shout objects.
  186.      *
  187.      * @static
  188.      * @access    public
  189.      * @throws    Error
  190.      */
  191.     public static function getShouts($artist){
  192.         $xml CallerFactory::getDefaultCaller()->call('artist.getShouts'array(
  193.             'artist' => $artist
  194.         ));
  195.  
  196.         $shouts array();
  197.  
  198.         foreach($xml->children(as $shout){
  199.             $shouts[Shout::fromSimpleXMLElement($shout);
  200.         }
  201.  
  202.         return $shouts;
  203.     }
  204.  
  205.     /** Get all the artists similar to this artist.
  206.      *
  207.      * @param    string    $artist    The artist name in question. (Required)
  208.      * @param    string    $limit    Limit the number of similar artists returned. (Optional)
  209.      * @return    array            An array of Artist objects.
  210.      * @see        getSimilarArtists
  211.      *
  212.      * @static
  213.      * @access    public
  214.      * @throws    Error
  215.      */
  216.     public static function getSimilar($artist$limit null){
  217.         $xml CallerFactory::getDefaultCaller()->call('artist.getSimilar'array(
  218.             'artist' => $artist,
  219.             'limit'  => $limit
  220.         ));
  221.  
  222.         $artists array();
  223.  
  224.         foreach($xml->children(as $artist){
  225.             $artists[Artist::fromSimpleXMLElement($artist);
  226.         }
  227.  
  228.         return $artists;
  229.     }
  230.  
  231.     /** Get the tags applied by an individual user to an artist on last.fm.
  232.      *
  233.      * @param    string    $artist        The artist name in question. (Required)
  234.      * @param    Session    $session    A session obtained by {@link de.felixbruns.lastfm.Auth#getSession Auth::getSession} or {@link de.felixbruns.lastfm.Auth#getMobileSession Auth::getMobileSession}. (Required)
  235.      * @return    array                An array of tags.
  236.      * @see        Tag
  237.      *
  238.      * @static
  239.      * @access    public
  240.      * @throws    Error
  241.      */
  242.     public static function getTags($artistSession $session){
  243.         $xml CallerFactory::getDefaultCaller()->signedCall('artist.getTags'array(
  244.             'artist' => $artist
  245.         )$session);
  246.  
  247.         $tags array();
  248.  
  249.         foreach($xml->children(as $tag){
  250.             $tags[Tag::fromSimpleXMLElement($tag);
  251.         }
  252.  
  253.         return $tags;
  254.     }
  255.  
  256.     /** Get the top albums for an artist on last.fm, ordered by popularity.
  257.      *
  258.      * @param    string    $artist    The artist name in question. (Required)
  259.      * @return    array            An array of Album objects.
  260.      * @see        Album
  261.      *
  262.      * @static
  263.      * @access    public
  264.      * @throws    Error
  265.      */
  266.     public static function getTopAlbums($artist){
  267.         $xml CallerFactory::getDefaultCaller()->call('artist.getTopAlbums'array(
  268.             'artist' => $artist
  269.         ));
  270.  
  271.         $albums array();
  272.  
  273.         foreach($xml->children(as $album){
  274.             $albums[Album::fromSimpleXMLElement($album);
  275.         }
  276.  
  277.         return $albums;
  278.     }
  279.  
  280.     /** Get the top fans for an artist on last.fm, based on listening data.
  281.      *
  282.      * @param    string    $artist    The artist name in question. (Required)
  283.      * @return    array            An array of User objects.
  284.      * @see        User
  285.      *
  286.      * @static
  287.      * @access    public
  288.      * @throws    Error
  289.      */
  290.     public static function getTopFans($artist){
  291.         $xml CallerFactory::getDefaultCaller()->call('artist.getTopFans'array(
  292.             'artist' => $artist
  293.         ));
  294.  
  295.         $fans array();
  296.  
  297.         foreach($xml->children(as $fan){
  298.             $fans[User::fromSimpleXMLElement($fan);
  299.         }
  300.  
  301.         return $fans;
  302.     }
  303.  
  304.     /** Get the top tags for an artist on last.fm, ordered by popularity.
  305.      *
  306.      * @param    string    $artist    The artist name in question. (Required)
  307.      * @return    array            An array of Tag objects.
  308.      * @see        Tag
  309.      *
  310.      * @static
  311.      * @access    public
  312.      * @throws    Error
  313.      */
  314.     public static function getTopTags($artist){
  315.         $xml CallerFactory::getDefaultCaller()->call('artist.getTopTags'array(
  316.             'artist' => $artist
  317.         ));
  318.  
  319.         $tags array();
  320.  
  321.         foreach($xml->children(as $tag){
  322.             $tags[Tag::fromSimpleXMLElement($tag);
  323.         }
  324.  
  325.         return $tags;
  326.     }
  327.  
  328.     /** Get the top tracks by an artist on last.fm, ordered by popularity.
  329.      *
  330.      * @param    string    $artist    The artist name in question. (Required)
  331.      * @return    array            An array of Track objects.
  332.      * @see        Track
  333.      *
  334.      * @static
  335.      * @access    public
  336.      * @throws    Error
  337.      */
  338.     public static function getTopTracks($artist){
  339.         $xml CallerFactory::getDefaultCaller()->call('artist.getTopTracks'array(
  340.             'artist' => $artist
  341.         ));
  342.  
  343.         $tracks array();
  344.  
  345.         foreach($xml->children(as $track){
  346.             $tracks[Track::fromSimpleXMLElement($track);
  347.         }
  348.  
  349.         return $tracks;
  350.     }
  351.  
  352.     /** Remove a user's tag from an artist.
  353.      *
  354.      * @param    string    $artist        The artist name in question. (Required)
  355.      * @param    string    $tag        A single user tag to remove from this artist. (Required)
  356.      * @param    Session    $session    A session obtained by {@link de.felixbruns.lastfm.Auth#getSession Auth::getSession} or {@link de.felixbruns.lastfm.Auth#getMobileSession Auth::getMobileSession}. (Required)
  357.      *
  358.      * @static
  359.      * @access    public
  360.      * @throws    Error
  361.      */
  362.     public static function removeTag($artist$tagSession $session){
  363.         CallerFactory::getDefaultCaller()->signedCall('artist.removeTag'array(
  364.             'artist' => $artist,
  365.             'tag'    => $tag
  366.         )$session'POST');
  367.     }
  368.  
  369.     /** Search for an artist by name. Returns artist matches sorted by relevance.
  370.      *
  371.      * @param    string    $artist    The artist name in question. (Required)
  372.      * @param    integer    $limit    Limit the number of artists returned at one time. Default (maximum) is 30. (Optional)
  373.      * @param    integer    $page    Scan into the results by specifying a page number. Defaults to first page. (Optional)
  374.      * @return    PaginatedResult    A PaginatedResult object.
  375.      * @see        PaginatedResult
  376.      *
  377.      * @static
  378.      * @access    public
  379.      * @throws    Error
  380.      */
  381.     public static function search($artist$limit null$page null){
  382.         $xml CallerFactory::getDefaultCaller()->call('artist.search'array(
  383.             'artist' => $artist,
  384.             'limit'  => $limit,
  385.             'page'   => $page
  386.         ));
  387.  
  388.         $artists array();
  389.  
  390.         foreach($xml->artistmatches->children(as $artist){
  391.             $artists[Artist::fromSimpleXMLElement($artist);
  392.         }
  393.  
  394.         $opensearch $xml->children('http://a9.com/-/spec/opensearch/1.1/');
  395.  
  396.         return new PaginatedResult(
  397.             Util::toInteger($opensearch->totalResults),
  398.             Util::toInteger($opensearch->startIndex),
  399.             Util::toInteger($opensearch->itemsPerPage),
  400.             $artists
  401.         );
  402.     }
  403.  
  404.     /** Share an artist with last.fm users or other friends.
  405.      *
  406.      * @param    string    $artist        The artist to share. (Required)
  407.      * @param    array    $recipients    An array email addresses or last.fm usernames. Maximum is 10. (Required)
  408.      * @param    string    $message    An optional message to send with the recommendation. If not supplied a default message will be used. (Optional)
  409.      * @param    Session    $session    A session obtained by {@link de.felixbruns.lastfm.Auth#getSession Auth::getSession} or {@link de.felixbruns.lastfm.Auth#getMobileSession Auth::getMobileSession}. (Required)
  410.      *
  411.      * @static
  412.      * @access    public
  413.      * @throws    Error
  414.      */
  415.     public static function share($artistarray $recipients$message nullSession $session){
  416.         CallerFactory::getDefaultCaller()->signedCall('artist.share'array(
  417.             'artist'    => $artist,
  418.             'recipient' => implode(','$recipients),
  419.             'message'   => $message
  420.         )$session'POST');
  421.     
  422.  
  423.     /** Get an artist playlist for streaming. INOFFICIAL.
  424.      *
  425.      * @param    string    $artist    Artist name.
  426.      * @return    Playlist        A Playlist object.
  427.      * @see        Playlist
  428.      *
  429.      * @static
  430.      * @access    public
  431.      * @throws    Error
  432.      */
  433.     public static function getPlaylist($artist){
  434.         $xml CallerFactory::getDefaultCaller()->call('artist.getPlayerMenu'array(
  435.             'artist' => $artist
  436.         ));
  437.  
  438.         return Playlist::fetch(Util::toString($xml->playlist->url)truetrue);
  439.     }
  440.  
  441.     /** Create an Artist object from a SimpleXMLElement object.
  442.      *
  443.      * @param    SimpleXMLElement    $xml    A SimpleXMLElement object.
  444.      * @return    Artist                        An Artist object.
  445.      *
  446.      * @static
  447.      * @access    public
  448.      * @internal
  449.      */
  450.     public static function fromSimpleXMLElement(SimpleXMLElement $xml){
  451.         $images  array();
  452.         $tags    array();
  453.         $similar array();
  454.  
  455.         /* NOTE: image, image_small... this sucks! */
  456.  
  457.         if($xml->image){
  458.             if(count($xml->image1){
  459.                 foreach($xml->image as $image){
  460.                     $images[Util::toImageType($image['size'])Util::toString($image);
  461.                 }
  462.             }
  463.             else{
  464.                 $images[Media::IMAGE_LARGEUtil::toString($image);
  465.             }
  466.         }
  467.  
  468.         if($xml->image_small){
  469.             $images[Media::IMAGE_SMALLUtil::toString($xml->image_small);
  470.         }
  471.  
  472.         if($xml->tags){
  473.             foreach($xml->tags->children(as $tag){
  474.                 $tags[Tag::fromSimpleXMLElement($tag);
  475.             }
  476.         }
  477.  
  478.         if($xml->similar){
  479.             foreach($xml->similar->children(as $artist){
  480.                 $similar[Artist::fromSimpleXMLElement($artist);
  481.             }
  482.         }
  483.  
  484.         return new Artist(
  485.             Util::toString($xml->name),
  486.             Util::toString($xml->mbid),
  487.             Util::toString($xml->url),
  488.             $images,
  489.             Util::toBoolean($xml->streamable),
  490.             ($xml->stats)?Util::toInteger($xml->stats->listeners):0,
  491.             ($xml->stats)?Util::toInteger($xml->stats->playcount):0,
  492.             $tags,
  493.             $similar,
  494.             ($xml->bio)?Util::toString($xml->bio->summary):""// TODO: Biography object
  495.             Util::toFloat($xml->match)
  496.         );
  497.     }
  498. }
  499.  
  500. ?>

Documentation generated on Mon, 22 Dec 2008 16:57:30 +0100 by phpDocumentor 1.4.1