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

Source for file AbstractSearch.php

Documentation is available at AbstractSearch.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Abstract search class
  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: AbstractSearch.php,v 1.11 2006/10/04 13:30:31 mj Exp $
  27.  * @link       http://pear.php.net/package/Services_Yahoo
  28.  */
  29.  
  30. require_once "Services/Yahoo/Search/Response.php";
  31. require_once "HTTP/Request.php";
  32.  
  33. /**
  34.  * Abstract search class
  35.  *
  36.  * This abstract class serves as the base class for all different
  37.  * types of searches that available through Services_Yahoo.
  38.  *
  39.  * @category   Services
  40.  * @package    Services_Yahoo
  41.  * @author     Martin Jansen <mj@php.net>
  42.  * @copyright  2005-2006 Martin Jansen
  43.  * @license    http://www.apache.org/licenses/LICENSE-2.0  Apache License, Version 2.0
  44.  * @version    CVS: $Id: AbstractSearch.php,v 1.11 2006/10/04 13:30:31 mj Exp $
  45.  * @link       http://pear.php.net/package/Services_Yahoo
  46.  */
  47.  
  48.     protected $parameters = array("appid" => "PEAR_Services_Yahoo""output" => "php")
  49.  
  50.     /**
  51.      * Submits the search
  52.      *
  53.      * This method submits the search and handles the response.  It
  54.      * returns an instance of Services_Yahoo_Result which may be used
  55.      * to further make use of the result.
  56.      *
  57.      * @access public
  58.      * @return object Services_Yahoo_Response Search result
  59.      * @throws Services_Yahoo_Exception
  60.      */
  61.     public function searchFor($query)
  62.     {
  63.         $this->parameters['query'$query;
  64.  
  65.         $url $this->requestURL "?";
  66.  
  67.         foreach ($this->parameters as $key => $value{
  68.             if (is_array($value)) {
  69.                 foreach ($value as $value2{
  70.                     $url .= $key "=" urlencode($value2"&";
  71.                 }
  72.                 continue;
  73.             }
  74.  
  75.             $url .= $key "=" urlencode($value"&";
  76.         }
  77.  
  78.         $request = new HTTP_Request($url);
  79.  
  80.         $result $request->sendRequest();
  81.         if (PEAR::isError($result)) {
  82.             throw new Services_Yahoo_Exception($result->getMessage());
  83.         }
  84.  
  85.         return new Services_Yahoo_Search_Response($request);
  86.     }
  87.  
  88.     /**
  89.      * Set Application ID for the search
  90.      *
  91.      * An Application ID is a string that uniquely identifies your
  92.      * application. Think of it as like a User-Agent string. If you
  93.      * have multiple applications, you should use a different ID for
  94.      * each one. You can register your ID and make sure nobody is
  95.      * already using your ID on Yahoo's Application ID registration
  96.      * page.
  97.      *
  98.      * The ID defaults to "PEAR_Services_Yahoo", but you are free to
  99.      * change it to whatever you want.  Please note that the access
  100.      * to the Yahoo API is not limited via the Application ID but via
  101.      * the IP address of the host where the package is used.
  102.      *
  103.      * @link   http://api.search.yahoo.com/webservices/register_application
  104.      * @link   http://developer.yahoo.net/documentation/rate.html
  105.      * @access public
  106.      * @param  string Application ID
  107.      * @return Services_Yahoo_AbstractSearch Object which contains the method
  108.      */
  109.     public function withAppID($id)
  110.     {
  111.         $this->parameters['appid'$id;
  112.  
  113.         return $this;
  114.     }
  115.  
  116.     /**
  117.      * Set the kind of search to submit
  118.      *
  119.      * The allowed values of the parameter depend on the search
  120.      * type. If unsure, please consult Yahoo's documentation at
  121.      * http://developer.yahoo.net/.
  122.      *
  123.      * Even if not all searches support this parameter, it is common
  124.      * enough to be part of the abstract base class.
  125.      *
  126.      * @access public
  127.      * @param  string Kind of search
  128.      * @return Services_Yahoo_AbstractSearch Object which contains the method
  129.      */
  130.     public function withType($type)
  131.     {
  132.         $this->parameters['type'$type;
  133.  
  134.         return $this;
  135.     }
  136.  
  137.     /**
  138.      * Set the number of results to return.
  139.      *
  140.      * Even if not all searches support this parameter, it is common
  141.      * enough to be part of the abstract base class.
  142.      *
  143.      * @access public
  144.      * @param  int Number of results
  145.      * @return Services_Yahoo_AbstractSearch Object which contains the method
  146.      */
  147.     public function withResults($count)
  148.     {
  149.         $count = (int)$count;
  150.         if ($count > 50 || $count < 0{
  151.             $count = 10;
  152.         }
  153.         $this->parameters['results'$count;
  154.  
  155.         return $this;
  156.     }
  157.  
  158.     /**
  159.      * Set the starting result position to return (1-based)
  160.      *
  161.      * Even if not all searches support this parameter, it is common
  162.      * enough to be part of the abstract base class.
  163.      * 
  164.      * @access public
  165.      * @param  int Starting position
  166.      * @return Services_Yahoo_AbstractSearch Object which contains the method
  167.      */
  168.     public function startingAt($start)
  169.     {
  170.         $this->parameters['start'$start;
  171.  
  172.         return $this;
  173.     }
  174.  
  175.     /**
  176.      * Set the format to search for
  177.      *
  178.      * The allowed values of the parameter depend on the search
  179.      * type. If unsure, please consult Yahoo's documentation at
  180.      * http://developer.yahoo.net/.
  181.      *
  182.      * Even if not all searches support this parameter, it is common
  183.      * enough to be part of the abstract base class.
  184.      *
  185.      * @access public
  186.      * @param  string Format of search
  187.      * @return Services_Yahoo_AbstractSearch Object which contains the method
  188.      */
  189.     public function inFormat($format)
  190.     {
  191.         $this->parameters['format'$format;
  192.  
  193.         return $this;
  194.     }
  195.  
  196.     /**
  197.      * Set that the results will include adult content
  198.      *
  199.      * Even if not all searches support this parameter, it is common
  200.      * enough to be part of the abstract base class.
  201.      *
  202.      * @access public
  203.      * @return Services_Yahoo_AbstractSearch Object which contains the method
  204.      */
  205.     public function adultContentOK()
  206.     {
  207.         $this->parameters['adult_ok'= 1;
  208.  
  209.         return $this;
  210.     }
  211. }

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