File indexing completed on 2024-05-12 06:03:06

0001 <?php
0002 
0003 /**
0004  * Zend Framework
0005  *
0006  * LICENSE
0007  *
0008  * This source file is subject to the new BSD license that is bundled
0009  * with this package in the file LICENSE.txt.
0010  * It is also available through the world-wide-web at this URL:
0011  * http://framework.zend.com/license/new-bsd
0012  * If you did not receive a copy of the license and are unable to
0013  * obtain it through the world-wide-web, please send an email
0014  * to license@zend.com so we can send you a copy immediately.
0015  *
0016  * @category   Zend
0017  * @package    Zend_Service
0018  * @subpackage Yahoo
0019  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0020  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0021  * @version    $Id$
0022  */
0023 
0024 /** @see Zend_Xml_Security */
0025 // require_once 'Zend/Xml/Security.php';
0026 
0027 /**
0028  * @category   Zend
0029  * @package    Zend_Service
0030  * @subpackage Yahoo
0031  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0032  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0033  */
0034 class Zend_Service_Yahoo
0035 {
0036     /**
0037      * Yahoo Developer Application ID
0038      *
0039      * @var string
0040      */
0041     public $appId;
0042 
0043     /**
0044      * Reference to the REST client
0045      *
0046      * @var Zend_Rest_Client
0047      */
0048     protected $_rest;
0049 
0050 
0051     /**
0052      * Sets the application ID and instantiates the REST client
0053      *
0054      * @param  string $appId specified the developer's appid
0055      * @return void
0056      */
0057     public function __construct($appId)
0058     {
0059         $this->appId = (string) $appId;
0060         /**
0061          * @see Zend_Rest_Client
0062          */
0063         // require_once 'Zend/Rest/Client.php';
0064         $this->_rest = new Zend_Rest_Client('http://search.yahooapis.com');
0065     }
0066 
0067 
0068     /**
0069      * Retrieve Inlink Data from siteexplorer.yahoo.com.  A basic query
0070      * consists simply of a URL.  Additional options that can be
0071      * specified consist of:
0072      * 'results'      => int  How many results to return, max is 100
0073      * 'start'        => int  The start offset for search results
0074      * 'entire_site'  => bool  Data for the whole site or a single page
0075      * 'omit_inlinks' => (none|domain|subdomain)  Filter inlinks from these sources
0076      *
0077      * @param  string $query    the query being run
0078      * @param  array  $options  any optional parameters
0079      * @return Zend_Service_Yahoo_ResultSet  The return set
0080      * @throws Zend_Service_Exception
0081      */
0082     public function inlinkDataSearch($query, array $options = array())
0083     {
0084         static $defaultOptions = array('results'     => '50',
0085                                        'start'    => 1);
0086 
0087         $options = $this->_prepareOptions($query, $options, $defaultOptions);
0088         $this->_validateInlinkDataSearch($options);
0089 
0090         $this->_rest->getHttpClient()->resetParameters();
0091         $this->_rest->setUri('http://search.yahooapis.com');
0092         $response = $this->_rest->restGet('/SiteExplorerService/V1/inlinkData', $options);
0093 
0094         if ($response->isError()) {
0095             /**
0096              * @see Zend_Service_Exception
0097              */
0098             // require_once 'Zend/Service/Exception.php';
0099             throw new Zend_Service_Exception('An error occurred sending request. Status code: ' .
0100                                              $response->getStatus());
0101         }
0102 
0103         $dom = new DOMDocument();
0104         $dom = Zend_Xml_Security::scan($response->getBody(), $dom);
0105         self::_checkErrors($dom);
0106 
0107         /**
0108          * @see Zend_Service_Yahoo_InlinkDataResultSet
0109          */
0110         // require_once 'Zend/Service/Yahoo/InlinkDataResultSet.php';
0111         return new Zend_Service_Yahoo_InlinkDataResultSet($dom);
0112     }
0113 
0114 
0115     /**
0116      * Perform a search of images.  The most basic query consists simply
0117      * of a plain text search, but you can also specify the type of
0118      * image, the format, color, etc.
0119      *
0120      * The specific options are:
0121      * 'type'       => (all|any|phrase)  How to parse the query terms
0122      * 'results'    => int  How many results to return, max is 50
0123      * 'start'      => int  The start offset for search results
0124      * 'format'     => (any|bmp|gif|jpeg|png)  The type of images to search for
0125      * 'coloration' => (any|color|bw)  The coloration of images to search for
0126      * 'adult_ok'   => bool  Flag to allow 'adult' images.
0127      *
0128      * @param  string $query   the query to be run
0129      * @param  array  $options an optional array of query options
0130      * @return Zend_Service_Yahoo_ImageResultSet the search results
0131      * @throws Zend_Service_Exception
0132      */
0133     public function imageSearch($query, array $options = array())
0134     {
0135         static $defaultOptions = array('type'       => 'all',
0136                                        'results'    => 10,
0137                                        'start'      => 1,
0138                                        'format'     => 'any',
0139                                        'coloration' => 'any');
0140 
0141         $options = $this->_prepareOptions($query, $options, $defaultOptions);
0142 
0143         $this->_validateImageSearch($options);
0144 
0145         $this->_rest->getHttpClient()->resetParameters();
0146         $this->_rest->setUri('http://search.yahooapis.com');
0147         $response = $this->_rest->restGet('/ImageSearchService/V1/imageSearch', $options);
0148 
0149         if ($response->isError()) {
0150             /**
0151              * @see Zend_Service_Exception
0152              */
0153             // require_once 'Zend/Service/Exception.php';
0154             throw new Zend_Service_Exception('An error occurred sending request. Status code: ' .
0155                                              $response->getStatus());
0156         }
0157 
0158         $dom = new DOMDocument();
0159         $dom = Zend_Xml_Security::scan($response->getBody(), $dom);
0160         self::_checkErrors($dom);
0161 
0162         /**
0163          * @see Zend_Service_YahooImageResultSet
0164          */
0165         // require_once 'Zend/Service/Yahoo/ImageResultSet.php';
0166         return new Zend_Service_Yahoo_ImageResultSet($dom);
0167     }
0168 
0169 
0170     /**
0171      * Perform a search on local.yahoo.com.  The basic search
0172      * consists of a query and some fragment of location information;
0173      * for example zipcode, latitude/longitude, or street address.
0174      *
0175      * Query options include:
0176      * 'results'    => int  How many results to return, max is 50
0177      * 'start'      => int  The start offset for search results
0178      * 'sort'       => (relevance|title|distance|rating) How to order your results
0179      *
0180      * 'radius'     => float  The radius (in miles) in which to search
0181      *
0182      * 'longitude'  => float  The longitude of the location to search around
0183      * 'latitude'   => float  The latitude of the location to search around
0184      *
0185      * 'zip'        => string The zipcode to search around
0186      *
0187      * 'street'     => string  The street address to search around
0188      * 'city'       => string  The city for address search
0189      * 'state'      => string  The state for address search
0190      * 'location'   => string  An adhoc location string to search around
0191      *
0192      * @param  string $query    The query string you want to run
0193      * @param  array  $options  The search options, including location
0194      * @return Zend_Service_Yahoo_LocalResultSet The results
0195      * @throws Zend_Service_Exception
0196      */
0197     public function localSearch($query, array $options = array())
0198     {
0199         static $defaultOptions = array('results' => 10,
0200                                        'start'   => 1,
0201                                        'sort'    => 'distance',
0202                                        'radius'  => 5);
0203 
0204         $options = $this->_prepareOptions($query, $options, $defaultOptions);
0205 
0206         $this->_validateLocalSearch($options);
0207 
0208         $this->_rest->getHttpClient()->resetParameters();
0209         $this->_rest->setUri('http://local.yahooapis.com');
0210         $response = $this->_rest->restGet('/LocalSearchService/V1/localSearch', $options);
0211 
0212         if ($response->isError()) {
0213             /**
0214              * @see Zend_Service_Exception
0215              */
0216             // require_once 'Zend/Service/Exception.php';
0217             throw new Zend_Service_Exception('An error occurred sending request. Status code: ' .
0218                                              $response->getStatus());
0219         }
0220 
0221         $dom = new DOMDocument();
0222         $dom = Zend_Xml_Security::scan($response->getBody(), $dom);
0223         self::_checkErrors($dom);
0224 
0225         /**
0226          * @see Zend_Service_Yahoo_LocalResultSet
0227          */
0228         // require_once 'Zend/Service/Yahoo/LocalResultSet.php';
0229         return new Zend_Service_Yahoo_LocalResultSet($dom);
0230     }
0231 
0232 
0233     /**
0234      * Execute a search on news.yahoo.com. This method minimally takes a
0235      * text query to search on.
0236      *
0237      * Query options coonsist of:
0238      *
0239      * 'results'    => int  How many results to return, max is 50
0240      * 'start'      => int  The start offset for search results
0241      * 'sort'       => (rank|date)  How to order your results
0242      * 'language'   => lang  The target document language to match
0243      * 'type'       => (all|any|phrase)  How the query should be parsed
0244      * 'site'       => string  A site to which your search should be restricted
0245      *
0246      * @param  string $query    The query to run
0247      * @param  array  $options  The array of optional parameters
0248      * @return Zend_Service_Yahoo_NewsResultSet  The query return set
0249      * @throws Zend_Service_Exception
0250      */
0251     public function newsSearch($query, array $options = array())
0252     {
0253         static $defaultOptions = array('type'     => 'all',
0254                                        'start'    => 1,
0255                                        'sort'     => 'rank');
0256 
0257         $options = $this->_prepareOptions($query, $options, $defaultOptions);
0258 
0259         $this->_validateNewsSearch($options);
0260 
0261         $this->_rest->getHttpClient()->resetParameters();
0262         $this->_rest->setUri('http://search.yahooapis.com');
0263         $response = $this->_rest->restGet('/NewsSearchService/V1/newsSearch', $options);
0264 
0265         if ($response->isError()) {
0266             /**
0267              * @see Zend_Service_Exception
0268              */
0269             // require_once 'Zend/Service/Exception.php';
0270             throw new Zend_Service_Exception('An error occurred sending request. Status code: ' .
0271                                              $response->getStatus());
0272         }
0273 
0274         $dom = new DOMDocument();
0275         $dom = Zend_Xml_Security::scan($response->getBody(), $dom);
0276         self::_checkErrors($dom);
0277 
0278         /**
0279          * @see Zend_Service_Yahoo_NewsResultSet
0280          */
0281         // require_once 'Zend/Service/Yahoo/NewsResultSet.php';
0282         return new Zend_Service_Yahoo_NewsResultSet($dom);
0283     }
0284 
0285 
0286     /**
0287      * Retrieve Page Data from siteexplorer.yahoo.com.  A basic query
0288      * consists simply of a URL.  Additional options that can be
0289      * specified consist of:
0290      * 'results'      => int  How many results to return, max is 100
0291      * 'start'        => int  The start offset for search results
0292      * 'domain_only'  => bool  Data for just the given domain or all sub-domains also
0293      *
0294      * @param  string $query    the query being run
0295      * @param  array  $options  any optional parameters
0296      * @return Zend_Service_Yahoo_ResultSet  The return set
0297      * @throws Zend_Service_Exception
0298      */
0299     public function pageDataSearch($query, array $options = array())
0300     {
0301         static $defaultOptions = array('results'     => '50',
0302                                        'start'    => 1);
0303 
0304         $options = $this->_prepareOptions($query, $options, $defaultOptions);
0305         $this->_validatePageDataSearch($options);
0306 
0307         $this->_rest->getHttpClient()->resetParameters();
0308         $this->_rest->setUri('http://search.yahooapis.com');
0309         $response = $this->_rest->restGet('/SiteExplorerService/V1/pageData', $options);
0310 
0311         if ($response->isError()) {
0312             /**
0313              * @see Zend_Service_Exception
0314              */
0315             // require_once 'Zend/Service/Exception.php';
0316             throw new Zend_Service_Exception('An error occurred sending request. Status code: ' .
0317                                              $response->getStatus());
0318         }
0319 
0320         $dom = new DOMDocument();
0321         $dom = Zend_Xml_Security::scan($response->getBody(), $dom);
0322         self::_checkErrors($dom);
0323 
0324         /**
0325          * @see Zend_Service_Yahoo_PageDataResultSet
0326          */
0327         // require_once 'Zend/Service/Yahoo/PageDataResultSet.php';
0328         return new Zend_Service_Yahoo_PageDataResultSet($dom);
0329     }
0330 
0331 
0332     /**
0333      * Perform a search of videos.  The most basic query consists simply
0334      * of a plain text search, but you can also specify the format of
0335      * video.
0336      *
0337      * The specific options are:
0338      * 'type'       => (all|any|phrase)  How to parse the query terms
0339      * 'results'    => int  How many results to return, max is 50
0340      * 'start'      => int  The start offset for search results
0341      * 'format'     => (any|avi|flash|mpeg|msmedia|quicktime|realmedia)  The type of videos to search for
0342      * 'adult_ok'   => bool  Flag to allow 'adult' videos.
0343      *
0344      * @param  string $query   the query to be run
0345      * @param  array  $options an optional array of query options
0346      * @return Zend_Service_Yahoo_VideoResultSet the search results
0347      * @throws Zend_Service_Exception
0348      */
0349     public function videoSearch($query, array $options = array())
0350     {
0351         static $defaultOptions = array('type'       => 'all',
0352                                        'results'    => 10,
0353                                        'start'      => 1,
0354                                        'format'     => 'any');
0355 
0356         $options = $this->_prepareOptions($query, $options, $defaultOptions);
0357 
0358         $this->_validateVideoSearch($options);
0359 
0360         $this->_rest->getHttpClient()->resetParameters();
0361         $this->_rest->setUri('http://search.yahooapis.com');
0362         $response = $this->_rest->restGet('/VideoSearchService/V1/videoSearch', $options);
0363 
0364         if ($response->isError()) {
0365             /**
0366              * @see Zend_Service_Exception
0367              */
0368             // require_once 'Zend/Service/Exception.php';
0369             throw new Zend_Service_Exception('An error occurred sending request. Status code: ' .
0370                                              $response->getStatus());
0371         }
0372 
0373         $dom = new DOMDocument();
0374         $dom = Zend_Xml_Security::scan($response->getBody(), $dom);
0375         self::_checkErrors($dom);
0376 
0377         /**
0378          * @see Zend_Service_YahooVideoResultSet
0379          */
0380         // require_once 'Zend/Service/Yahoo/VideoResultSet.php';
0381         return new Zend_Service_Yahoo_VideoResultSet($dom);
0382     }
0383 
0384 
0385     /**
0386      * Perform a web content search on search.yahoo.com.  A basic query
0387      * consists simply of a text query.  Additional options that can be
0388      * specified consist of:
0389      * 'results'    => int  How many results to return, max is 50
0390      * 'start'      => int  The start offset for search results
0391      * 'language'   => lang  The target document language to match
0392      * 'type'       => (all|any|phrase)  How the query should be parsed
0393      * 'site'       => string  A site to which your search should be restricted
0394      * 'format'     => (any|html|msword|pdf|ppt|rss|txt|xls)
0395      * 'adult_ok'   => bool  permit 'adult' content in the search results
0396      * 'similar_ok' => bool  permit similar results in the result set
0397      * 'country'    => string  The country code for the content searched
0398      * 'license'    => (any|cc_any|cc_commercial|cc_modifiable)  The license of content being searched
0399      * 'region'     => The regional search engine on which the service performs the search. default us.
0400      *
0401      * @param  string $query    the query being run
0402      * @param  array  $options  any optional parameters
0403      * @return Zend_Service_Yahoo_WebResultSet  The return set
0404      * @throws Zend_Service_Exception
0405      */
0406     public function webSearch($query, array $options = array())
0407     {
0408         static $defaultOptions = array('type'     => 'all',
0409                                        'start'    => 1,
0410                                        'results'  => 10,
0411                                        'format'   => 'any');
0412 
0413         $options = $this->_prepareOptions($query, $options, $defaultOptions);
0414         $this->_validateWebSearch($options);
0415 
0416         $this->_rest->getHttpClient()->resetParameters();
0417         $this->_rest->setUri('http://search.yahooapis.com');
0418         $response = $this->_rest->restGet('/WebSearchService/V1/webSearch', $options);
0419 
0420         if ($response->isError()) {
0421             /**
0422              * @see Zend_Service_Exception
0423              */
0424             // require_once 'Zend/Service/Exception.php';
0425             throw new Zend_Service_Exception('An error occurred sending request. Status code: ' .
0426                                              $response->getStatus());
0427         }
0428 
0429         $dom = new DOMDocument();
0430         $dom = Zend_Xml_Security::scan($response->getBody(), $dom);
0431         self::_checkErrors($dom);
0432 
0433         /**
0434          * @see Zend_Service_Yahoo_WebResultSet
0435          */
0436         // require_once 'Zend/Service/Yahoo/WebResultSet.php';
0437         return new Zend_Service_Yahoo_WebResultSet($dom);
0438     }
0439 
0440 
0441     /**
0442      * Returns a reference to the REST client
0443      *
0444      * @return Zend_Rest_Client
0445      */
0446     public function getRestClient()
0447     {
0448         return $this->_rest;
0449     }
0450 
0451 
0452     /**
0453      * Validate Inlink Data Search Options
0454      *
0455      * @param  array $options
0456      * @return void
0457      * @throws Zend_Service_Exception
0458      */
0459     protected function _validateInlinkDataSearch(array $options)
0460     {
0461         $validOptions = array('appid', 'query', 'results', 'start', 'entire_site', 'omit_inlinks');
0462 
0463         $this->_compareOptions($options, $validOptions);
0464 
0465         /**
0466          * @see Zend_Validate_Between
0467          */
0468         // require_once 'Zend/Validate/Between.php';
0469         $between = new Zend_Validate_Between(1, 100, true);
0470 
0471         if (isset($options['results']) && !$between->setMin(1)->setMax(100)->isValid($options['results'])) {
0472             /**
0473              * @see Zend_Service_Exception
0474              */
0475             // require_once 'Zend/Service/Exception.php';
0476             throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
0477         }
0478 
0479         if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
0480             /**
0481              * @see Zend_Service_Exception
0482              */
0483             // require_once 'Zend/Service/Exception.php';
0484             throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
0485         }
0486 
0487         if (isset($options['omit_inlinks'])) {
0488             $this->_validateInArray('omit_inlinks', $options['omit_inlinks'], array('none', 'domain', 'subdomain'));
0489         }
0490     }
0491 
0492 
0493     /**
0494      * Validate Image Search Options
0495      *
0496      * @param  array $options
0497      * @return void
0498      * @throws Zend_Service_Exception
0499      */
0500     protected function _validateImageSearch(array $options)
0501     {
0502         $validOptions = array('appid', 'query', 'type', 'results', 'start', 'format', 'coloration', 'adult_ok');
0503 
0504         $this->_compareOptions($options, $validOptions);
0505 
0506         if (isset($options['type'])) {
0507             switch($options['type']) {
0508                 case 'all':
0509                 case 'any':
0510                 case 'phrase':
0511                     break;
0512                 default:
0513                     /**
0514                      * @see Zend_Service_Exception
0515                      */
0516                     // require_once 'Zend/Service/Exception.php';
0517                     throw new Zend_Service_Exception("Invalid value for option 'type': '{$options['type']}'");
0518             }
0519         }
0520 
0521         /**
0522          * @see Zend_Validate_Between
0523          */
0524         // require_once 'Zend/Validate/Between.php';
0525         $between = new Zend_Validate_Between(1, 50, true);
0526 
0527         if (isset($options['results']) && !$between->setMin(1)->setMax(50)->isValid($options['results'])) {
0528             /**
0529              * @see Zend_Service_Exception
0530              */
0531             // require_once 'Zend/Service/Exception.php';
0532             throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
0533         }
0534 
0535         if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
0536             /**
0537              * @see Zend_Service_Exception
0538              */
0539             // require_once 'Zend/Service/Exception.php';
0540             throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
0541         }
0542 
0543         if (isset($options['format'])) {
0544             switch ($options['format']) {
0545                 case 'any':
0546                 case 'bmp':
0547                 case 'gif':
0548                 case 'jpeg':
0549                 case 'png':
0550                     break;
0551                 default:
0552                     /**
0553                      * @see Zend_Service_Exception
0554                      */
0555                     // require_once 'Zend/Service/Exception.php';
0556                     throw new Zend_Service_Exception("Invalid value for option 'format': {$options['format']}");
0557             }
0558         }
0559 
0560         if (isset($options['coloration'])) {
0561             switch ($options['coloration']) {
0562                 case 'any':
0563                 case 'color':
0564                 case 'bw':
0565                     break;
0566                 default:
0567                     /**
0568                      * @see Zend_Service_Exception
0569                      */
0570                     // require_once 'Zend/Service/Exception.php';
0571                     throw new Zend_Service_Exception("Invalid value for option 'coloration': "
0572                                                    . "{$options['coloration']}");
0573             }
0574         }
0575     }
0576 
0577 
0578     /**
0579      * Validate Local Search Options
0580      *
0581      * @param  array $options
0582      * @return void
0583      * @throws Zend_Service_Exception
0584      */
0585     protected function _validateLocalSearch(array $options)
0586     {
0587         $validOptions = array('appid', 'query', 'results', 'start', 'sort', 'radius', 'street',
0588                               'city', 'state', 'zip', 'location', 'latitude', 'longitude');
0589 
0590         $this->_compareOptions($options, $validOptions);
0591 
0592         /**
0593          * @see Zend_Validate_Between
0594          */
0595         // require_once 'Zend/Validate/Between.php';
0596         $between = new Zend_Validate_Between(1, 20, true);
0597 
0598         if (isset($options['results']) && !$between->setMin(1)->setMax(20)->isValid($options['results'])) {
0599             /**
0600              * @see Zend_Service_Exception
0601              */
0602             // require_once 'Zend/Service/Exception.php';
0603             throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
0604         }
0605 
0606         if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
0607             /**
0608              * @see Zend_Service_Exception
0609              */
0610             // require_once 'Zend/Service/Exception.php';
0611             throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
0612         }
0613 
0614         if (isset($options['longitude']) && !$between->setMin(-90)->setMax(90)->isValid($options['longitude'])) {
0615             /**
0616              * @see Zend_Service_Exception
0617              */
0618             // require_once 'Zend/Service/Exception.php';
0619             throw new Zend_Service_Exception("Invalid value for option 'longitude': {$options['longitude']}");
0620         }
0621 
0622         if (isset($options['latitude']) && !$between->setMin(-180)->setMax(180)->isValid($options['latitude'])) {
0623             /**
0624              * @see Zend_Service_Exception
0625              */
0626             // require_once 'Zend/Service/Exception.php';
0627             throw new Zend_Service_Exception("Invalid value for option 'latitude': {$options['latitude']}");
0628         }
0629 
0630         if (isset($options['zip']) && !preg_match('/(^\d{5}$)|(^\d{5}-\d{4}$)/', $options['zip'])) {
0631             /**
0632              * @see Zend_Service_Exception
0633              */
0634             // require_once 'Zend/Service/Exception.php';
0635             throw new Zend_Service_Exception("Invalid value for option 'zip': {$options['zip']}");
0636         }
0637 
0638         $hasLocation = false;
0639         $locationFields = array('street', 'city', 'state', 'zip', 'location');
0640         foreach ($locationFields as $field) {
0641             if (isset($options[$field]) && $options[$field] != '') {
0642                 $hasLocation = true;
0643                 break;
0644             }
0645         }
0646 
0647         if (!$hasLocation && (!isset($options['latitude']) || !isset($options['longitude']))) {
0648             /**
0649              * @see Zend_Service_Exception
0650              */
0651             // require_once 'Zend/Service/Exception.php';
0652             throw new Zend_Service_Exception('Location data are required but missing');
0653         }
0654 
0655         if (!in_array($options['sort'], array('relevance', 'title', 'distance', 'rating'))) {
0656             /**
0657              * @see Zend_Service_Exception
0658              */
0659             // require_once 'Zend/Service/Exception.php';
0660             throw new Zend_Service_Exception("Invalid value for option 'sort': {$options['sort']}");
0661         }
0662     }
0663 
0664 
0665     /**
0666      * Validate News Search Options
0667      *
0668      * @param  array $options
0669      * @return void
0670      * @throws Zend_Service_Exception
0671      */
0672     protected function _validateNewsSearch(array $options)
0673     {
0674         $validOptions = array('appid', 'query', 'results', 'start', 'sort', 'language', 'type', 'site');
0675 
0676         $this->_compareOptions($options, $validOptions);
0677 
0678         /**
0679          * @see Zend_Validate_Between
0680          */
0681         // require_once 'Zend/Validate/Between.php';
0682         $between = new Zend_Validate_Between(1, 50, true);
0683 
0684         if (isset($options['results']) && !$between->setMin(1)->setMax(50)->isValid($options['results'])) {
0685             /**
0686              * @see Zend_Service_Exception
0687              */
0688             // require_once 'Zend/Service/Exception.php';
0689             throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
0690         }
0691 
0692         if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
0693             /**
0694              * @see Zend_Service_Exception
0695              */
0696             // require_once 'Zend/Service/Exception.php';
0697             throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
0698         }
0699 
0700         if (isset($options['language'])) {
0701             $this->_validateLanguage($options['language']);
0702         }
0703 
0704         $this->_validateInArray('sort', $options['sort'], array('rank', 'date'));
0705         $this->_validateInArray('type', $options['type'], array('all', 'any', 'phrase'));
0706     }
0707 
0708 
0709     /**
0710      * Validate Page Data Search Options
0711      *
0712      * @param  array $options
0713      * @return void
0714      * @throws Zend_Service_Exception
0715      */
0716     protected function _validatePageDataSearch(array $options)
0717     {
0718         $validOptions = array('appid', 'query', 'results', 'start', 'domain_only');
0719 
0720         $this->_compareOptions($options, $validOptions);
0721 
0722         /**
0723          * @see Zend_Validate_Between
0724          */
0725         // require_once 'Zend/Validate/Between.php';
0726         $between = new Zend_Validate_Between(1, 100, true);
0727 
0728         if (isset($options['results']) && !$between->setMin(1)->setMax(100)->isValid($options['results'])) {
0729             /**
0730              * @see Zend_Service_Exception
0731              */
0732             // require_once 'Zend/Service/Exception.php';
0733             throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
0734         }
0735 
0736         if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
0737             /**
0738              * @see Zend_Service_Exception
0739              */
0740             // require_once 'Zend/Service/Exception.php';
0741             throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
0742         }
0743     }
0744 
0745 
0746     /**
0747      * Validate Video Search Options
0748      *
0749      * @param  array $options
0750      * @return void
0751      * @throws Zend_Service_Exception
0752      */
0753     protected function _validateVideoSearch(array $options)
0754     {
0755         $validOptions = array('appid', 'query', 'type', 'results', 'start', 'format', 'adult_ok');
0756 
0757         $this->_compareOptions($options, $validOptions);
0758 
0759         if (isset($options['type'])) {
0760             $this->_validateInArray('type', $options['type'], array('all', 'any', 'phrase'));
0761         }
0762 
0763         /**
0764          * @see Zend_Validate_Between
0765          */
0766         // require_once 'Zend/Validate/Between.php';
0767         $between = new Zend_Validate_Between(1, 50, true);
0768 
0769         if (isset($options['results']) && !$between->setMin(1)->setMax(50)->isValid($options['results'])) {
0770             /**
0771              * @see Zend_Service_Exception
0772              */
0773             // require_once 'Zend/Service/Exception.php';
0774             throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
0775         }
0776 
0777         if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
0778             /**
0779              * @see Zend_Service_Exception
0780              */
0781             // require_once 'Zend/Service/Exception.php';
0782             throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
0783         }
0784 
0785         if (isset($options['format'])) {
0786             $this->_validateInArray('format', $options['format'], array('any', 'avi', 'flash', 'mpeg', 'msmedia', 'quicktime', 'realmedia'));
0787         }
0788     }
0789 
0790 
0791     /**
0792      * Validate Web Search Options
0793      *
0794      * @param  array $options
0795      * @return void
0796      * @throws Zend_Service_Exception
0797      */
0798     protected function _validateWebSearch(array $options)
0799     {
0800         $validOptions = array('appid', 'query', 'results', 'start', 'language', 'type', 'format', 'adult_ok',
0801                               'similar_ok', 'country', 'site', 'subscription', 'license', 'region');
0802 
0803         $this->_compareOptions($options, $validOptions);
0804 
0805         /**
0806          * @see Zend_Validate_Between
0807          */
0808         // require_once 'Zend/Validate/Between.php';
0809         $between = new Zend_Validate_Between(1, 100, true);
0810 
0811         if (isset($options['results']) && !$between->setMin(1)->setMax(100)->isValid($options['results'])) {
0812             /**
0813              * @see Zend_Service_Exception
0814              */
0815             // require_once 'Zend/Service/Exception.php';
0816             throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
0817         }
0818 
0819         if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
0820             /**
0821              * @see Zend_Service_Exception
0822              */
0823             // require_once 'Zend/Service/Exception.php';
0824             throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
0825         }
0826 
0827         if (isset($options['language'])) {
0828             $this->_validateLanguage($options['language']);
0829         }
0830 
0831         $this->_validateInArray('type', $options['type'], array('all', 'any', 'phrase'));
0832         $this->_validateInArray('format', $options['format'], array('any', 'html', 'msword', 'pdf', 'ppt', 'rss',
0833                                                                     'txt', 'xls'));
0834         if (isset($options['license'])) {
0835             $this->_validateInArray('license', $options['license'], array('any', 'cc_any', 'cc_commercial',
0836                                                                       'cc_modifiable'));
0837         }
0838 
0839         if (isset($options['region'])){
0840             $this->_validateInArray('region', $options['region'], array('ar', 'au', 'at', 'br', 'ca', 'ct', 'dk', 'fi',
0841                                                                           'fr', 'de', 'in', 'id', 'it', 'my', 'mx',
0842                                                                           'nl', 'no', 'ph', 'ru', 'sg', 'es', 'se',
0843                                                                           'ch', 'th', 'uk', 'us'));
0844         }
0845     }
0846 
0847 
0848     /**
0849      * Prepare options for sending to Yahoo!
0850      *
0851      * @param  string $query          Search Query
0852      * @param  array  $options        User specified options
0853      * @param  array  $defaultOptions Required/Default options
0854      * @return array
0855      */
0856     protected function _prepareOptions($query, array $options, array $defaultOptions = array())
0857     {
0858         $options['appid'] = $this->appId;
0859         $options['query'] = (string) $query;
0860 
0861         return array_merge($defaultOptions, $options);
0862     }
0863 
0864 
0865     /**
0866      * Throws an exception if the chosen language is not supported
0867      *
0868      * @param  string $lang Language code
0869      * @return void
0870      * @throws Zend_Service_Exception
0871      */
0872     protected function _validateLanguage($lang)
0873     {
0874         $languages = array('ar', 'bg', 'ca', 'szh', 'tzh', 'hr', 'cs', 'da', 'nl', 'en', 'et', 'fi', 'fr', 'de', 'el',
0875             'he', 'hu', 'is', 'id', 'it', 'ja', 'ko', 'lv', 'lt', 'no', 'fa', 'pl', 'pt', 'ro', 'ru', 'sk', 'sr', 'sl',
0876             'es', 'sv', 'th', 'tr'
0877             );
0878         if (!in_array($lang, $languages)) {
0879             /**
0880              * @see Zend_Service_Exception
0881              */
0882             // require_once 'Zend/Service/Exception.php';
0883             throw new Zend_Service_Exception("The selected language '$lang' is not supported");
0884         }
0885     }
0886 
0887 
0888     /**
0889      * Utility function to check for a difference between two arrays.
0890      *
0891      * @param  array $options      User specified options
0892      * @param  array $validOptions Valid options
0893      * @return void
0894      * @throws Zend_Service_Exception if difference is found (e.g., unsupported query option)
0895      */
0896     protected function _compareOptions(array $options, array $validOptions)
0897     {
0898         $difference = array_diff(array_keys($options), $validOptions);
0899         if ($difference) {
0900             /**
0901              * @see Zend_Service_Exception
0902              */
0903             // require_once 'Zend/Service/Exception.php';
0904             throw new Zend_Service_Exception('The following parameters are invalid: ' . join(', ', $difference));
0905         }
0906     }
0907 
0908 
0909     /**
0910      * Check that a named value is in the given array
0911      *
0912      * @param  string $name  Name associated with the value
0913      * @param  mixed  $value Value
0914      * @param  array  $array Array in which to check for the value
0915      * @return void
0916      * @throws Zend_Service_Exception
0917      */
0918     protected function _validateInArray($name, $value, array $array)
0919     {
0920         if (!in_array($value, $array)) {
0921             /**
0922              * @see Zend_Service_Exception
0923              */
0924             // require_once 'Zend/Service/Exception.php';
0925             throw new Zend_Service_Exception("Invalid value for option '$name': $value");
0926         }
0927     }
0928 
0929 
0930     /**
0931      * Check if response is an error
0932      *
0933      * @param  DOMDocument $dom DOM Object representing the result XML
0934      * @return void
0935      * @throws Zend_Service_Exception Thrown when the result from Yahoo! is an error
0936      */
0937     protected static function _checkErrors(DOMDocument $dom)
0938     {
0939         $xpath = new DOMXPath($dom);
0940         $xpath->registerNamespace('yapi', 'urn:yahoo:api');
0941 
0942         if ($xpath->query('//yapi:Error')->length >= 1) {
0943             $message = $xpath->query('//yapi:Error/yapi:Message/text()')->item(0)->data;
0944             /**
0945              * @see Zend_Service_Exception
0946              */
0947             // require_once 'Zend/Service/Exception.php';
0948             throw new Zend_Service_Exception($message);
0949         }
0950     }
0951 }