Yahoo.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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-2012 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-2012 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. 'results' => 10,
  355. 'format' => 'any');
  356. $options = $this->_prepareOptions($query, $options, $defaultOptions);
  357. $this->_validateWebSearch($options);
  358. $this->_rest->getHttpClient()->resetParameters();
  359. $this->_rest->setUri('http://search.yahooapis.com');
  360. $response = $this->_rest->restGet('/WebSearchService/V1/webSearch', $options);
  361. if ($response->isError()) {
  362. /**
  363. * @see Zend_Service_Exception
  364. */
  365. require_once 'Zend/Service/Exception.php';
  366. throw new Zend_Service_Exception('An error occurred sending request. Status code: ' .
  367. $response->getStatus());
  368. }
  369. $dom = new DOMDocument();
  370. $dom->loadXML($response->getBody());
  371. self::_checkErrors($dom);
  372. /**
  373. * @see Zend_Service_Yahoo_WebResultSet
  374. */
  375. require_once 'Zend/Service/Yahoo/WebResultSet.php';
  376. return new Zend_Service_Yahoo_WebResultSet($dom);
  377. }
  378. /**
  379. * Returns a reference to the REST client
  380. *
  381. * @return Zend_Rest_Client
  382. */
  383. public function getRestClient()
  384. {
  385. return $this->_rest;
  386. }
  387. /**
  388. * Validate Inlink Data Search Options
  389. *
  390. * @param array $options
  391. * @return void
  392. * @throws Zend_Service_Exception
  393. */
  394. protected function _validateInlinkDataSearch(array $options)
  395. {
  396. $validOptions = array('appid', 'query', 'results', 'start', 'entire_site', 'omit_inlinks');
  397. $this->_compareOptions($options, $validOptions);
  398. /**
  399. * @see Zend_Validate_Between
  400. */
  401. require_once 'Zend/Validate/Between.php';
  402. $between = new Zend_Validate_Between(1, 100, true);
  403. if (isset($options['results']) && !$between->setMin(1)->setMax(100)->isValid($options['results'])) {
  404. /**
  405. * @see Zend_Service_Exception
  406. */
  407. require_once 'Zend/Service/Exception.php';
  408. throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
  409. }
  410. if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
  411. /**
  412. * @see Zend_Service_Exception
  413. */
  414. require_once 'Zend/Service/Exception.php';
  415. throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
  416. }
  417. if (isset($options['omit_inlinks'])) {
  418. $this->_validateInArray('omit_inlinks', $options['omit_inlinks'], array('none', 'domain', 'subdomain'));
  419. }
  420. }
  421. /**
  422. * Validate Image Search Options
  423. *
  424. * @param array $options
  425. * @return void
  426. * @throws Zend_Service_Exception
  427. */
  428. protected function _validateImageSearch(array $options)
  429. {
  430. $validOptions = array('appid', 'query', 'type', 'results', 'start', 'format', 'coloration', 'adult_ok');
  431. $this->_compareOptions($options, $validOptions);
  432. if (isset($options['type'])) {
  433. switch($options['type']) {
  434. case 'all':
  435. case 'any':
  436. case 'phrase':
  437. break;
  438. default:
  439. /**
  440. * @see Zend_Service_Exception
  441. */
  442. require_once 'Zend/Service/Exception.php';
  443. throw new Zend_Service_Exception("Invalid value for option 'type': '{$options['type']}'");
  444. }
  445. }
  446. /**
  447. * @see Zend_Validate_Between
  448. */
  449. require_once 'Zend/Validate/Between.php';
  450. $between = new Zend_Validate_Between(1, 50, true);
  451. if (isset($options['results']) && !$between->setMin(1)->setMax(50)->isValid($options['results'])) {
  452. /**
  453. * @see Zend_Service_Exception
  454. */
  455. require_once 'Zend/Service/Exception.php';
  456. throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
  457. }
  458. if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
  459. /**
  460. * @see Zend_Service_Exception
  461. */
  462. require_once 'Zend/Service/Exception.php';
  463. throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
  464. }
  465. if (isset($options['format'])) {
  466. switch ($options['format']) {
  467. case 'any':
  468. case 'bmp':
  469. case 'gif':
  470. case 'jpeg':
  471. case 'png':
  472. break;
  473. default:
  474. /**
  475. * @see Zend_Service_Exception
  476. */
  477. require_once 'Zend/Service/Exception.php';
  478. throw new Zend_Service_Exception("Invalid value for option 'format': {$options['format']}");
  479. }
  480. }
  481. if (isset($options['coloration'])) {
  482. switch ($options['coloration']) {
  483. case 'any':
  484. case 'color':
  485. case 'bw':
  486. break;
  487. default:
  488. /**
  489. * @see Zend_Service_Exception
  490. */
  491. require_once 'Zend/Service/Exception.php';
  492. throw new Zend_Service_Exception("Invalid value for option 'coloration': "
  493. . "{$options['coloration']}");
  494. }
  495. }
  496. }
  497. /**
  498. * Validate Local Search Options
  499. *
  500. * @param array $options
  501. * @return void
  502. * @throws Zend_Service_Exception
  503. */
  504. protected function _validateLocalSearch(array $options)
  505. {
  506. $validOptions = array('appid', 'query', 'results', 'start', 'sort', 'radius', 'street',
  507. 'city', 'state', 'zip', 'location', 'latitude', 'longitude');
  508. $this->_compareOptions($options, $validOptions);
  509. /**
  510. * @see Zend_Validate_Between
  511. */
  512. require_once 'Zend/Validate/Between.php';
  513. $between = new Zend_Validate_Between(1, 20, true);
  514. if (isset($options['results']) && !$between->setMin(1)->setMax(20)->isValid($options['results'])) {
  515. /**
  516. * @see Zend_Service_Exception
  517. */
  518. require_once 'Zend/Service/Exception.php';
  519. throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
  520. }
  521. if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
  522. /**
  523. * @see Zend_Service_Exception
  524. */
  525. require_once 'Zend/Service/Exception.php';
  526. throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
  527. }
  528. if (isset($options['longitude']) && !$between->setMin(-90)->setMax(90)->isValid($options['longitude'])) {
  529. /**
  530. * @see Zend_Service_Exception
  531. */
  532. require_once 'Zend/Service/Exception.php';
  533. throw new Zend_Service_Exception("Invalid value for option 'longitude': {$options['longitude']}");
  534. }
  535. if (isset($options['latitude']) && !$between->setMin(-180)->setMax(180)->isValid($options['latitude'])) {
  536. /**
  537. * @see Zend_Service_Exception
  538. */
  539. require_once 'Zend/Service/Exception.php';
  540. throw new Zend_Service_Exception("Invalid value for option 'latitude': {$options['latitude']}");
  541. }
  542. if (isset($options['zip']) && !preg_match('/(^\d{5}$)|(^\d{5}-\d{4}$)/', $options['zip'])) {
  543. /**
  544. * @see Zend_Service_Exception
  545. */
  546. require_once 'Zend/Service/Exception.php';
  547. throw new Zend_Service_Exception("Invalid value for option 'zip': {$options['zip']}");
  548. }
  549. $hasLocation = false;
  550. $locationFields = array('street', 'city', 'state', 'zip', 'location');
  551. foreach ($locationFields as $field) {
  552. if (isset($options[$field]) && $options[$field] != '') {
  553. $hasLocation = true;
  554. break;
  555. }
  556. }
  557. if (!$hasLocation && (!isset($options['latitude']) || !isset($options['longitude']))) {
  558. /**
  559. * @see Zend_Service_Exception
  560. */
  561. require_once 'Zend/Service/Exception.php';
  562. throw new Zend_Service_Exception('Location data are required but missing');
  563. }
  564. if (!in_array($options['sort'], array('relevance', 'title', 'distance', 'rating'))) {
  565. /**
  566. * @see Zend_Service_Exception
  567. */
  568. require_once 'Zend/Service/Exception.php';
  569. throw new Zend_Service_Exception("Invalid value for option 'sort': {$options['sort']}");
  570. }
  571. }
  572. /**
  573. * Validate News Search Options
  574. *
  575. * @param array $options
  576. * @return void
  577. * @throws Zend_Service_Exception
  578. */
  579. protected function _validateNewsSearch(array $options)
  580. {
  581. $validOptions = array('appid', 'query', 'results', 'start', 'sort', 'language', 'type', 'site');
  582. $this->_compareOptions($options, $validOptions);
  583. /**
  584. * @see Zend_Validate_Between
  585. */
  586. require_once 'Zend/Validate/Between.php';
  587. $between = new Zend_Validate_Between(1, 50, true);
  588. if (isset($options['results']) && !$between->setMin(1)->setMax(50)->isValid($options['results'])) {
  589. /**
  590. * @see Zend_Service_Exception
  591. */
  592. require_once 'Zend/Service/Exception.php';
  593. throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
  594. }
  595. if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
  596. /**
  597. * @see Zend_Service_Exception
  598. */
  599. require_once 'Zend/Service/Exception.php';
  600. throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
  601. }
  602. if (isset($options['language'])) {
  603. $this->_validateLanguage($options['language']);
  604. }
  605. $this->_validateInArray('sort', $options['sort'], array('rank', 'date'));
  606. $this->_validateInArray('type', $options['type'], array('all', 'any', 'phrase'));
  607. }
  608. /**
  609. * Validate Page Data Search Options
  610. *
  611. * @param array $options
  612. * @return void
  613. * @throws Zend_Service_Exception
  614. */
  615. protected function _validatePageDataSearch(array $options)
  616. {
  617. $validOptions = array('appid', 'query', 'results', 'start', 'domain_only');
  618. $this->_compareOptions($options, $validOptions);
  619. /**
  620. * @see Zend_Validate_Between
  621. */
  622. require_once 'Zend/Validate/Between.php';
  623. $between = new Zend_Validate_Between(1, 100, true);
  624. if (isset($options['results']) && !$between->setMin(1)->setMax(100)->isValid($options['results'])) {
  625. /**
  626. * @see Zend_Service_Exception
  627. */
  628. require_once 'Zend/Service/Exception.php';
  629. throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
  630. }
  631. if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
  632. /**
  633. * @see Zend_Service_Exception
  634. */
  635. require_once 'Zend/Service/Exception.php';
  636. throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
  637. }
  638. }
  639. /**
  640. * Validate Video Search Options
  641. *
  642. * @param array $options
  643. * @return void
  644. * @throws Zend_Service_Exception
  645. */
  646. protected function _validateVideoSearch(array $options)
  647. {
  648. $validOptions = array('appid', 'query', 'type', 'results', 'start', 'format', 'adult_ok');
  649. $this->_compareOptions($options, $validOptions);
  650. if (isset($options['type'])) {
  651. $this->_validateInArray('type', $options['type'], array('all', 'any', 'phrase'));
  652. }
  653. /**
  654. * @see Zend_Validate_Between
  655. */
  656. require_once 'Zend/Validate/Between.php';
  657. $between = new Zend_Validate_Between(1, 50, true);
  658. if (isset($options['results']) && !$between->setMin(1)->setMax(50)->isValid($options['results'])) {
  659. /**
  660. * @see Zend_Service_Exception
  661. */
  662. require_once 'Zend/Service/Exception.php';
  663. throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
  664. }
  665. if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
  666. /**
  667. * @see Zend_Service_Exception
  668. */
  669. require_once 'Zend/Service/Exception.php';
  670. throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
  671. }
  672. if (isset($options['format'])) {
  673. $this->_validateInArray('format', $options['format'], array('any', 'avi', 'flash', 'mpeg', 'msmedia', 'quicktime', 'realmedia'));
  674. }
  675. }
  676. /**
  677. * Validate Web Search Options
  678. *
  679. * @param array $options
  680. * @return void
  681. * @throws Zend_Service_Exception
  682. */
  683. protected function _validateWebSearch(array $options)
  684. {
  685. $validOptions = array('appid', 'query', 'results', 'start', 'language', 'type', 'format', 'adult_ok',
  686. 'similar_ok', 'country', 'site', 'subscription', 'license', 'region');
  687. $this->_compareOptions($options, $validOptions);
  688. /**
  689. * @see Zend_Validate_Between
  690. */
  691. require_once 'Zend/Validate/Between.php';
  692. $between = new Zend_Validate_Between(1, 100, true);
  693. if (isset($options['results']) && !$between->setMin(1)->setMax(100)->isValid($options['results'])) {
  694. /**
  695. * @see Zend_Service_Exception
  696. */
  697. require_once 'Zend/Service/Exception.php';
  698. throw new Zend_Service_Exception("Invalid value for option 'results': {$options['results']}");
  699. }
  700. if (isset($options['start']) && !$between->setMin(1)->setMax(1000)->isValid($options['start'])) {
  701. /**
  702. * @see Zend_Service_Exception
  703. */
  704. require_once 'Zend/Service/Exception.php';
  705. throw new Zend_Service_Exception("Invalid value for option 'start': {$options['start']}");
  706. }
  707. if (isset($options['language'])) {
  708. $this->_validateLanguage($options['language']);
  709. }
  710. $this->_validateInArray('type', $options['type'], array('all', 'any', 'phrase'));
  711. $this->_validateInArray('format', $options['format'], array('any', 'html', 'msword', 'pdf', 'ppt', 'rss',
  712. 'txt', 'xls'));
  713. if (isset($options['license'])) {
  714. $this->_validateInArray('license', $options['license'], array('any', 'cc_any', 'cc_commercial',
  715. 'cc_modifiable'));
  716. }
  717. if (isset($options['region'])){
  718. $this->_validateInArray('region', $options['region'], array('ar', 'au', 'at', 'br', 'ca', 'ct', 'dk', 'fi',
  719. 'fr', 'de', 'in', 'id', 'it', 'my', 'mx',
  720. 'nl', 'no', 'ph', 'ru', 'sg', 'es', 'se',
  721. 'ch', 'th', 'uk', 'us'));
  722. }
  723. }
  724. /**
  725. * Prepare options for sending to Yahoo!
  726. *
  727. * @param string $query Search Query
  728. * @param array $options User specified options
  729. * @param array $defaultOptions Required/Default options
  730. * @return array
  731. */
  732. protected function _prepareOptions($query, array $options, array $defaultOptions = array())
  733. {
  734. $options['appid'] = $this->appId;
  735. $options['query'] = (string) $query;
  736. return array_merge($defaultOptions, $options);
  737. }
  738. /**
  739. * Throws an exception if the chosen language is not supported
  740. *
  741. * @param string $lang Language code
  742. * @return void
  743. * @throws Zend_Service_Exception
  744. */
  745. protected function _validateLanguage($lang)
  746. {
  747. $languages = array('ar', 'bg', 'ca', 'szh', 'tzh', 'hr', 'cs', 'da', 'nl', 'en', 'et', 'fi', 'fr', 'de', 'el',
  748. 'he', 'hu', 'is', 'id', 'it', 'ja', 'ko', 'lv', 'lt', 'no', 'fa', 'pl', 'pt', 'ro', 'ru', 'sk', 'sr', 'sl',
  749. 'es', 'sv', 'th', 'tr'
  750. );
  751. if (!in_array($lang, $languages)) {
  752. /**
  753. * @see Zend_Service_Exception
  754. */
  755. require_once 'Zend/Service/Exception.php';
  756. throw new Zend_Service_Exception("The selected language '$lang' is not supported");
  757. }
  758. }
  759. /**
  760. * Utility function to check for a difference between two arrays.
  761. *
  762. * @param array $options User specified options
  763. * @param array $validOptions Valid options
  764. * @return void
  765. * @throws Zend_Service_Exception if difference is found (e.g., unsupported query option)
  766. */
  767. protected function _compareOptions(array $options, array $validOptions)
  768. {
  769. $difference = array_diff(array_keys($options), $validOptions);
  770. if ($difference) {
  771. /**
  772. * @see Zend_Service_Exception
  773. */
  774. require_once 'Zend/Service/Exception.php';
  775. throw new Zend_Service_Exception('The following parameters are invalid: ' . join(', ', $difference));
  776. }
  777. }
  778. /**
  779. * Check that a named value is in the given array
  780. *
  781. * @param string $name Name associated with the value
  782. * @param mixed $value Value
  783. * @param array $array Array in which to check for the value
  784. * @return void
  785. * @throws Zend_Service_Exception
  786. */
  787. protected function _validateInArray($name, $value, array $array)
  788. {
  789. if (!in_array($value, $array)) {
  790. /**
  791. * @see Zend_Service_Exception
  792. */
  793. require_once 'Zend/Service/Exception.php';
  794. throw new Zend_Service_Exception("Invalid value for option '$name': $value");
  795. }
  796. }
  797. /**
  798. * Check if response is an error
  799. *
  800. * @param DOMDocument $dom DOM Object representing the result XML
  801. * @return void
  802. * @throws Zend_Service_Exception Thrown when the result from Yahoo! is an error
  803. */
  804. protected static function _checkErrors(DOMDocument $dom)
  805. {
  806. $xpath = new DOMXPath($dom);
  807. $xpath->registerNamespace('yapi', 'urn:yahoo:api');
  808. if ($xpath->query('//yapi:Error')->length >= 1) {
  809. $message = $xpath->query('//yapi:Error/yapi:Message/text()')->item(0)->data;
  810. /**
  811. * @see Zend_Service_Exception
  812. */
  813. require_once 'Zend/Service/Exception.php';
  814. throw new Zend_Service_Exception($message);
  815. }
  816. }
  817. }