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 Search 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.5 2006/10/04 16:16:53 mj Exp $
  27.  * @link       http://pear.php.net/package/Services_Yahoo
  28.  */
  29.  
  30. /**
  31.  * Services_Yahoo Search Response class
  32.  *
  33.  * This class provides methods for accessing the response of a search
  34.  * 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.5 2006/10/04 16:16:53 mj Exp $
  43.  */
  44. class Services_Yahoo_Search_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.         if ($this->isError(== true{
  63.             $exception = new Services_Yahoo_Exception("Search query failed");
  64.             $exception->addErrors($this->getMessages());
  65.  
  66.             throw $exception;
  67.         }
  68.  
  69.         $this->parseRequest();
  70.     }
  71.  
  72.     // {{{ response handling
  73.  
  74.     
  75.     /**
  76.      * Get number of query matches in the database
  77.      *
  78.      * This method returns the overall number of matches in the
  79.      * database.
  80.      *
  81.      * @access public
  82.      * @return integer Number of query matches
  83.      */
  84.     public function getTotal()
  85.     {
  86.         return (int)$this->returnAttribute("totalResultsAvailable");
  87.     }
  88.  
  89.     /**
  90.      * Get the position of the first result in the overall search
  91.      *
  92.      * @access public
  93.      * @return integer Position of the first result in the overall search
  94.      */
  95.     public function getFirstResultPosition()
  96.     {
  97.         return (int)$this->returnAttribute("firstResultPosition");
  98.     }
  99.  
  100.     /**
  101.      * Get number of query matches returned from the search
  102.      *
  103.      * This may be lower than the number of results requested if
  104.      * there were fewer total results available.
  105.      *
  106.      * @access public
  107.      * @return integer Number of query matches returned
  108.      */
  109.     public function getTotalResultsReturned()
  110.     {
  111.         return (int)$this->returnAttribute("totalResultsReturned");
  112.     }
  113.  
  114.     /**
  115.      * Get the URL of a webpage containing a map graphic with all returned results plotted on it
  116.      *
  117.      * This URL is not part of all search responses, but it is used
  118.      * often enough to be part of the general response class.
  119.      *
  120.      * @access public
  121.      * @return string Map URL
  122.      */
  123.     public function getResultSetMapUrl()
  124.     {
  125.         if (isset($this->result['ResultSetMapUrl'])) {
  126.             return $this->result['ResultSetMapUrl'];
  127.         }
  128.  
  129.         return "";
  130.     }
  131.  
  132.     /**
  133.      * Determine if there are more matches than the ones that have been returned
  134.      *
  135.      * This method may be used as the criteria for displaying
  136.      * "Next" links or similar.
  137.      *
  138.      * @access public
  139.      * @return boolean True if more results are available, false otherwise.
  140.      */
  141.     public function hasMore()
  142.     {
  143.         $firstResult $this->getFirstResultPosition();
  144.         $lastResult $firstResult $this->getTotalResultsReturned(- 1;
  145.  
  146.         return ($this->lastResult $this->getTotal());
  147.     }
  148.  
  149.     /**
  150.      * Get the HTTP_Request instance that was used for the query
  151.      *
  152.      * Access to the HTTP_Request instance is useful for introspecting
  153.      * into the request details.  (E.g. for getting the HTTP response
  154.      * code.)
  155.      *
  156.      * @access public
  157.      * @return object HTTP_Request Instance of HTTP_Request
  158.      */
  159.     public function getRequest()
  160.     {
  161.         return $this->request;
  162.     }
  163.  
  164.     // }}}
  165.     // {{{ Iterator implementation
  166.  
  167.     
  168.     public function current()
  169.     {
  170.         return (array)$this->result['Result'][$this->iteratorCounter];
  171.     }
  172.  
  173.     public function next()
  174.     {
  175.         $this->iteratorCounter++;
  176.         if (!isset($this->result['Result'][$this->iteratorCounter])) {
  177.             $this->isValidIterator = false;
  178.         }
  179.     }
  180.  
  181.     public function key()
  182.     {
  183.         return $this->iteratorCounter;
  184.     }
  185.  
  186.     public function rewind()
  187.     {
  188.         $this->iteratorCounter = 0;
  189.     }
  190.  
  191.     public function valid()
  192.     {
  193.         return $this->isValidIterator;
  194.     }
  195.  
  196.     // }}}
  197.     // {{{ private methods
  198.  
  199.     
  200.     /**
  201.      * Parse result set from the response
  202.      *
  203.      * @access private
  204.      * @throws Services_Yahoo_Exception
  205.      */
  206.     private function parseRequest()
  207.     {
  208.         $tmp unserialize($this->request->getResponseBody());
  209.  
  210.         if ($tmp === false || !is_array($tmp|| !isset($tmp['ResultSet'])) {
  211.             throw new Services_Yahoo_Exception("The response did not contain a serialized array of search results");
  212.         }
  213.         
  214.         $this->result $tmp['ResultSet'];
  215.     }
  216.  
  217.     /**
  218.      * Determine if an error was returned by the Yahoo API
  219.      *
  220.      * This method evaluates the HTTP response code. If it indicates
  221.      * an error, the method returns true.
  222.      *
  223.      * @access private
  224.      * @return boolean  True on error, otherwise false.
  225.      */
  226.     private function isError()
  227.     {
  228.         return in_array($this->request->getResponseCode()array(400403404503));
  229.     }
  230.  
  231.     /**
  232.      * Get all error messages if the response contained an error
  233.      *
  234.      * Returns all errors in an numerically indexed array that were
  235.      * part of the response.
  236.      *
  237.      * @access private
  238.      * @see    isError()
  239.      * @return array 
  240.      */
  241.     private function getMessages()
  242.     {
  243.         $returnValue = array();
  244.         foreach ($this->result['Message'as $message{
  245.             $returnValue[$message;
  246.         }
  247.         return $returnValue;
  248.     }
  249.  
  250.     /**
  251.      * Attempts to get the value of a specific attribute of the top level tag
  252.      *
  253.      * @access private
  254.      * @param  string Name of the attribute
  255.      * @return mixed  A string containing the attribute value if the
  256.      *                 attribute exists. NULL otherwise.
  257.      */
  258.     private function returnAttribute($name)
  259.     {
  260.         if (isset($this->result[$name])) {
  261.             return $this->result[$name];
  262.         }
  263.  
  264.         return null;
  265.     }
  266.  
  267.     // }}}
  268. }

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