Services_Yahoo
[ class tree: Services_Yahoo ] [ index: Services_Yahoo ] [ 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 Content Analysis Response
  6.  *
  7.  * Copyright 2005-2006 Martin Jansen
  8.  *
  9.  * Licensed under the Apache License, Version 2.0 (the "License");
  10.  * you may not use this file except in compliance with the License.
  11.  * You may obtain a copy of the License at
  12.  *
  13.  *     http://www.apache.org/licenses/LICENSE-2.0
  14.  *
  15.  * Unless required by applicable law or agreed to in writing, software
  16.  * distributed under the License is distributed on an "AS IS" BASIS,
  17.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18.  * See the License for the specific language governing permissions and
  19.  * limitations under the License.
  20.  *
  21.  * @category   Services
  22.  * @package    Services_Yahoo
  23.  * @author     Martin Jansen <mj@php.net>
  24.  * @copyright  2005-2006 Martin Jansen
  25.  * @license    http://www.apache.org/licenses/LICENSE-2.0  Apache License, Version 2.0
  26.  * @version    CVS: $Id: Response.php,v 1.2 2006/10/02 12:53:33 mj Exp $
  27.  * @link       http://pear.php.net/package/Services_Yahoo
  28.  */
  29.  
  30. /**
  31.  * Services_Yahoo Content Analysis Response class
  32.  *
  33.  * This class provides methods for accessing the response of a content
  34.  * analysis request.
  35.  *
  36.  * @category   Services
  37.  * @package    Services_Yahoo
  38.  * @extends    Exception
  39.  * @author     Martin Jansen <mj@php.net>
  40.  * @copyright  2005-2006 Martin Jansen
  41.  * @license    http://www.apache.org/licenses/LICENSE-2.0  Apache License, Version 2.0
  42.  * @version    CVS: $Id: Response.php,v 1.2 2006/10/02 12:53:33 mj Exp $
  43.  */
  44. class Services_Yahoo_ContentAnalysis_Response implements Iterator {
  45.  
  46.     private $isValidIterator = true;
  47.     private $iteratorCounter = 0;
  48.  
  49.     private $request;
  50.     private $results = array();
  51.  
  52.     /**
  53.      * Constructor
  54.      *
  55.      * @param  object HTTP_Request Instance of HTTP_Request that was used for the request
  56.      * @throws Services_Yahoo_Exception
  57.      */
  58.     public function __construct(HTTP_Request $request)
  59.     {
  60.         $this->request $request;
  61.  
  62.         $this->parseRequest();
  63.         
  64.         if ($this->isError(== true{
  65.             $exception = new Services_Yahoo_Exception("Content analysis failed");
  66.             $exception->addErrors($this->getMessages());
  67.  
  68.             throw $exception;
  69.         }
  70.     }
  71.  
  72.     // {{{ response handling
  73.  
  74.     
  75.     /**
  76.      * Get number of result sets returned by the content analysis
  77.      *
  78.      * @access public
  79.      * @return integer Number of result sets returned
  80.      */
  81.     public function getTotalResultsReturned()
  82.     {
  83.         return count((array)$this->xml->Result);
  84.     }
  85.  
  86.     /**
  87.      * Get the HTTP_Request instance that was used for the query
  88.      *
  89.      * Access to the HTTP_Request instance is useful for introspecting
  90.      * into the request details.  (E.g. for getting the HTTP response
  91.      * code.)
  92.      *
  93.      * @access public
  94.      * @return object HTTP_Request Instance of HTTP_Request
  95.      */
  96.     public function getRequest()
  97.     {
  98.         return $this->request;
  99.     }
  100.  
  101.     // }}}
  102.     // {{{ Iterator implementation
  103.  
  104.     
  105.     public function current()
  106.     {
  107.         return (string)$this->xml->Result[$this->iteratorCounter];
  108.     }
  109.  
  110.     public function next()
  111.     {
  112.         $this->iteratorCounter++;
  113.         if (!isset($this->xml->Result[$this->iteratorCounter])) {
  114.             $this->isValidIterator = false;
  115.         }
  116.     }
  117.  
  118.     public function key()
  119.     {
  120.         return $this->iteratorCounter;
  121.     }
  122.  
  123.     public function rewind()
  124.     {
  125.         $this->iteratorCounter = 0;
  126.     }
  127.  
  128.     public function valid()
  129.     {
  130.         return $this->isValidIterator;
  131.     }
  132.  
  133.     // }}}
  134.     // {{{ private methods
  135.  
  136.     
  137.     /**
  138.      * Parse XML from the response
  139.      *
  140.      * @access private
  141.      * @throws Services_Yahoo_Exception
  142.      */
  143.     private function parseRequest()
  144.     {
  145.         $this->xml simplexml_load_string($this->request->getResponseBody());
  146.  
  147.         if ($this->xml === false{
  148.             throw new Services_Yahoo_Exception("The response contained no valid XML");
  149.         }
  150.     }
  151.  
  152.     /**
  153.      * Determine if an error was returned by the Yahoo API
  154.      *
  155.      * This method evaluates the HTTP response code. If it indicates
  156.      * an error, the method returns true.
  157.      *
  158.      * @access private
  159.      * @return boolean  True on error, otherwise false.
  160.      */
  161.     private function isError()
  162.     {
  163.         return in_array($this->request->getResponseCode()array(400403404503));
  164.     }
  165.  
  166.     /**
  167.      * Get all error messages if the response contained an error
  168.      *
  169.      * Returns all errors in an numerically indexed array that were
  170.      * part of the response.
  171.      *
  172.      * @access private
  173.      * @see    isError()
  174.      * @return array 
  175.      */
  176.     private function getMessages()
  177.     {
  178.         $returnValue = array();
  179.         foreach ($this->xml->Message as $message{
  180.             $returnValue[$message;
  181.         }
  182.         return $returnValue;
  183.     }
  184.  
  185.     // }}}
  186. }

Documentation generated on Fri, 20 Apr 2007 14:30:07 -0400 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.