Yahoo.php 35 KB

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