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 Auction 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 Auction Response class
  26.  *
  27.  * This class provides methods for accessing the response of a Auction
  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_Auction_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.      * index key for loop
  75.      *
  76.      * @access private
  77.      * @var    string 
  78.      */
  79.     private $_indexkey;
  80.  
  81.     /**
  82.      * Constructor
  83.      *
  84.      * @param object $request HTTP_Request Instance of
  85.      *                         HTTP_Request that was used for the request
  86.      *
  87.      * @throws Services_Yahoo_Exception
  88.      */
  89.     public function __construct(HTTP_Request $request)
  90.     {
  91.         $this->_request $request;
  92.  
  93.         $this->_parseRequest();
  94.         
  95.         if ($this->_isError(== true{
  96.             $exception = new Services_Yahoo_Exception("Auction query failed");
  97.             $exception->addErrors($this->_getMessages());
  98.  
  99.             throw $exception;
  100.         }
  101.     }
  102.  
  103.     // {{{ response handling
  104.  
  105.     /**
  106.      * Get number of result sets returned by the content analysis
  107.      *
  108.      * @return integer Number of result sets returned
  109.      */
  110.     public function getTotalResultsReturned()
  111.     {
  112.         return count((array)$this->xml->{$this->_indexkey});
  113.     }
  114.  
  115.     /**
  116.      * Get the HTTP_Request instance that was used for the query
  117.      *
  118.      * Access to the HTTP_Request instance is useful for introspecting
  119.      * into the request details.  (E.g. for getting the HTTP response
  120.      * code.)
  121.      *
  122.      * @return object HTTP_Request Instance of HTTP_Request
  123.      */
  124.     public function getRequest()
  125.     {
  126.         return $this->_request;
  127.     }
  128.  
  129.     // }}}
  130.     // {{{ Iterator implementation
  131.  
  132.     /**
  133.      * get current result of response
  134.      *
  135.      * @return array current result of response
  136.      */
  137.     public function current()
  138.     {
  139.         return (array)$this->xml->{$this->_indexkey}[$this->_iteratorCounter];
  140.     }
  141.  
  142.     /**
  143.      * get next result of response
  144.      *
  145.      * @return array next result of response
  146.      */
  147.     public function next()
  148.     {
  149.         $this->_iteratorCounter++;
  150.         if (!isset($this->xml->{$this->_indexkey}[$this->_iteratorCounter])) {
  151.             $this->_isValidIterator = false;
  152.         }
  153.     }
  154.  
  155.     /**
  156.      * get counter for iterator
  157.      *
  158.      * @return string counter
  159.      */
  160.     public function key()
  161.     {
  162.         return $this->_iteratorCounter;
  163.     }
  164.  
  165.     /**
  166.      * clear counter for  iterator
  167.      *
  168.      * @return void 
  169.      */
  170.     public function rewind()
  171.     {
  172.         $this->_iteratorCounter = 0;
  173.     }
  174.  
  175.     /**
  176.      * get validate status
  177.      *
  178.      * @return bool validate status
  179.      */
  180.     public function valid()
  181.     {
  182.         return $this->_isValidIterator;
  183.     }
  184.  
  185.     // }}}
  186.     // {{{ private methods
  187.  
  188.     /**
  189.      * Parse XML from the response
  190.      *
  191.      * @throws Services_Yahoo_Exception
  192.      *
  193.      * @return void 
  194.      */
  195.     private function _parseRequest()
  196.     {
  197.         $this->xml simplexml_load_string($this->_request->getResponseBody());
  198.  
  199.         if ($this->xml === false{
  200.             throw new 
  201.                 Services_Yahoo_Exception("The response contained no valid XML");
  202.         }
  203.     }
  204.  
  205.     /**
  206.      * Determine if an error was returned by the Yahoo API
  207.      *
  208.      * This method evaluates the HTTP response code. If it indicates
  209.      * an error, the method returns true.
  210.      *
  211.      * @return boolean  True on error, otherwise false.
  212.      */
  213.     private function _isError()
  214.     {
  215.         return in_array($this->_request->getResponseCode(),
  216.                    array(400403404503));
  217.     }
  218.  
  219.     /**
  220.      * Get all error messages if the response contained an error
  221.      *
  222.      * Returns all errors in an numerically indexed array that were
  223.      * part of the response.
  224.      *
  225.      * @see    _isError()
  226.      * @return array 
  227.      */
  228.     private function _getMessages()
  229.     {
  230.         $returnValue = array();
  231.         foreach ($this->xml->Message as $message{
  232.             $returnValue[$message;
  233.         }
  234.         return $returnValue;
  235.     }
  236.  
  237.     /**
  238.      * Set Key for Response Loop Field
  239.      *
  240.      * @param string $key key for loop in result
  241.      *
  242.      * @return void 
  243.      */
  244.     public function setIndexKey($key)
  245.     {
  246.         return $this->_indexkey $key;
  247.     }
  248.  
  249.     /**
  250.      * get Attribute
  251.      *
  252.      * @param string $name key in result
  253.      *
  254.      * @return string value of attribute
  255.      */
  256.     public function returnAttribute($name)
  257.     {
  258.         if (isset($this->xml[$name])) {
  259.             return $this->xml[$name];
  260.         }
  261.  
  262.         return null;
  263.     }
  264.     // }}}
  265. }

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