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 MA 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 MA Response class
  26.  *
  27.  * This class provides methods for accessing the response of a MA
  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_MA_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("MA 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->{$this->_getResultType()}->word_list->word);
  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 
  132.             (array)$this->xml->{$this->_getResultType()}->word_list->word[$this->_iteratorCounter];
  133.     }
  134.  
  135.     /**
  136.      * get next result of response
  137.      *
  138.      * @return array next result of response
  139.      */
  140.     public function next()
  141.     {
  142.         $this->_iteratorCounter++;
  143.         if (!isset($this->xml->{$this->_getResultType()}->word_list->word[$this->_iteratorCounter])) {
  144.             $this->_isValidIterator = false;
  145.         }
  146.     }
  147.  
  148.     /**
  149.      * get counter for iterator
  150.      *
  151.      * @return string counter
  152.      */
  153.     public function key()
  154.     {
  155.         return $this->_iteratorCounter;
  156.     }
  157.  
  158.     /**
  159.      * clear counter for  iterator
  160.      *
  161.      * @return void 
  162.      */
  163.     public function rewind()
  164.     {
  165.         $this->_iteratorCounter = 0;
  166.     }
  167.  
  168.     /**
  169.      * get validate status
  170.      *
  171.      * @return bool validate status
  172.      */
  173.     public function valid()
  174.     {
  175.         return $this->_isValidIterator;
  176.     }
  177.  
  178.     // }}}
  179.     // {{{ private methods
  180.  
  181.     /**
  182.      * Parse XML from the response
  183.      *
  184.      * @throws Services_Yahoo_Exception
  185.      *
  186.      * @return void 
  187.      */
  188.     private function _parseRequest()
  189.     {
  190.         $this->xml = simplexml_load_string($this->_request->getResponseBody());
  191.  
  192.         if ($this->xml === false{
  193.             throw
  194.                 new Services_Yahoo_Exception("The response contained no valid XML");
  195.         }
  196.     }
  197.  
  198.     /**
  199.      * Determine if an error was returned by the Yahoo API
  200.      *
  201.      * This method evaluates the HTTP response code. If it indicates
  202.      * an error, the method returns true.
  203.      *
  204.      * @return boolean  True on error, otherwise false.
  205.      */
  206.     private function _isError()
  207.     {
  208.         return
  209.             in_array($this->_request->getResponseCode()array(400403404503));
  210.     }
  211.  
  212.     /**
  213.      * Get all error messages if the response contained an error
  214.      *
  215.      * Returns all errors in an numerically indexed array that were
  216.      * part of the response.
  217.      *
  218.      * @see    _isError()
  219.      * @return array 
  220.      */
  221.     private function _getMessages()
  222.     {
  223.         $returnValue = array();
  224.         foreach ($this->xml->Message as $message{
  225.             $returnValue[$message;
  226.         }
  227.  
  228.         return $returnValue;
  229.     }
  230.  
  231.     /**
  232.      * getAttribute
  233.      *
  234.      * @param string $name key in result
  235.      *
  236.      * @return string value of attribute
  237.      */
  238.     public function returnAttribute($name)
  239.     {
  240.         if (isset($this->xml[$name])) {
  241.             return $this->xml[$name];
  242.         }
  243.  
  244.         return null;
  245.     }
  246.  
  247.     /**
  248.      * getResultType
  249.      *
  250.      * @return string result type
  251.      */
  252.     private function _getResultType()
  253.     {
  254.         if (isset($this->_request->_url->querystring['results'])) {
  255.             return $this->_request->_url->querystring['results''_result';
  256.         else {
  257.             return 'ma_result';
  258.         }
  259.     }
  260.  
  261.     // }}}
  262. }

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