2
0

Yahoo.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service
  17. * @subpackage Yahoo
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @category Zend
  24. * @package Zend_Service
  25. * @subpackage Yahoo
  26. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Service_Yahoo
  30. {
  31. /**
  32. * Yahoo Developer Application ID
  33. *
  34. * @var string
  35. */
  36. public $appId;
  37. /**
  38. * Reference to the REST client
  39. *
  40. * @var Zend_Rest_Client
  41. */
  42. protected $_rest;
  43. /**
  44. * Sets the application ID and instantiates the REST client
  45. *
  46. * @param string $appId specified the developer's appid
  47. * @return void
  48. */
  49. public function __construct($appId)
  50. {
  51. $this->appId = (string) $appId;
  52. /**
  53. * @see Zend_Rest_Client
  54. */
  55. require_once 'Zend/Rest/Client.php';
  56. $this->_rest = new Zend_Rest_Client('http://search.yahooapis.com');
  57. }
  58. /**
  59. * Retrieve Inlink Data from siteexplorer.yahoo.com. A basic query
  60. * consists simply of a URL. Additional options that can be
  61. * specified consist of:
  62. * 'results' => int How many results to return, max is 100
  63. * 'start' => int The start offset for search results
  64. * 'entire_site' => bool Data for the whole site or a single page
  65. * 'omit_inlinks' => (none|domain|subdomain) Filter inlinks from these sources
  66. *
  67. * @param string $query the query being run
  68. * @param array $options any optional parameters
  69. * @return Zend_Service_Yahoo_ResultSet The return set
  70. * @throws Zend_Service_Exception
  71. */
  72. public function inlinkDataSearch($query, array $options = array())
  73. {
  74. static $defaultOptions = array('results' => '50',
  75. 'start' => 1);
  76. $options = $this->_prepareOptions($query, $options, $defaultOptions);
  77. $this->_validateInlinkDataSearch($options);
  78. $this->_rest->getHttpClient()->resetParameters();
  79. $this->_rest->setUri('http://search.yahooapis.com');
  80. $response = $this->_rest->restGet('/SiteExplorerService/V1/inlinkData', $options);
  81. if ($response->isError()) {
  82. /**
  83. * @see Zend_Service_Exception
  84. */
  85. require_once 'Zend/Service/Exception.php';
  86. throw new Zend_Service_Exception('An error occurred sending request. Status code: ' .
  87. $response->getStatus());
  88. }
  89. $dom = new DOMDocument();
  90. $dom->loadXML($response->getBody());
  91. self::_checkErrors($dom);
  92. /**
  93. * @see Zend_Service_Yahoo_InlinkDataResultSet
  94. */
  95. require_once 'Zend/Service/Yahoo/InlinkDataResultSet.php';
  96. return new Zend_Service_Yahoo_InlinkDataResultSet($dom);
  97. }
  98. /**
  99. * Perform a search of images. The most basic query consists simply
  100. * of a plain text search, but you can also specify the type of
  101. * image, the format, color, etc.
  102. *
  103. * The specific options are:
  104. * 'type' => (all|any|phrase) How to parse the query terms
  105. * 'results' => int How many results to return, max is 50
  106. * 'start' => int The start offset for search results
  107. * 'format' => (any|bmp|gif|jpeg|png) The type of images to search for
  108. * 'coloration' => (any|color|bw) The coloration of images to search for
  109. * 'adult_ok' => bool Flag to allow 'adult' images.
  110. *
  111. * @param string $query the query to be run
  112. * @param array $options an optional array of query options
  113. * @return Zend_Service_Yahoo_ImageResultSet the search results
  114. * @throws Zend_Service_Exception
  115. */
  116. public function imageSearch($query, array $options = array())
  117. {
  118. static $defaultOptions = array('type' => 'all',
  119. 'results' => 10,
  120. 'start' => 1,
  121. 'format' => 'any',
  122. 'coloration' => 'any');
  123. $options = $this->_prepareOptions($query, $options, $defaultOptions);
  124. $this->_validateImageSearch($options);
  125. $this->_rest->getHttpClient()->resetParameters();
  126. $this->_rest->setUri('http://search.yahooapis.com');
  127. $response = $this->_rest->restGet('/ImageSearchService/V1/imageSearch', $options);
  128. if ($response->isError()) {
  129. /**
  130. * @see Zend_Service_Exception
  131. */
  132. require_once 'Zend/Service/Exception.php';
  133. throw new Zend_Service_Exception('An error occurred sending request. Status code: ' .
  134. $response->getStatus());
  135. }
  136. $dom = new DOMDocument();
  137. $dom->loadXML($response->getBody());
  138. self::_checkErrors($dom);
  139. /**
  140. * @see Zend_Service_YahooImageResultSet
  141. */
  142. require_once 'Zend/Service/Yahoo/ImageResultSet.php';
  143. return new Zend_Service_Yahoo_ImageResultSet($dom);
  144. }
  145. /**
  146. * Perform a search on local.yahoo.com. The basic search
  147. * consists of a query and some fragment of location information;
  148. * for example zipcode, latitude/longitude, or street address.
  149. *
  150. * Query options include:
  151. * 'results' => int How many results to return, max is 50
  152. * 'start' => int The start offset for search results
  153. * 'sort' => (relevance|title|distance|rating) How to order your results
  154. *
  155. * 'radius' => float The radius (in miles) in which to search
  156. *
  157. * 'longitude' => float The longitude of the location to search around
  158. * 'latitude' => float The latitude of the location to search around
  159. *
  160. * 'zip' => string The zipcode to search around
  161. *
  162. * 'street' => string The street address to search around
  163. * 'city' => string The city for address search
  164. * 'state' => string The state for address search
  165. * 'location' => string An adhoc location string to search around
  166. *
  167. * @param string $query The query string you want to run
  168. * @param array $options The search options, including location
  169. * @return Zend_Service_Yahoo_LocalResultSet The results
  170. * @throws Zend_Service_Exception
  171. */
  172. public function localSearch($query, array $options = array())
  173. {
  174. static $defaultOptions = array('results' => 10,
  175. 'start' => 1,
  176. 'sort' => 'distance',
  177. 'radius' => 5);
  178. $options = $this->_prepareOptions($query, $options, $defaultOptions);
  179. $this->_validateLocalSearch($options);
  180. $this->_rest->getHttpClient()->resetParameters();
  181. $this->_rest->setUri('http://local.yahooapis.com');
  182. $response = $this->_rest->restGet('/LocalSearchService/V1/localSearch', $options);
  183. if ($response->isError()) {
  184. /**
  185. * @see Zend_Service_Exception
  186. */
  187. require_once 'Zend/Service/Exception.php';
  188. throw new Zend_Service_Exception('An error occurred sending request. Status code: ' .
  189. $response->getStatus());
  190. }
  191. $dom = new DOMDocument();
  192. $dom->loadXML($response->getBody());
  193. self::_checkErrors($dom);
  194. /**
  195. * @see Zend_Service_Yahoo_LocalResultSet
  196. */
  197. require_once 'Zend/Service/Yahoo/LocalResultSet.php';
  198. return new Zend_Service_Yahoo_LocalResultSet($dom);
  199. }
  200. /**
  201. * Execute a search on news.yahoo.com. This method minimally takes a
  202. * text query to search on.
  203. *
  204. * Query options coonsist of:
  205. *
  206. * 'results' => int How many results to return, max is 50
  207. * 'start' => int The start offset for search results
  208. * 'sort' => (rank|date) How to order your results
  209. * 'language' => lang The target document language to match
  210. * 'type' => (all|any|phrase) How the query should be parsed
  211. * 'site' => string A site to which your search should be restricted
  212. *
  213. * @param string $query The query to run
  214. * @param array $options The array of optional parameters
  215. * @return Zend_Service_Yahoo_NewsResultSet The query return set
  216. * @throws Zend_Service_Exception
  217. */
  218. public function newsSearch($query, array $options = array())
  219. {
  220. static $defaultOptions = array('type' => 'all',
  221. 'start' => 1,
  222. 'sort' => 'rank');
  223. $options = $this->_prepareOptions($query, $options, $defaultOptions);
  224. $this->_validateNewsSearch($options);
  225. $this->_rest->getHttpClient()->resetParameters();
  226. $this->_rest->setUri('http://search.yahooapis.com');
  227. $response = $this->_rest->restGet('/NewsSearchService/V1/newsSearch', $options);
  228. if ($response->isError()) {
  229. /**
  230. * @see Zend_Service_Exception
  231. */
  232. require_once 'Zend/Service/Exception.php';
  233. throw new Zend_Service_Exception('An error occurred sending request. Status code: ' .
  234. $response->getStatus());
  235. }
  236. $dom = new DOMDocument();
  237. $dom->loadXML($response->getBody());
  238. self::_checkErrors($dom);
  239. /**
  240. * @see Zend_Service_Yahoo_NewsResultSet
  241. */
  242. require_once 'Zend/Service/Yahoo/NewsResultSet.php';
  243. return new Zend_Service_Yahoo_NewsResultSet($dom);
  244. }
  245. /**
  246. * Retrieve Page Data from siteexplorer.yahoo.com. A basic query
  247. * consists simply of a URL. Additional options that can be
  248. * specified consist of:
  249. * 'results' => int How many results to return, max is 100
  250. * 'start' => int The start offset for search results
  251. * 'domain_only' => bool Data for just the given domain or all sub-domains also
  252. *
  253. * @param string $query the query being run
  254. * @param array $options any optional parameters
  255. * @return Zend_Service_Yahoo_ResultSet The return set
  256. * @throws Zend_Service_Exception
  257. */
  258. public function pageDataSearch($query, array $options = array())
  259. {
  260. static $defaultOptions = array('results' => '50',
  261. 'start' => 1);
  262. $options = $this->_prepareOptions($query, $options, $defaultOptions);
  263. $this->_validatePageDataSearch($options);
  264. $this->_rest->getHttpClient()->resetParameters();
  265. $this->_rest->setUri('http://search.yahooapis.com');
  266. $response = $this->_rest->restGet('/SiteExplorerService/V1/pageData', $options);
  267. if ($response->isError()) {
  268. /**
  269. * @see Zend_Service_Exception
  270. */
  271. require_once 'Zend/Service/Exception.php';
  272. throw new Zend_Service_Exception('An error occurred sending request. Status code: ' .
  273. $response->getStatus());
  274. }
  275. $dom = new DOMDocument();
  276. $dom->loadXML($response->getBody());
  277. self::_checkErrors($dom);
  278. /**
  279. * @see Zend_Service_Yahoo_PageDataResultSet
  280. */
  281. require_once 'Zend/Service/Yahoo/PageDataResultSet.php';
  282. return new Zend_Service_Yahoo_PageDataResultSet($dom);
  283. }
  284. /**
  285. * Perform a search of videos. The most basic query consists simply
  286. * of a plain text search, but you can also specify the format of
  287. * video.
  288. *
  289. * The specific options are:
  290. * 'type' => (all|any|phrase) How to parse the query terms
  291. * 'results' => int How many results to return, max is 50
  292. * 'start' => int The start offset for search results
  293. * 'format' => (any|avi|flash|mpeg|msmedia|quicktime|realmedia) The type of videos to search for
  294. * 'adult_ok' => bool Flag to allow 'adult' videos.
  295. *
  296. * @param string $query the query to be run
  297. * @param array $options an optional array of query options
  298. * @return Zend_Service_Yahoo_VideoResultSet the search results
  299. * @throws Zend_Service_Exception
  300. */
  301. public function videoSearch($query, array $options = array())
  302. {
  303. static $defaultOptions = array('type' => 'all',
  304. 'results' => 10,
  305. 'start' => 1,
  306. 'format' => 'any');
  307. $options = $this->_prepareOptions($query, $options, $defaultOptions);
  308. $this->_validateVideoSearch($options);
  309. $this->_rest->getHttpClient()->resetParameters();
  310. $this->_rest->setUri('http://search.yahooapis.com');
  311. $response = $this->_rest->restGet('/VideoSearchService/V1/videoSearch', $options);
  312. if ($response->isError()) {
  313. /**
  314. * @see Zend_Service_Exception
  315. */
  316. require_once 'Zend/Service/Exception.php';
  317. throw new Zend_Service_Exception('An error occurred sending request. Status code: ' .
  318. $response->getStatus());
  319. }
  320. $dom = new DOMDocument();
  321. $dom->loadXML($response->getBody());
  322. self::_checkErrors($dom);
  323. /**
  324. * @see Zend_Service_YahooVideoResultSet
  325. */
  326. require_once 'Zend/Service/Yahoo/VideoResultSet.php';
  327. return new Zend_Service_Yahoo_VideoResultSet($dom);
  328. }
  329. /**
  330. * Perform a web content search on search.yahoo.com. A basic query
  331. * consists simply of a text query. Additional options that can be
  332. * specified consist of:
  333. * 'results' => int How many results to return, max is 50
  334. * 'start' => int The start offset for search results
  335. * 'language' => lang The target document language to match
  336. * 'type' => (all|any|phrase) How the query should be parsed
  337. * 'site' => string A site to which your search should be restricted
  338. * 'format' => (any|html|msword|pdf|ppt|rss|txt|xls)
  339. * 'adult_ok' => bool permit 'adult' content in the search results
  340. * 'similar_ok' => bool permit similar results in the result set
  341. * 'country' => string The country code for the content searched
  342. * 'license' => (any|cc_any|cc_commercial|cc_modifiable) The license of content being searched
  343. * 'region' => The regional search engine on which the service performs the search. default us.
  344. *
  345. * @param string $query the query being run
  346. * @param array $options any optional parameters
  347. * @return Zend_Service_Yahoo_WebResultSet The return set
  348. * @throws Zend_Service_Exception
  349. */
  350. public function webSearch($query, array $options = array())
  351. {
  352. static $defaultOptions = array('type' => 'all',
  353. 'start' => 1,
  354. 'license' => 'any',
  355. 'results' => 10,
  356. 'format' => 'any');
  357. $options = $this->_prepareOptions($query, $options, $defaultOptions);
  358. $this->_validateWebSearch($options);
  359. $this->_rest->getHttpClient()->resetParameters();
  360. $this->_rest->setUri('http://search.yahooapis.com');
  361. $response = $this->_rest->restGet('/WebSearchService/V1/webSearch', $options);
  362. if ($response->isError()) {
  363. /**
  364. * @see Zend_Service_Exception
  365. */
  366. require_once 'Zend/Service/Exception.php';
  367. throw new Zend_Service_Exception('An error occurred sending request. Status code: ' .
  368. $response->getStatus());
  369. }
  370. $dom = new DOMDocument();
  371. $dom->loadXML($response->getBody());
  372. self::_checkErrors($dom);
  373. /**
  374. * @see Zend_Service_Yahoo_WebResultSet
  375. */
  376. require_once 'Zend/Service/Yahoo/WebResultSet.php';
  377. return new Zend_Service_Yahoo_WebResultSet($dom);
  378. }
  379. /**
  380. * Returns a reference to the REST client
  381. *
  382. * @return Zend_Rest_Client
  383. */
  384. public function getRestClient()
  385. {
  386. return $this->_rest;
  387. }
  388. /**
  389. * Validate Inlink Data Search Options
  390. *
  391. * @param array $options
  392. * @return void
  393. * @throws Zend_Service_Exception
  394. */
  395. protected function _validateInlinkDataSearch(array $options)
  396. {
  397. $validOptions = array('appid', 'query', 'results', 'start', 'entire_site', 'omit_inlinks');
  398. $this->_compareOptions($options, $validOptions);
  399. /**
  400. * @see Zend_Validate_Between
  401. */
  402. require_once 'Zend/Validate/Between.php';
  403. $between = new Zend_Validate_Between(1, 100, true);
  404. if (isset($options['results']) && !$between->setMin(1)->setMax(100)->isValid($options['results'])) {
  405. /**
  406. * @see Zend_Service_Exception
  407. */
  408. require_once 'Zend/Service/Exception.php';
  409. throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
  410. }
  411. if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
  412. /**
  413. * @see Zend_Service_Exception
  414. */
  415. require_once 'Zend/Service/Exception.php';
  416. throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
  417. }
  418. if (isset($options['omit_inlinks'])) {
  419. $this->_validateInArray('omit_inlinks', $options['omit_inlinks'], array('none', 'domain', 'subdomain'));
  420. }
  421. }
  422. /**
  423. * Validate Image Search Options
  424. *
  425. * @param array $options
  426. * @return void
  427. * @throws Zend_Service_Exception
  428. */
  429. protected function _validateImageSearch(array $options)
  430. {
  431. $validOptions = array('appid', 'query', 'type', 'results', 'start', 'format', 'coloration', 'adult_ok');
  432. $this->_compareOptions($options, $validOptions);
  433. if (isset($options['type'])) {
  434. switch($options['type']) {
  435. case 'all':
  436. case 'any':
  437. case 'phrase':
  438. break;
  439. default:
  440. /**
  441. * @see Zend_Service_Exception
  442. */
  443. require_once 'Zend/Service/Exception.php';
  444. throw new Zend_Service_Exception("Invalid value for option 'type': '{$options['type']}'");
  445. }
  446. }
  447. /**
  448. * @see Zend_Validate_Between
  449. */
  450. require_once 'Zend/Validate/Between.php';
  451. $between = new Zend_Validate_Between(1, 50, true);
  452. if (isset($options['results']) && !$between->setMin(1)->setMax(50)->isValid($options['results'])) {
  453. /**
  454. * @see Zend_Service_Exception
  455. */
  456. require_once 'Zend/Service/Exception.php';
  457. throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
  458. }
  459. if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
  460. /**
  461. * @see Zend_Service_Exception
  462. */
  463. require_once 'Zend/Service/Exception.php';
  464. throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
  465. }
  466. if (isset($options['format'])) {
  467. switch ($options['format']) {
  468. case 'any':
  469. case 'bmp':
  470. case 'gif':
  471. case 'jpeg':
  472. case 'png':
  473. break;
  474. default:
  475. /**
  476. * @see Zend_Service_Exception
  477. */
  478. require_once 'Zend/Service/Exception.php';
  479. throw new Zend_Service_Exception("Invalid value for option 'format': {$options['format']}");
  480. }
  481. }
  482. if (isset($options['coloration'])) {
  483. switch ($options['coloration']) {
  484. case 'any':
  485. case 'color':
  486. case 'bw':
  487. break;
  488. default:
  489. /**
  490. * @see Zend_Service_Exception
  491. */
  492. require_once 'Zend/Service/Exception.php';
  493. throw new Zend_Service_Exception("Invalid value for option 'coloration': "
  494. . "{$options['coloration']}");
  495. }
  496. }
  497. }
  498. /**
  499. * Validate Local Search Options
  500. *
  501. * @param array $options
  502. * @return void
  503. * @throws Zend_Service_Exception
  504. */
  505. protected function _validateLocalSearch(array $options)
  506. {
  507. $validOptions = array('appid', 'query', 'results', 'start', 'sort', 'radius', 'street',
  508. 'city', 'state', 'zip', 'location', 'latitude', 'longitude');
  509. $this->_compareOptions($options, $validOptions);
  510. /**
  511. * @see Zend_Validate_Between
  512. */
  513. require_once 'Zend/Validate/Between.php';
  514. $between = new Zend_Validate_Between(1, 20, true);
  515. if (isset($options['results']) && !$between->setMin(1)->setMax(20)->isValid($options['results'])) {
  516. /**
  517. * @see Zend_Service_Exception
  518. */
  519. require_once 'Zend/Service/Exception.php';
  520. throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
  521. }
  522. if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
  523. /**
  524. * @see Zend_Service_Exception
  525. */
  526. require_once 'Zend/Service/Exception.php';
  527. throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
  528. }
  529. if (isset($options['longitude']) && !$between->setMin(-90)->setMax(90)->isValid($options['longitude'])) {
  530. /**
  531. * @see Zend_Service_Exception
  532. */
  533. require_once 'Zend/Service/Exception.php';
  534. throw new Zend_Service_Exception("Invalid value for option 'longitude': {$options['longitude']}");
  535. }
  536. if (isset($options['latitude']) && !$between->setMin(-180)->setMax(180)->isValid($options['latitude'])) {
  537. /**
  538. * @see Zend_Service_Exception
  539. */
  540. require_once 'Zend/Service/Exception.php';
  541. throw new Zend_Service_Exception("Invalid value for option 'latitude': {$options['latitude']}");
  542. }
  543. if (isset($options['zip']) && !preg_match('/(^\d{5}$)|(^\d{5}-\d{4}$)/', $options['zip'])) {
  544. /**
  545. * @see Zend_Service_Exception
  546. */
  547. require_once 'Zend/Service/Exception.php';
  548. throw new Zend_Service_Exception("Invalid value for option 'zip': {$options['zip']}");
  549. }
  550. $hasLocation = false;
  551. $locationFields = array('street', 'city', 'state', 'zip', 'location');
  552. foreach ($locationFields as $field) {
  553. if (isset($options[$field]) && $options[$field] != '') {
  554. $hasLocation = true;
  555. break;
  556. }
  557. }
  558. if (!$hasLocation && (!isset($options['latitude']) || !isset($options['longitude']))) {
  559. /**
  560. * @see Zend_Service_Exception
  561. */
  562. require_once 'Zend/Service/Exception.php';
  563. throw new Zend_Service_Exception('Location data are required but missing');
  564. }
  565. if (!in_array($options['sort'], array('relevance', 'title', 'distance', 'rating'))) {
  566. /**
  567. * @see Zend_Service_Exception
  568. */
  569. require_once 'Zend/Service/Exception.php';
  570. throw new Zend_Service_Exception("Invalid value for option 'sort': {$options['sort']}");
  571. }
  572. }
  573. /**
  574. * Validate News Search Options
  575. *
  576. * @param array $options
  577. * @return void
  578. * @throws Zend_Service_Exception
  579. */
  580. protected function _validateNewsSearch(array $options)
  581. {
  582. $validOptions = array('appid', 'query', 'results', 'start', 'sort', 'language', 'type', 'site');
  583. $this->_compareOptions($options, $validOptions);
  584. /**
  585. * @see Zend_Validate_Between
  586. */
  587. require_once 'Zend/Validate/Between.php';
  588. $between = new Zend_Validate_Between(1, 50, true);
  589. if (isset($options['results']) && !$between->setMin(1)->setMax(50)->isValid($options['results'])) {
  590. /**
  591. * @see Zend_Service_Exception
  592. */
  593. require_once 'Zend/Service/Exception.php';
  594. throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
  595. }
  596. if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
  597. /**
  598. * @see Zend_Service_Exception
  599. */
  600. require_once 'Zend/Service/Exception.php';
  601. throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
  602. }
  603. if (isset($options['language'])) {
  604. $this->_validateLanguage($options['language']);
  605. }
  606. $this->_validateInArray('sort', $options['sort'], array('rank', 'date'));
  607. $this->_validateInArray('type', $options['type'], array('all', 'any', 'phrase'));
  608. }
  609. /**
  610. * Validate Page Data Search Options
  611. *
  612. * @param array $options
  613. * @return void
  614. * @throws Zend_Service_Exception
  615. */
  616. protected function _validatePageDataSearch(array $options)
  617. {
  618. $validOptions = array('appid', 'query', 'results', 'start', 'domain_only');
  619. $this->_compareOptions($options, $validOptions);
  620. /**
  621. * @see Zend_Validate_Between
  622. */
  623. require_once 'Zend/Validate/Between.php';
  624. $between = new Zend_Validate_Between(1, 100, true);
  625. if (isset($options['results']) && !$between->setMin(1)->setMax(100)->isValid($options['results'])) {
  626. /**
  627. * @see Zend_Service_Exception
  628. */
  629. require_once 'Zend/Service/Exception.php';
  630. throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
  631. }
  632. if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
  633. /**
  634. * @see Zend_Service_Exception
  635. */
  636. require_once 'Zend/Service/Exception.php';
  637. throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
  638. }
  639. }
  640. /**
  641. * Validate Video Search Options
  642. *
  643. * @param array $options
  644. * @return void
  645. * @throws Zend_Service_Exception
  646. */
  647. protected function _validateVideoSearch(array $options)
  648. {
  649. $validOptions = array('appid', 'query', 'type', 'results', 'start', 'format', 'adult_ok');
  650. $this->_compareOptions($options, $validOptions);
  651. if (isset($options['type'])) {
  652. $this->_validateInArray('type', $options['type'], array('all', 'any', 'phrase'));
  653. }
  654. /**
  655. * @see Zend_Validate_Between
  656. */
  657. require_once 'Zend/Validate/Between.php';
  658. $between = new Zend_Validate_Between(1, 50, true);
  659. if (isset($options['results']) && !$between->setMin(1)->setMax(50)->isValid($options['results'])) {
  660. /**
  661. * @see Zend_Service_Exception
  662. */
  663. require_once 'Zend/Service/Exception.php';
  664. throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
  665. }
  666. if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
  667. /**
  668. * @see Zend_Service_Exception
  669. */
  670. require_once 'Zend/Service/Exception.php';
  671. throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
  672. }
  673. if (isset($options['format'])) {
  674. $this->_validateInArray('format', $options['format'], array('any', 'avi', 'flash', 'mpeg', 'msmedia', 'quicktime', 'realmedia'));
  675. }
  676. }
  677. /**
  678. * Validate Web Search Options
  679. *
  680. * @param array $options
  681. * @return void
  682. * @throws Zend_Service_Exception
  683. */
  684. protected function _validateWebSearch(array $options)
  685. {
  686. $validOptions = array('appid', 'query', 'results', 'start', 'language', 'type', 'format', 'adult_ok',
  687. 'similar_ok', 'country', 'site', 'subscription', 'license', 'region');
  688. $this->_compareOptions($options, $validOptions);
  689. /**
  690. * @see Zend_Validate_Between
  691. */
  692. require_once 'Zend/Validate/Between.php';
  693. $between = new Zend_Validate_Between(1, 100, true);
  694. if (isset($options['results']) && !$between->setMin(1)->setMax(100)->isValid($options['results'])) {
  695. /**
  696. * @see Zend_Service_Exception
  697. */
  698. require_once 'Zend/Service/Exception.php';
  699. throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
  700. }
  701. if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
  702. /**
  703. * @see Zend_Service_Exception
  704. */
  705. require_once 'Zend/Service/Exception.php';
  706. throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
  707. }
  708. if (isset($options['language'])) {
  709. $this->_validateLanguage($options['language']);
  710. }
  711. $this->_validateInArray('type', $options['type'], array('all', 'any', 'phrase'));
  712. $this->_validateInArray('format', $options['format'], array('any', 'html', 'msword', 'pdf', 'ppt', 'rss',
  713. 'txt', 'xls'));
  714. $this->_validateInArray('license', $options['license'], array('any', 'cc_any', 'cc_commercial',
  715. 'cc_modifiable'));
  716. if (isset($options['region'])){
  717. $this->_validateInArray('region', $options['region'], array('ar', 'au', 'at', 'br', 'ca', 'ct', 'dk', 'fi',
  718. 'fr', 'de', 'in', 'id', 'it', 'my', 'mx',
  719. 'nl', 'no', 'ph', 'ru', 'sg', 'es', 'se',
  720. 'ch', 'th', 'uk', 'us'));
  721. }
  722. }
  723. /**
  724. * Prepare options for sending to Yahoo!
  725. *
  726. * @param string $query Search Query
  727. * @param array $options User specified options
  728. * @param array $defaultOptions Required/Default options
  729. * @return array
  730. */
  731. protected function _prepareOptions($query, array $options, array $defaultOptions = array())
  732. {
  733. $options['appid'] = $this->appId;
  734. $options['query'] = (string) $query;
  735. return array_merge($defaultOptions, $options);
  736. }
  737. /**
  738. * Throws an exception if the chosen language is not supported
  739. *
  740. * @param string $lang Language code
  741. * @return void
  742. * @throws Zend_Service_Exception
  743. */
  744. protected function _validateLanguage($lang)
  745. {
  746. $languages = array('ar', 'bg', 'ca', 'szh', 'tzh', 'hr', 'cs', 'da', 'nl', 'en', 'et', 'fi', 'fr', 'de', 'el',
  747. 'he', 'hu', 'is', 'id', 'it', 'ja', 'ko', 'lv', 'lt', 'no', 'fa', 'pl', 'pt', 'ro', 'ru', 'sk', 'sr', 'sl',
  748. 'es', 'sv', 'th', 'tr'
  749. );
  750. if (!in_array($lang, $languages)) {
  751. /**
  752. * @see Zend_Service_Exception
  753. */
  754. require_once 'Zend/Service/Exception.php';
  755. throw new Zend_Service_Exception("The selected language '$lang' is not supported");
  756. }
  757. }
  758. /**
  759. * Utility function to check for a difference between two arrays.
  760. *
  761. * @param array $options User specified options
  762. * @param array $validOptions Valid options
  763. * @return void
  764. * @throws Zend_Service_Exception if difference is found (e.g., unsupported query option)
  765. */
  766. protected function _compareOptions(array $options, array $validOptions)
  767. {
  768. $difference = array_diff(array_keys($options), $validOptions);
  769. if ($difference) {
  770. /**
  771. * @see Zend_Service_Exception
  772. */
  773. require_once 'Zend/Service/Exception.php';
  774. throw new Zend_Service_Exception('The following parameters are invalid: ' . join(', ', $difference));
  775. }
  776. }
  777. /**
  778. * Check that a named value is in the given array
  779. *
  780. * @param string $name Name associated with the value
  781. * @param mixed $value Value
  782. * @param array $array Array in which to check for the value
  783. * @return void
  784. * @throws Zend_Service_Exception
  785. */
  786. protected function _validateInArray($name, $value, array $array)
  787. {
  788. if (!in_array($value, $array)) {
  789. /**
  790. * @see Zend_Service_Exception
  791. */
  792. require_once 'Zend/Service/Exception.php';
  793. throw new Zend_Service_Exception("Invalid value for option '$name': $value");
  794. }
  795. }
  796. /**
  797. * Check if response is an error
  798. *
  799. * @param DOMDocument $dom DOM Object representing the result XML
  800. * @return void
  801. * @throws Zend_Service_Exception Thrown when the result from Yahoo! is an error
  802. */
  803. protected static function _checkErrors(DOMDocument $dom)
  804. {
  805. $xpath = new DOMXPath($dom);
  806. $xpath->registerNamespace('yapi', 'urn:yahoo:api');
  807. if ($xpath->query('//yapi:Error')->length >= 1) {
  808. $message = $xpath->query('//yapi:Error/yapi:Message/text()')->item(0)->data;
  809. /**
  810. * @see Zend_Service_Exception
  811. */
  812. require_once 'Zend/Service/Exception.php';
  813. throw new Zend_Service_Exception($message);
  814. }
  815. }
  816. }