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
|
<?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_Gdata
* @subpackage YouTube
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: VideoQuery.php 25185 2013-01-08 08:07:08Z frosch $
*/
/**
* Zend_Gdata_YouTube
*/
require_once('Zend/Gdata/YouTube.php');
/**
* Zend_Gdata_Query
*/
require_once('Zend/Gdata/Query.php');
/**
* Assists in constructing queries for YouTube videos
*
* @link http://code.google.com/apis/youtube/
*
* @category Zend
* @package Zend_Gdata
* @subpackage YouTube
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Gdata_YouTube_VideoQuery extends Zend_Gdata_Query
{
/**
* Create Gdata_YouTube_VideoQuery object
*/
public function __construct($url = null)
{
parent::__construct($url);
}
/**
* Sets the type of feed this query should be used to search
*
* @param string $feedType The type of feed
* @param string $videoId The ID of the video associated with this query
* @param string $entry The ID of the entry associated with this query
*/
public function setFeedType($feedType, $videoId = null, $entry = null)
{
switch ($feedType) {
case 'top rated':
$this->_url = Zend_Gdata_YouTube::STANDARD_TOP_RATED_URI;
break;
case 'most viewed':
$this->_url = Zend_Gdata_YouTube::STANDARD_MOST_VIEWED_URI;
break;
case 'recently featured':
$this->_url = Zend_Gdata_YouTube::STANDARD_RECENTLY_FEATURED_URI;
break;
case 'mobile':
$this->_url = Zend_Gdata_YouTube::STANDARD_WATCH_ON_MOBILE_URI;
break;
case 'related':
if ($videoId === null) {
require_once 'Zend/Gdata/App/InvalidArgumentException.php';
throw new Zend_Gdata_App_InvalidArgumentException(
'Video ID must be set for feed of type: ' . $feedType);
} else {
$this->_url = Zend_Gdata_YouTube::VIDEO_URI . '/' . $videoId .
'/related';
}
break;
case 'responses':
if ($videoId === null) {
require_once 'Zend/Gdata/App/InvalidArgumentException.php';
throw new Zend_Gdata_App_Exception(
'Video ID must be set for feed of type: ' . $feedType);
} else {
$this->_url = Zend_Gdata_YouTube::VIDEO_URI . '/' . $videoId .
'/responses';
}
break;
case 'comments':
if ($videoId === null) {
require_once 'Zend/Gdata/App/InvalidArgumentException.php';
throw new Zend_Gdata_App_Exception(
'Video ID must be set for feed of type: ' . $feedType);
} else {
$this->_url = Zend_Gdata_YouTube::VIDEO_URI . '/' .
$videoId . '/comments';
if ($entry !== null) {
$this->_url .= '/' . $entry;
}
}
break;
default:
require_once 'Zend/Gdata/App/Exception.php';
throw new Zend_Gdata_App_Exception('Unknown feed type');
break;
}
}
/**
* Sets the location parameter for the query
*
* @param string $value
* @throws Zend_Gdata_App_InvalidArgumentException
* @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface
*/
public function setLocation($value)
{
switch($value) {
case null:
unset($this->_params['location']);
default:
$parameters = explode(',', $value);
if (count($parameters) != 2) {
require_once 'Zend/Gdata/App/InvalidArgumentException.php';
throw new Zend_Gdata_App_InvalidArgumentException(
'You must provide 2 coordinates to the location ' .
'URL parameter');
}
foreach($parameters as $param) {
$temp = trim($param);
// strip off the optional exclamation mark for numeric check
if (substr($temp, -1) == '!') {
$temp = substr($temp, 0, -1);
}
if (!is_numeric($temp)) {
require_once 'Zend/Gdata/App/InvalidArgumentException.php';
throw new Zend_Gdata_App_InvalidArgumentException(
'Value provided to location parameter must' .
' be in the form of two coordinates');
}
}
$this->_params['location'] = $value;
}
}
/**
* Get the value of the location parameter
*
* @return string|null Return the location if it exists, null otherwise.
*/
public function getLocation()
{
if (array_key_exists('location', $this->_params)) {
return $this->_params['location'];
} else {
return null;
}
}
/**
* Sets the location-radius parameter for the query
*
* @param string $value
* @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface
*/
public function setLocationRadius($value)
{
switch($value) {
case null:
unset($this->_params['location-radius']);
default:
$this->_params['location-radius'] = $value;
}
}
/**
* Get the value of the location-radius parameter
*
* @return string|null Return the location-radius if it exists,
* null otherwise.
*/
public function getLocationRadius()
{
if (array_key_exists('location-radius', $this->_params)) {
return $this->_params['location-radius'];
} else {
return null;
}
}
/**
* Sets the time period over which this query should apply
*
* @param string $value
* @throws Zend_Gdata_App_InvalidArgumentException
* @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface
*/
public function setTime($value = null)
{
switch ($value) {
case 'today':
$this->_params['time'] = 'today';
break;
case 'this_week':
$this->_params['time'] = 'this_week';
break;
case 'this_month':
$this->_params['time'] = 'this_month';
break;
case 'all_time':
$this->_params['time'] = 'all_time';
break;
case null:
unset($this->_params['time']);
default:
require_once 'Zend/Gdata/App/InvalidArgumentException.php';
throw new Zend_Gdata_App_InvalidArgumentException(
'Unknown time value');
break;
}
return $this;
}
/**
* Sets the value of the uploader parameter
*
* @param string $value The value of the uploader parameter. Currently this
* can only be set to the value of 'partner'.
* @throws Zend_Gdata_App_InvalidArgumentException
* @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface
*/
public function setUploader($value = null)
{
switch ($value) {
case 'partner':
$this->_params['uploader'] = 'partner';
break;
case null:
unset($this->_params['uploader']);
break;
default:
require_once 'Zend/Gdata/App/InvalidArgumentException.php';
throw new Zend_Gdata_App_InvalidArgumentException(
'Unknown value for uploader');
}
return $this;
}
/**
* Sets the formatted video query (vq) URL param value
*
* @param string $value
* @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface
*/
public function setVideoQuery($value = null)
{
if ($value != null) {
$this->_params['vq'] = $value;
} else {
unset($this->_params['vq']);
}
return $this;
}
/**
* Sets the param to return videos of a specific format
*
* @param string $value
* @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface
*/
public function setFormat($value = null)
{
if ($value != null) {
$this->_params['format'] = $value;
} else {
unset($this->_params['format']);
}
return $this;
}
/**
* Sets whether or not to include racy videos in the search results
*
* @param string $value
* @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface
*/
public function setRacy($value = null)
{
switch ($value) {
case 'include':
$this->_params['racy'] = $value;
break;
case 'exclude':
$this->_params['racy'] = $value;
break;
case null:
unset($this->_params['racy']);
break;
}
return $this;
}
/**
* Whether or not to include racy videos in the search results
*
* @return string|null The value of racy if it exists, null otherwise.
*/
public function getRacy()
{
if (array_key_exists('racy', $this->_params)) {
return $this->_params['racy'];
} else {
return null;
}
}
/**
* Set the safeSearch parameter
*
* @param string $value The value of the parameter, currently only 'none',
* 'moderate' or 'strict' are allowed values.
* @throws Zend_Gdata_App_InvalidArgumentException
* @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface
*/
public function setSafeSearch($value)
{
switch ($value) {
case 'none':
$this->_params['safeSearch'] = 'none';
break;
case 'moderate':
$this->_params['safeSearch'] = 'moderate';
break;
case 'strict':
$this->_params['safeSearch'] = 'strict';
break;
case null:
unset($this->_params['safeSearch']);
default:
require_once 'Zend/Gdata/App/InvalidArgumentException.php';
throw new Zend_Gdata_App_InvalidArgumentException(
'The safeSearch parameter only supports the values '.
'\'none\', \'moderate\' or \'strict\'.');
}
}
/**
* Return the value of the safeSearch parameter
*
* @return string|null The value of the safeSearch parameter if it has been
* set, null otherwise.
*/
public function getSafeSearch()
{
if (array_key_exists('safeSearch', $this->_params)) {
return $this->_params['safeSearch'];
}
return $this;
}
/**
* Set the value of the orderby parameter
*
* @param string $value
* @return Zend_Gdata_YouTube_Query Provides a fluent interface
*/
public function setOrderBy($value)
{
if ($value != null) {
$this->_params['orderby'] = $value;
} else {
unset($this->_params['orderby']);
}
return $this;
}
/**
* Return the value of the format parameter
*
* @return string|null The value of format if it exists, null otherwise.
*/
public function getFormat()
{
if (array_key_exists('format', $this->_params)) {
return $this->_params['format'];
} else {
return null;
}
}
/**
* Return the value of the video query that has been set
*
* @return string|null The value of the video query if it exists,
* null otherwise.
*/
public function getVideoQuery()
{
if (array_key_exists('vq', $this->_params)) {
return $this->_params['vq'];
} else {
return null;
}
}
/**
* Return the value of the time parameter
*
* @return string|null The time parameter if it exists, null otherwise.
*/
public function getTime()
{
if (array_key_exists('time', $this->_params)) {
return $this->_params['time'];
} else {
return null;
}
}
/**
* Return the value of the orderby parameter if it exists
*
* @return string|null The value of orderby if it exists, null otherwise.
*/
public function getOrderBy()
{
if (array_key_exists('orderby', $this->_params)) {
return $this->_params['orderby'];
} else {
return null;
}
}
/**
* Generate the query string from the URL parameters, optionally modifying
* them based on protocol version.
*
* @param integer $majorProtocolVersion The major protocol version
* @param integer $minorProtocolVersion The minor protocol version
* @throws Zend_Gdata_App_VersionException
* @return string querystring
*/
public function getQueryString($majorProtocolVersion = null,
$minorProtocolVersion = null)
{
$queryArray = array();
foreach ($this->_params as $name => $value) {
if (substr($name, 0, 1) == '_') {
continue;
}
switch($name) {
case 'location-radius':
if ($majorProtocolVersion == 1) {
require_once 'Zend/Gdata/App/VersionException.php';
throw new Zend_Gdata_App_VersionException("The $name " .
"parameter is only supported in version 2.");
}
break;
case 'racy':
if ($majorProtocolVersion == 2) {
require_once 'Zend/Gdata/App/VersionException.php';
throw new Zend_Gdata_App_VersionException("The $name " .
"parameter is not supported in version 2. " .
"Please use 'safeSearch'.");
}
break;
case 'safeSearch':
if ($majorProtocolVersion == 1) {
require_once 'Zend/Gdata/App/VersionException.php';
throw new Zend_Gdata_App_VersionException("The $name " .
"parameter is only supported in version 2. " .
"Please use 'racy'.");
}
break;
case 'uploader':
if ($majorProtocolVersion == 1) {
require_once 'Zend/Gdata/App/VersionException.php';
throw new Zend_Gdata_App_VersionException("The $name " .
"parameter is only supported in version 2.");
}
break;
case 'vq':
if ($majorProtocolVersion == 2) {
$name = 'q';
}
break;
}
$queryArray[] = urlencode($name) . '=' . urlencode($value);
}
if (count($queryArray) > 0) {
return '?' . implode('&', $queryArray);
} else {
return '';
}
}
/**
* Returns the generated full query URL, optionally modifying it based on
* the protocol version.
*
* @param integer $majorProtocolVersion The major protocol version
* @param integer $minorProtocolVersion The minor protocol version
* @return string The URL
*/
public function getQueryUrl($majorProtocolVersion = null,
$minorProtocolVersion = null)
{
if (isset($this->_url)) {
$url = $this->_url;
} else {
$url = Zend_Gdata_YouTube::VIDEO_URI;
}
if ($this->getCategory() !== null) {
$url .= '/-/' . $this->getCategory();
}
$url = $url . $this->getQueryString($majorProtocolVersion,
$minorProtocolVersion);
return $url;
}
}
|