Services_Digg
[ class tree: Services_Digg ] [ index: Services_Digg ] [ all elements ]

Source for file php.php

Documentation is available at php.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * API for Digg's web services
  7.  *
  8.  * PHP version 5.1.0+
  9.  *
  10.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  11.  * that is available through the world-wide-web at the following URI:
  12.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  13.  * the PHP License and are unable to obtain it through the web, please
  14.  * send a note to license@php.net so we can mail you a copy immediately.
  15.  *
  16.  * @category    Services
  17.  * @package     Services_Digg
  18.  * @author      Joe Stump <joe@joestump.net>
  19.  * @copyright   1997-2007 The PHP Group
  20.  * @license     http://www.php.net/license/3_0.txt  PHP License 3.0
  21.  * @version     CVS: $Id:$
  22.  * @link        http://pear.php.net/package/Services_Digg
  23.  */
  24.  
  25. require_once 'Services/Digg/Response/Common.php';
  26.  
  27. /**
  28.  * Services_Digg_Response_php
  29.  *  
  30.  * Parses the PHP response type from the Digg API. Digg's API responds with
  31.  * PHP, JSON and XML currently.
  32.  *  
  33.  * @category    Services
  34.  * @package     Services_Digg
  35.  * @author      Joe Stump <joe@joestump.net>
  36.  */
  37. {
  38.     /**
  39.      * Parse PHP respnse
  40.      *
  41.      * @access      public
  42.      * @return      mixed       DiggAPIError on failure
  43.      */
  44.     public function parse()
  45.     {
  46.         $result @unserialize($this->response);
  47.         if (!is_object($result)) {
  48.             throw new Services_Digg_Response_Exception('Could not parse result');
  49.         }
  50.  
  51.         if ($result instanceof DiggAPIError{
  52.             throw new Services_Digg_Response_Exception($result->message$result->code);
  53.         }
  54.  
  55.         return $result;
  56.     }
  57. }
  58.  
  59. if (!class_exists('DiggAPIStory'false)) {
  60.     require_once 'Services/Digg/Story.php';
  61.  
  62.     /**
  63.      * DiggAPIStory
  64.      *
  65.      * @author      Joe Stump <joe@joestump.net>
  66.      * @see         Services_Digg_Story
  67.      */
  68.     class DiggAPIStory extends Services_Digg_Story
  69.     {
  70.         public function __call($functionarray $args)
  71.         {
  72.             $params = array();
  73.             if (isset($args[0]&& is_array($args[0]&& count($args[0])) {
  74.                 $params $args[0];
  75.             }
  76.  
  77.             return parent::__call($functionarray($this->id$params));
  78.         }
  79.  
  80.         /**
  81.          * Get comment activity for a story
  82.          * 
  83.          * @access      public
  84.          * @param       array       $params     Digg API arguments
  85.          * @throws      PEAR_Exception
  86.          */
  87.         public function getCommentActivity(array $params = array()) 
  88.         {
  89.             return parent::getCommentActivity($this->id$params);
  90.         }
  91.  
  92.         /**
  93.          * Get digg activity for a story
  94.          * 
  95.          * @access      public
  96.          * @param       array       $params     Digg API arguments
  97.          * @throws      PEAR_Exception
  98.          */
  99.         public function getDiggActivity(array $params = array()) 
  100.         {
  101.             return parent::getCommentActivity($this->id$params);
  102.         }
  103.     }
  104. }
  105.  
  106. if (!class_exists('DiggAPIGalleryPhoto'false)) {
  107.     require_once 'Services/Digg/GalleryPhoto.php';
  108.  
  109.     /**
  110.      * DiggAPIStory
  111.      *
  112.      * @author      Joe Stump <joe@joestump.net>
  113.      * @see         Services_Digg_GalleryPhoto
  114.      */
  115.     {
  116.         /**
  117.          * Make a second service call
  118.          *
  119.          * @param       string      $function 
  120.          * @param       array       $args 
  121.          */
  122.         public function __call($functionarray $args)
  123.         {
  124.             $params = array();
  125.             if (isset($args[0]&& is_array($args[0]&& count($args[0])) {
  126.                 $params $args[0];
  127.             }
  128.  
  129.             return parent::__call($functionarray($this->id$params));
  130.         }
  131.     }
  132. }
  133.  
  134.  
  135. if (!class_exists('DiggAPIError'false)) {
  136.     /**
  137.      * DiggAPIError
  138.      *
  139.      * @category    Services
  140.      * @package     Services_Digg
  141.      * @author      Joe Stump <joe@joestump.net>
  142.      * @throws      PEAR_Exception
  143.      */
  144.     class DiggAPIError 
  145.     {
  146.         /**
  147.          * Error message
  148.          *
  149.          * @access      public
  150.          * @var         string      $message    Error message
  151.          */
  152.         public $message = '';
  153.  
  154.         /**
  155.          * Error code
  156.          *
  157.          * @access      public
  158.          * @var         int         $code       Error code
  159.          */
  160.         public $code = 0;
  161.  
  162.         /**
  163.          * Get error message
  164.          *
  165.          * @access      public
  166.          * @return      string      Error message
  167.          */
  168.         public function getMessage()
  169.         {
  170.             return $this->message;
  171.         }
  172.  
  173.         /**
  174.          * Get error code
  175.          *
  176.          * @access      public
  177.          * @return      int         Error code
  178.          */
  179.         public function getCode()
  180.         {
  181.             return $this->code;
  182.         }
  183.     }
  184. }
  185.  
  186. if (!class_exists('DiggAPIUser'false)) {
  187.     require_once 'Services/Digg/User.php';
  188.  
  189.     /**
  190.      * DiggAPIUser
  191.      *
  192.      * @category    Services
  193.      * @package     Services_Digg
  194.      * @author      Joe Stump <joe@joestump.net>
  195.      * @see         Services_Digg_User
  196.      */
  197.     class DiggAPIUser extends Services_Digg_User
  198.     {
  199.         /**
  200.          * __call
  201.          * 
  202.          * @access      public
  203.          * @param       string      $function 
  204.          * @param       array       $args 
  205.          * @return      mixed 
  206.          */
  207.         public function __call($functionarray $args
  208.         {
  209.             $params = array();
  210.             if (isset($args[0]&& is_array($args[0]&& count($args[0])) {
  211.                 $params $args[0];
  212.             }
  213.  
  214.             return parent::__call($functionarray($this->name$params));
  215.         }
  216.  
  217.         /**
  218.          * Get a user's comment activity
  219.          *
  220.          * @access      public
  221.          * @param       array       $params 
  222.          * @throws      PEAR_Exception
  223.          */
  224.         public function getCommentActivity(array $params = array()) 
  225.         {
  226.             return parent::getCommentActivity($this->name$params);
  227.         }
  228.  
  229.         /**
  230.          * Get a user's digg activity
  231.          *
  232.          * @access      public
  233.          * @param       array       $params 
  234.          * @throws      PEAR_Exception
  235.          */
  236.         public function getDiggsActivity(array $params = array()) 
  237.         {
  238.             return parent::getDiggsActivity($this->name$params);
  239.         }
  240.  
  241.         /**
  242.          * Is the user friends with a person
  243.          *
  244.          * @access      public
  245.          * @param       string      $friend         Username to check for
  246.          * @return      boolean 
  247.          */
  248.         public function isFriend($friend)
  249.         {
  250.             return parent::isFan($this->name$friend);
  251.         }
  252.  
  253.         /**
  254.          * Is the person a fan of this user
  255.          *
  256.          * @access      public
  257.          * @param       string      $fan            Username to check for
  258.          * @return      boolean 
  259.          */
  260.         public function isFan($fan)
  261.         {
  262.             return parent::isFan($this->name$fan);
  263.         }
  264.  
  265.         /**
  266.          * Get a user's friends' submissions
  267.          *
  268.          * @access      public
  269.          * @param       array       $params 
  270.          * @throws      Services_Digg_Exception
  271.          */
  272.         public function getFriendsSubmissions(array $params = array())
  273.         {
  274.             $endPoint '/user/' $this->name . '/friends/submissions';
  275.             return $this->sendRequest($endPoint$params);
  276.         }
  277.  
  278.         /**
  279.          * Get a user's friends' dugg stories
  280.          *
  281.          * @access      public
  282.          * @param       array       $params 
  283.          * @throws      Services_Digg_Exception
  284.          */
  285.         public function getFriendsDugg(array $params = array())
  286.         {
  287.             $endPoint '/user/' $this->name . '/friends/dugg';
  288.             return $this->sendRequest($endPoint$params);
  289.         }
  290.     
  291.         /**
  292.          * Get a user's friends' commented stories
  293.          *
  294.          * @access      public
  295.          * @param       array       $params 
  296.          * @throws      Services_Digg_Exception
  297.          */
  298.         public function getFriendsCommented(array $params = array())
  299.         {
  300.             $endPoint '/user/' $this->name . '/friends/commented';
  301.             return $this->sendRequest($endPoint$params);
  302.         }
  303.  
  304.         /**
  305.          * Get a user's friends' popular stories that they dugg
  306.          *
  307.          * @access      public
  308.          * @param       array       $params 
  309.          * @throws      Services_Digg_Exception
  310.          */
  311.         public function getFriendsPopular(array $params = array())
  312.         {
  313.             $endPoint '/user/' $this->name . '/friends/popular';
  314.             return $this->sendRequest($endPoint$params);
  315.         }
  316.     
  317.         /**
  318.          * Get a user's friends' upcoming stories that they dugg
  319.          *
  320.          * @access      public
  321.          * @param       array       $params 
  322.          * @throws      Services_Digg_Exception
  323.          */
  324.         public function getFriendsUpcoming(array $params = array())
  325.         {
  326.             $endPoint '/user/' $this->name . '/friends/upcoming';
  327.             return $this->sendRequest($endPoint$params);
  328.         }
  329.     }
  330. }
  331.  
  332. if (!class_exists('DiggAPIComment'false)) {
  333.     require_once 'Services/Digg/Comment.php';
  334.  
  335.     /**
  336.      * DiggAPIComment
  337.      *
  338.      * @category    Services
  339.      * @package     Services_Digg
  340.      * @author      Joe Stump <joe@joestump.net>
  341.      * @see         Services_Digg_Comment
  342.      */
  343.     class DiggAPIComment extends Services_Digg_Comment
  344.     {
  345.         /**
  346.          * Get a comment's replies
  347.          *
  348.          * This function returns replies to the current comment. It only makes
  349.          * the second API call if the reply count is greater than zero.
  350.          *
  351.          * @access      public
  352.          * @param       array       $params     Digg API arguments
  353.          * @return      object      Instance of DiggAPIEvents
  354.          */
  355.         public function replies(array $params = array())
  356.         {
  357.             if ($this->replies > 0{
  358.                 $endPoint '/story/' $this->story '/comment/' $this->id .
  359.                             '/replies';
  360.                 return $this->sendRequest($endPoint$params);
  361.             }
  362.  
  363.             $ret = new DiggAPIEvents();
  364.             $ret->timestamp = time();
  365.             $ret->total = $ret->offset = $ret->count = 0;
  366.             return $ret;
  367.         }
  368.  
  369.         /**
  370.          * __toString
  371.          *
  372.          * @access      public
  373.          * @return      string      Comment's content
  374.          */
  375.         public function __toString(
  376.         {
  377.             return $this->content;
  378.         }
  379.     }
  380. }
  381.  
  382. if (!class_exists('DiggAPIActivityPeriod'false)) {
  383.     /**
  384.      * DiggAPIActivityPeriod
  385.      *
  386.      * @category    Services
  387.      * @package     Services_Digg
  388.      * @author      Joe Stump <joe@joestump.net>
  389.      */
  390.     class DiggAPIActivityPeriod
  391.     {
  392.  
  393.     }
  394. }
  395.  
  396. if (!class_exists('DiggAPIActivity'false)) {
  397.     /**
  398.      * DiggAPIActivity
  399.      *
  400.      * @category    Services
  401.      * @package     Services_Digg
  402.      * @author      Joe Stump <joe@joestump.net>
  403.      */
  404.     class DiggAPIActivity
  405.     {
  406.         /**
  407.          * Rewind $activity array
  408.          *
  409.          * @access      public
  410.          * @return      void 
  411.          */
  412.         public function rewind(
  413.         {
  414.             if (is_array($this->activity&& count($this->activity)) {
  415.                 reset($this->activity);
  416.             }
  417.  
  418.             return false;
  419.         }
  420.  
  421.         /**
  422.          * Return current element of $activity
  423.          *
  424.          * @access      public
  425.          * @return      mixed 
  426.          */
  427.         public function current()
  428.         {
  429.             if (is_array($this->activity&& count($this->activity)) {
  430.                 return current($this->activity);
  431.             }
  432.  
  433.             return false;
  434.         }
  435.  
  436.         /**
  437.          * Return a key from $activity array
  438.          *
  439.          * @access      public
  440.          * @return      mixed 
  441.          */
  442.         public function key()
  443.         {
  444.             if (is_array($this->activity&& count($this->activity)) {
  445.                 return key($this->activity);
  446.             }
  447.  
  448.             return false;
  449.         }
  450.  
  451.         /**
  452.          * Advance the internal pointer of $activity array
  453.          *
  454.          * @access      public
  455.          * @return      mixed 
  456.          */
  457.         public function next()
  458.         {
  459.             if (is_array($this->activity&& count($this->activity)) {
  460.                 return next($this->activity);
  461.             }
  462.  
  463.             return false;
  464.         }
  465.  
  466.         /**
  467.          * Is the next iteration valid?
  468.          *
  469.          * @access      public
  470.          * @return      boolean 
  471.          */
  472.         public function valid()
  473.         {
  474.             return ($this->current(!== false);
  475.         }
  476.     }
  477. }
  478.  
  479. if (!class_exists('DiggAPIContainer'false)) {
  480.     /**
  481.      * DiggAPIContainer
  482.      *
  483.      * @category    Services
  484.      * @package     Services_Digg
  485.      * @author      Joe Stump <joe@joestump.net>
  486.      */
  487.     class DiggAPIContainer
  488.     {
  489.  
  490.     }
  491. }
  492.  
  493. if (!class_exists('DiggAPIDigg'false)) {
  494.     /**
  495.      * DiggAPIDigg
  496.      *
  497.      * @category    Services
  498.      * @package     Services_Digg
  499.      * @author      Joe Stump <joe@joestump.net>
  500.      */
  501.     class DiggAPIDigg 
  502.     {
  503.  
  504.     }
  505. }
  506.  
  507. if (!class_exists('DiggAPIErrors'false)) {
  508.     /**
  509.      * DiggAPIError
  510.      *
  511.      * @category    Services
  512.      * @package     Services_Digg
  513.      * @author      Joe Stump <joe@joestump.net>
  514.      */
  515.     class DiggAPIErrors
  516.     {
  517.         /**
  518.          * Rewind $errors array
  519.          *
  520.          * @access      public
  521.          * @return      void 
  522.          */
  523.         public function rewind(
  524.         {
  525.             if (is_array($this->errors&& count($this->errors)) {
  526.                 reset($this->errors);
  527.             }
  528.  
  529.             return false;
  530.         }
  531.  
  532.         /**
  533.          * Return current element of $errors
  534.          *
  535.          * @access      public
  536.          * @return      mixed 
  537.          */
  538.         public function current()
  539.         {
  540.             if (is_array($this->errors&& count($this->errors)) {
  541.                 return current($this->errors);
  542.             }
  543.  
  544.             return false;
  545.         }
  546.  
  547.         /**
  548.          * Return a key from $errors array
  549.          *
  550.          * @access      public
  551.          * @return      mixed 
  552.          */
  553.         public function key()
  554.         {
  555.             if (is_array($this->errors&& count($this->errors)) {
  556.                 return key($this->errors);
  557.             }
  558.  
  559.             return false;
  560.         }
  561.  
  562.         /**
  563.          * Advance the internal pointer of $errors array
  564.          *
  565.          * @access      public
  566.          * @return      mixed 
  567.          */
  568.         public function next()
  569.         {
  570.             if (is_array($this->errors&& count($this->errors)) {
  571.                 return next($this->errors);
  572.             }
  573.  
  574.             return false;
  575.         }
  576.  
  577.         /**
  578.          * Is the next iteration valid?
  579.          *
  580.          * @access      public
  581.          * @return      boolean 
  582.          */
  583.         public function valid()
  584.         {
  585.             return ($this->current(!== false);
  586.         }
  587.     }
  588.  
  589. if (!class_exists('DiggAPIEvents'false)) {
  590.     /**
  591.      * DiggAPIEvents
  592.      *
  593.      * @category    Services
  594.      * @package     Services_Digg
  595.      * @author      Joe Stump <joe@joestump.net>
  596.      */
  597.     class DiggAPIEvents implements Iterator
  598.     {
  599.         /**
  600.          * Which array to iterate
  601.          *
  602.          * The DiggAPIEvents class is returned for both comments and diggs.
  603.          * In order for PHP5 object iteration to happen we need to figure out
  604.          * which events are returned (diggs v. comments) and iterate through
  605.          * that array.
  606.          *
  607.          * @access      private
  608.          * @var         string      $iterate 
  609.          */
  610.         private $iterator = null;
  611.  
  612.         /**
  613.          * __wakeup
  614.          *
  615.          * When this is unserialized we check to see if the events listed
  616.          * are diggs or comments and then set $iterator appropriately.
  617.          *
  618.          * @access      public
  619.          * @return      void 
  620.          * @see         DiggAPIEvents::$iterator
  621.          */
  622.         public function __wakeup(
  623.         {
  624.             if (isset($this->diggs&& 
  625.                 is_array($this->diggs)) {
  626.                 $this->iterator 'diggs';
  627.             elseif (isset($this->comments&&
  628.                       is_array($this->comments)) {
  629.                 $this->iterator 'comments';
  630.             }
  631.         }
  632.  
  633.         /**
  634.          * Rewind $stories array
  635.          *
  636.          * @access      public
  637.          * @return      void 
  638.          */
  639.         public function rewind(
  640.         {
  641.             if (!is_null($this->iterator)) {
  642.                 reset($this->{$this->iterator});