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

Source for file Response.php

Documentation is available at Response.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Services_Yahoo_JP News Response
  6.  *
  7.  * PHP version 5
  8.  *
  9.  * LICENSE: This source file is subject to the New BSD license that is
  10.  * available through the world-wide-web at the following URI:
  11.  * http://www.opensource.org/licenses/bsd-license.php. If you did not receive
  12.  * a copy of the New BSD License and are unable to obtain it through the web,
  13.  * please send a note to license@php.net so we can mail you a copy immediately.
  14.  *
  15.  * @category  Services
  16.  * @package   Services_Yahoo_JP
  17.  * @author    Tetsuya Nakase <phpizer@gmail.com>
  18.  * @copyright 2008 Tetsuya Nakase
  19.  * @license   http://www.opensource.org/licenses/bsd-license.php BSD
  20.  * @version   CVS: $Id: Response.php,v 1.1 2008/04/28 15:59:30 tetsuya Exp $
  21.  * @link      http://phpize.net
  22.  */
  23.  
  24. /**
  25.  * Services_Yahoo News Response class
  26.  *
  27.  * This class provides methods for accessing the response of a category
  28.  * request.
  29.  *
  30.  * @category  Services
  31.  * @package   Services_Yahoo_JP
  32.  * @extends   Exception
  33.  * @author    Tetsuya Nakase <phpizer@gmail.com>
  34.  * @copyright 2008 Tetsuya Nakase
  35.  * @license   http://www.opensource.org/licenses/bsd-license.php BSD
  36.  * @version   Release: 0.0.1
  37.  * @link      http://phpize.net
  38.  */
  39. class Services_Yahoo_JP_News_Response implements Iterator
  40. {
  41.     /**
  42.      * validate flag
  43.      *
  44.      * @access private
  45.      * @var    bool 
  46.      */
  47.     private $_isValidIterator = true;
  48.  
  49.     /**
  50.      * counter
  51.      *
  52.      * @access private
  53.      * @var    intger 
  54.      */
  55.     private $_iteratorCounter = 0;
  56.  
  57.     /**
  58.      * request object
  59.      *
  60.      * @access private
  61.      * @var    object 
  62.      */
  63.     private $_request;
  64.  
  65.     /**
  66.      * result
  67.      *
  68.      * @access private
  69.      * @var    array 
  70.      */
  71.     private $_results = array();
  72.  
  73.     /**
  74.      * Constructor
  75.      *
  76.      * @param object $request HTTP_Request Instance of
  77.      *                         HTTP_Request that was used for the request
  78.      *
  79.      * @throws Services_Yahoo_Exception
  80.      */
  81.     public function __construct(HTTP_Request $request)
  82.     {
  83.         $this->_request $request;
  84.  
  85.         $this->_parseRequest();
  86.         
  87.         if ($this->_isError(== true{
  88.             $exception = new Services_Yahoo_Exception("News query failed");
  89.             $exception->addErrors($this->_getMessages());
  90.  
  91.             throw $exception;
  92.         }
  93.     }
  94.  
  95.     // {{{ response handling
  96.  
  97.     /**
  98.      * Get number of result sets returned by the content analysis
  99.      *
  100.      * @return integer Number of result sets returned
  101.      */
  102.     public function getTotalResultsReturned()
  103.     {
  104.         return count((array)$this->xml->Result);
  105.     }
  106.  
  107.     /**
  108.      * Get the HTTP_Request instance that was used for the query
  109.      *
  110.      * Access to the HTTP_Request instance is useful for introspecting
  111.      * into the request details.  (E.g. for getting the HTTP response
  112.      * code.)
  113.      *
  114.      * @return object HTTP_Request Instance of HTTP_Request
  115.      */
  116.     public function getRequest()
  117.     {
  118.         return $this->_request;
  119.     }
  120.  
  121.     // }}}
  122.     // {{{ Iterator implementation
  123.  
  124.     /**
  125.      * get current result of response
  126.      *
  127.      * @return array current result of response
  128.      */
  129.     public function current()
  130.     {
  131.         return (array)$this->xml->Result[$this->_iteratorCounter];
  132.     }
  133.  
  134.     /**
  135.      * get next result of response
  136.      *
  137.      * @return array next result of response
  138.      */
  139.     public function next()
  140.     {
  141.         $this->_iteratorCounter++;
  142.         if (!isset($this->xml->Result[$this->_iteratorCounter])) {
  143.             $this->_isValidIterator = false;
  144.         }
  145.     }
  146.  
  147.     /**
  148.      * get counter for iterator
  149.      *
  150.      * @return string counter
  151.      */
  152.     public function key()
  153.     {
  154.         return $this->_iteratorCounter;
  155.     }
  156.  
  157.     /**
  158.      * clear counter for  iterator
  159.      *
  160.      * @return void 
  161.      */
  162.     public function rewind()
  163.     {
  164.         $this->_iteratorCounter = 0;
  165.     }
  166.  
  167.     /**
  168.      * get validate status
  169.      *
  170.      * @return bool validate status
  171.      */
  172.     public function valid()
  173.     {
  174.         return $this->_isValidIterator;
  175.     }
  176.  
  177.     // }}}
  178.     // {{{ private methods
  179.  
  180.     /**
  181.      * Parse XML from the response
  182.      *
  183.      * @throws Services_Yahoo_Exception
  184.      *
  185.      * @return void 
  186.      */
  187.     private function _parseRequest()
  188.     {
  189.         $this->xml simplexml_load_string($this->_request->getResponseBody());
  190.  
  191.         if ($this->xml === false{
  192.             throw
  193.                 new Services_Yahoo_Exception("The response contained no valid XML");
  194.         }
  195.     }
  196.  
  197.     /**
  198.      * Determine if an error was returned by the Yahoo API
  199.      *
  200.      * This method evaluates the HTTP response code. If it indicates
  201.      * an error, the method returns true.
  202.      *
  203.      * @return boolean  True on error, otherwise false.
  204.      */
  205.     private function _isError()
  206.     {
  207.         return
  208.             in_array($this->_request->getResponseCode()array(400403404503));
  209.     }
  210.  
  211.     /**
  212.      * Get all error messages if the response contained an error
  213.      *
  214.      * Returns all errors in an numerically indexed array that were
  215.      * part of the response.
  216.      *
  217.      * @see    _isError()
  218.      * @return array 
  219.      */
  220.     private function _getMessages()
  221.     {
  222.         $returnValue = array();
  223.         foreach ($this->xml->Message as $message{
  224.             $returnValue[$message;
  225.         }
  226.  
  227.         return $returnValue;
  228.     }
  229.  
  230.     /**
  231.      * get Attribute
  232.      *
  233.      * @param string $name key in result
  234.      *
  235.      * @return string value of attribute
  236.      */
  237.     public function returnAttribute($name)
  238.     {
  239.         if (isset($this->xml[$name])) {
  240.             return $this->xml[$name];
  241.         }
  242.  
  243.         return null;
  244.     }
  245.  
  246.     // }}}
  247. }

Documentation generated on Fri, 19 Sep 2008 21:30:06 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.