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