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