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_Gdata
|
---|
18 | * @subpackage YouTube
|
---|
19 | * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
---|
20 | * @license http://framework.zend.com/license/new-bsd New BSD License
|
---|
21 | */
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Zend_Gdata_YouTube
|
---|
25 | */
|
---|
26 | require_once('Zend/Gdata/YouTube.php');
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Zend_Gdata_Query
|
---|
30 | */
|
---|
31 | require_once('Zend/Gdata/Query.php');
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Assists in constructing queries for YouTube videos
|
---|
35 | *
|
---|
36 | * @link http://code.google.com/apis/youtube/
|
---|
37 | *
|
---|
38 | * @category Zend
|
---|
39 | * @package Zend_Gdata
|
---|
40 | * @subpackage YouTube
|
---|
41 | * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
---|
42 | * @license http://framework.zend.com/license/new-bsd New BSD License
|
---|
43 | */
|
---|
44 | class Zend_Gdata_YouTube_VideoQuery extends Zend_Gdata_Query
|
---|
45 | {
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Create Gdata_YouTube_VideoQuery object
|
---|
49 | */
|
---|
50 | public function __construct($url = null)
|
---|
51 | {
|
---|
52 | parent::__construct($url);
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Sets the type of feed this query should be used to search
|
---|
57 | *
|
---|
58 | * @param string $feedType The type of feed
|
---|
59 | * @param string $videoId The ID of the video associated with this query
|
---|
60 | * @param string $entry The ID of the entry associated with this query
|
---|
61 | */
|
---|
62 | public function setFeedType($feedType, $videoId = null, $entry = null)
|
---|
63 | {
|
---|
64 | switch ($feedType) {
|
---|
65 | case 'top rated':
|
---|
66 | $this->_url = Zend_Gdata_YouTube::STANDARD_TOP_RATED_URI;
|
---|
67 | break;
|
---|
68 | case 'most viewed':
|
---|
69 | $this->_url = Zend_Gdata_YouTube::STANDARD_MOST_VIEWED_URI;
|
---|
70 | break;
|
---|
71 | case 'recently featured':
|
---|
72 | $this->_url = Zend_Gdata_YouTube::STANDARD_RECENTLY_FEATURED_URI;
|
---|
73 | break;
|
---|
74 | case 'mobile':
|
---|
75 | $this->_url = Zend_Gdata_YouTube::STANDARD_WATCH_ON_MOBILE_URI;
|
---|
76 | break;
|
---|
77 | case 'related':
|
---|
78 | if ($videoId === null) {
|
---|
79 | require_once 'Zend/Gdata/App/InvalidArgumentException.php';
|
---|
80 | throw new Zend_Gdata_App_InvalidArgumentException(
|
---|
81 | 'Video ID must be set for feed of type: ' . $feedType);
|
---|
82 | } else {
|
---|
83 | $this->_url = Zend_Gdata_YouTube::VIDEO_URI . '/' . $videoId .
|
---|
84 | '/related';
|
---|
85 | }
|
---|
86 | break;
|
---|
87 | case 'responses':
|
---|
88 | if ($videoId === null) {
|
---|
89 | require_once 'Zend/Gdata/App/InvalidArgumentException.php';
|
---|
90 | throw new Zend_Gdata_App_Exception(
|
---|
91 | 'Video ID must be set for feed of type: ' . $feedType);
|
---|
92 | } else {
|
---|
93 | $this->_url = Zend_Gdata_YouTube::VIDEO_URI . '/' . $videoId .
|
---|
94 | 'responses';
|
---|
95 | }
|
---|
96 | break;
|
---|
97 | case 'comments':
|
---|
98 | if ($videoId === null) {
|
---|
99 | require_once 'Zend/Gdata/App/InvalidArgumentException.php';
|
---|
100 | throw new Zend_Gdata_App_Exception(
|
---|
101 | 'Video ID must be set for feed of type: ' . $feedType);
|
---|
102 | } else {
|
---|
103 | $this->_url = Zend_Gdata_YouTube::VIDEO_URI . '/' .
|
---|
104 | $videoId . 'comments';
|
---|
105 | if ($entry !== null) {
|
---|
106 | $this->_url .= '/' . $entry;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | break;
|
---|
110 | default:
|
---|
111 | require_once 'Zend/Gdata/App/Exception.php';
|
---|
112 | throw new Zend_Gdata_App_Exception('Unknown feed type');
|
---|
113 | break;
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Sets the location parameter for the query
|
---|
119 | *
|
---|
120 | * @param string $value
|
---|
121 | * @throws Zend_Gdata_App_InvalidArgumentException
|
---|
122 | * @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface
|
---|
123 | */
|
---|
124 | public function setLocation($value)
|
---|
125 | {
|
---|
126 | switch($value) {
|
---|
127 | case null:
|
---|
128 | unset($this->_params['location']);
|
---|
129 | default:
|
---|
130 | $parameters = explode(',', $value);
|
---|
131 | if (count($parameters) != 2) {
|
---|
132 | require_once 'Zend/Gdata/App/InvalidArgumentException.php';
|
---|
133 | throw new Zend_Gdata_App_InvalidArgumentException(
|
---|
134 | 'You must provide 2 coordinates to the location ' .
|
---|
135 | 'URL parameter');
|
---|
136 | }
|
---|
137 |
|
---|
138 | foreach($parameters as $param) {
|
---|
139 | $temp = trim($param);
|
---|
140 | // strip off the optional exclamation mark for numeric check
|
---|
141 | if (substr($temp, -1) == '!') {
|
---|
142 | $temp = substr($temp, -1);
|
---|
143 | }
|
---|
144 | if (!is_numeric($temp)) {
|
---|
145 | require_once 'Zend/Gdata/App/InvalidArgumentException.php';
|
---|
146 | throw new Zend_Gdata_App_InvalidArgumentException(
|
---|
147 | 'Value provided to location parameter must' .
|
---|
148 | ' be in the form of two coordinates');
|
---|
149 | }
|
---|
150 | }
|
---|
151 | $this->_params['location'] = $value;
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Get the value of the location parameter
|
---|
157 | *
|
---|
158 | * @return string|null Return the location if it exists, null otherwise.
|
---|
159 | */
|
---|
160 | public function getLocation()
|
---|
161 | {
|
---|
162 | if (array_key_exists('location', $this->_params)) {
|
---|
163 | return $this->_params['location'];
|
---|
164 | } else {
|
---|
165 | return null;
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Sets the location-radius parameter for the query
|
---|
172 | *
|
---|
173 | * @param string $value
|
---|
174 | * @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface
|
---|
175 | */
|
---|
176 | public function setLocationRadius($value)
|
---|
177 | {
|
---|
178 | switch($value) {
|
---|
179 | case null:
|
---|
180 | unset($this->_params['location-radius']);
|
---|
181 | default:
|
---|
182 | $this->_params['location-radius'] = $value;
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Get the value of the location-radius parameter
|
---|
188 | *
|
---|
189 | * @return string|null Return the location-radius if it exists,
|
---|
190 | * null otherwise.
|
---|
191 | */
|
---|
192 | public function getLocationRadius()
|
---|
193 | {
|
---|
194 | if (array_key_exists('location-radius', $this->_params)) {
|
---|
195 | return $this->_params['location-radius'];
|
---|
196 | } else {
|
---|
197 | return null;
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Sets the time period over which this query should apply
|
---|
203 | *
|
---|
204 | * @param string $value
|
---|
205 | * @throws Zend_Gdata_App_InvalidArgumentException
|
---|
206 | * @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface
|
---|
207 | */
|
---|
208 | public function setTime($value = null)
|
---|
209 | {
|
---|
210 | switch ($value) {
|
---|
211 | case 'today':
|
---|
212 | $this->_params['time'] = 'today';
|
---|
213 | break;
|
---|
214 | case 'this_week':
|
---|
215 | $this->_params['time'] = 'this_week';
|
---|
216 | break;
|
---|
217 | case 'this_month':
|
---|
218 | $this->_params['time'] = 'this_month';
|
---|
219 | break;
|
---|
220 | case 'all_time':
|
---|
221 | $this->_params['time'] = 'all_time';
|
---|
222 | break;
|
---|
223 | case null:
|
---|
224 | unset($this->_params['time']);
|
---|
225 | default:
|
---|
226 | require_once 'Zend/Gdata/App/InvalidArgumentException.php';
|
---|
227 | throw new Zend_Gdata_App_InvalidArgumentException(
|
---|
228 | 'Unknown time value');
|
---|
229 | break;
|
---|
230 | }
|
---|
231 | return $this;
|
---|
232 | }
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * Sets the value of the uploader parameter
|
---|
236 | *
|
---|
237 | * @param string $value The value of the uploader parameter. Currently this
|
---|
238 | * can only be set to the value of 'partner'.
|
---|
239 | * @throws Zend_Gdata_App_InvalidArgumentException
|
---|
240 | * @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface
|
---|
241 | */
|
---|
242 | public function setUploader($value = null)
|
---|
243 | {
|
---|
244 | switch ($value) {
|
---|
245 | case 'partner':
|
---|
246 | $this->_params['uploader'] = 'partner';
|
---|
247 | break;
|
---|
248 | case null:
|
---|
249 | unset($this->_params['uploader']);
|
---|
250 | break;
|
---|
251 | default:
|
---|
252 | require_once 'Zend/Gdata/App/InvalidArgumentException.php';
|
---|
253 | throw new Zend_Gdata_App_InvalidArgumentException(
|
---|
254 | 'Unknown value for uploader');
|
---|
255 | }
|
---|
256 | return $this;
|
---|
257 | }
|
---|
258 |
|
---|
259 | /**
|
---|
260 | * Sets the formatted video query (vq) URL param value
|
---|
261 | *
|
---|
262 | * @param string $value
|
---|
263 | * @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface
|
---|
264 | */
|
---|
265 | public function setVideoQuery($value = null)
|
---|
266 | {
|
---|
267 | if ($value != null) {
|
---|
268 | $this->_params['vq'] = $value;
|
---|
269 | } else {
|
---|
270 | unset($this->_params['vq']);
|
---|
271 | }
|
---|
272 | return $this;
|
---|
273 | }
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * Sets the param to return videos of a specific format
|
---|
277 | *
|
---|
278 | * @param string $value
|
---|
279 | * @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface
|
---|
280 | */
|
---|
281 | public function setFormat($value = null)
|
---|
282 | {
|
---|
283 | if ($value != null) {
|
---|
284 | $this->_params['format'] = $value;
|
---|
285 | } else {
|
---|
286 | unset($this->_params['format']);
|
---|
287 | }
|
---|
288 | return $this;
|
---|
289 | }
|
---|
290 |
|
---|
291 | /**
|
---|
292 | * Sets whether or not to include racy videos in the search results
|
---|
293 | *
|
---|
294 | * @param string $value
|
---|
295 | * @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface
|
---|
296 | */
|
---|
297 | public function setRacy($value = null)
|
---|
298 | {
|
---|
299 | switch ($value) {
|
---|
300 | case 'include':
|
---|
301 | $this->_params['racy'] = $value;
|
---|
302 | break;
|
---|
303 | case 'exclude':
|
---|
304 | $this->_params['racy'] = $value;
|
---|
305 | break;
|
---|
306 | case null:
|
---|
307 | unset($this->_params['racy']);
|
---|
308 | break;
|
---|
309 | }
|
---|
310 | return $this;
|
---|
311 | }
|
---|
312 |
|
---|
313 | /**
|
---|
314 | * Whether or not to include racy videos in the search results
|
---|
315 | *
|
---|
316 | * @return string|null The value of racy if it exists, null otherwise.
|
---|
317 | */
|
---|
318 | public function getRacy()
|
---|
319 | {
|
---|
320 | if (array_key_exists('racy', $this->_params)) {
|
---|
321 | return $this->_params['racy'];
|
---|
322 | } else {
|
---|
323 | return null;
|
---|
324 | }
|
---|
325 | }
|
---|
326 |
|
---|
327 | /**
|
---|
328 | * Set the safeSearch parameter
|
---|
329 | *
|
---|
330 | * @param string $value The value of the parameter, currently only 'none',
|
---|
331 | * 'moderate' or 'strict' are allowed values.
|
---|
332 | * @throws Zend_Gdata_App_InvalidArgumentException
|
---|
333 | * @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface
|
---|
334 | */
|
---|
335 | public function setSafeSearch($value)
|
---|
336 | {
|
---|
337 | switch ($value) {
|
---|
338 | case 'none':
|
---|
339 | $this->_params['safeSearch'] = 'none';
|
---|
340 | break;
|
---|
341 | case 'moderate':
|
---|
342 | $this->_params['safeSearch'] = 'moderate';
|
---|
343 | break;
|
---|
344 | case 'strict':
|
---|
345 | $this->_params['safeSearch'] = 'strict';
|
---|
346 | break;
|
---|
347 | case null:
|
---|
348 | unset($this->_params['safeSearch']);
|
---|
349 | default:
|
---|
350 | require_once 'Zend/Gdata/App/InvalidArgumentException.php';
|
---|
351 | throw new Zend_Gdata_App_InvalidArgumentException(
|
---|
352 | 'The safeSearch parameter only supports the values '.
|
---|
353 | '\'none\', \'moderate\' or \'strict\'.');
|
---|
354 | }
|
---|
355 | }
|
---|
356 |
|
---|
357 | /**
|
---|
358 | * Return the value of the safeSearch parameter
|
---|
359 | *
|
---|
360 | * @return string|null The value of the safeSearch parameter if it has been
|
---|
361 | * set, null otherwise.
|
---|
362 | */
|
---|
363 | public function getSafeSearch()
|
---|
364 | {
|
---|
365 | if (array_key_exists('safeSearch', $this->_params)) {
|
---|
366 | return $this->_params['safeSearch'];
|
---|
367 | }
|
---|
368 | return $this;
|
---|
369 | }
|
---|
370 |
|
---|
371 | /**
|
---|
372 | * Set the value of the orderby parameter
|
---|
373 | *
|
---|
374 | * @param string $value
|
---|
375 | * @return Zend_Gdata_YouTube_Query Provides a fluent interface
|
---|
376 | */
|
---|
377 | public function setOrderBy($value)
|
---|
378 | {
|
---|
379 | if ($value != null) {
|
---|
380 | $this->_params['orderby'] = $value;
|
---|
381 | } else {
|
---|
382 | unset($this->_params['orderby']);
|
---|
383 | }
|
---|
384 | return $this;
|
---|
385 | }
|
---|
386 |
|
---|
387 | /**
|
---|
388 | * Return the value of the format parameter
|
---|
389 | *
|
---|
390 | * @return string|null The value of format if it exists, null otherwise.
|
---|
391 | */
|
---|
392 | public function getFormat()
|
---|
393 | {
|
---|
394 | if (array_key_exists('format', $this->_params)) {
|
---|
395 | return $this->_params['format'];
|
---|
396 | } else {
|
---|
397 | return null;
|
---|
398 | }
|
---|
399 | }
|
---|
400 |
|
---|
401 | /**
|
---|
402 | * Return the value of the video query that has been set
|
---|
403 | *
|
---|
404 | * @return string|null The value of the video query if it exists,
|
---|
405 | * null otherwise.
|
---|
406 | */
|
---|
407 | public function getVideoQuery()
|
---|
408 | {
|
---|
409 | if (array_key_exists('vq', $this->_params)) {
|
---|
410 | return $this->_params['vq'];
|
---|
411 | } else {
|
---|
412 | return null;
|
---|
413 | }
|
---|
414 | }
|
---|
415 |
|
---|
416 | /**
|
---|
417 | * Return the value of the time parameter
|
---|
418 | *
|
---|
419 | * @return string|null The time parameter if it exists, null otherwise.
|
---|
420 | */
|
---|
421 | public function getTime()
|
---|
422 | {
|
---|
423 | if (array_key_exists('time', $this->_params)) {
|
---|
424 | return $this->_params['time'];
|
---|
425 | } else {
|
---|
426 | return null;
|
---|
427 | }
|
---|
428 | }
|
---|
429 |
|
---|
430 | /**
|
---|
431 | * Return the value of the orderby parameter if it exists
|
---|
432 | *
|
---|
433 | * @return string|null The value of orderby if it exists, null otherwise.
|
---|
434 | */
|
---|
435 | public function getOrderBy()
|
---|
436 | {
|
---|
437 | if (array_key_exists('orderby', $this->_params)) {
|
---|
438 | return $this->_params['orderby'];
|
---|
439 | } else {
|
---|
440 | return null;
|
---|
441 | }
|
---|
442 | }
|
---|
443 |
|
---|
444 | /**
|
---|
445 | * Generate the query string from the URL parameters, optionally modifying
|
---|
446 | * them based on protocol version.
|
---|
447 | *
|
---|
448 | * @param integer $majorProtocolVersion The major protocol version
|
---|
449 | * @param integer $minorProtocolVersion The minor protocol version
|
---|
450 | * @throws Zend_Gdata_App_VersionException
|
---|
451 | * @return string querystring
|
---|
452 | */
|
---|
453 | public function getQueryString($majorProtocolVersion = null,
|
---|
454 | $minorProtocolVersion = null)
|
---|
455 | {
|
---|
456 | $queryArray = array();
|
---|
457 |
|
---|
458 | foreach ($this->_params as $name => $value) {
|
---|
459 | if (substr($name, 0, 1) == '_') {
|
---|
460 | continue;
|
---|
461 | }
|
---|
462 |
|
---|
463 | switch($name) {
|
---|
464 | case 'location-radius':
|
---|
465 | if ($majorProtocolVersion == 1) {
|
---|
466 | require_once 'Zend/Gdata/App/VersionException.php';
|
---|
467 | throw new Zend_Gdata_App_VersionException("The $name " .
|
---|
468 | "parameter is only supported in version 2.");
|
---|
469 | }
|
---|
470 | break;
|
---|
471 |
|
---|
472 | case 'racy':
|
---|
473 | if ($majorProtocolVersion == 2) {
|
---|
474 | require_once 'Zend/Gdata/App/VersionException.php';
|
---|
475 | throw new Zend_Gdata_App_VersionException("The $name " .
|
---|
476 | "parameter is not supported in version 2. " .
|
---|
477 | "Please use 'safeSearch'.");
|
---|
478 | }
|
---|
479 | break;
|
---|
480 |
|
---|
481 | case 'safeSearch':
|
---|
482 | if ($majorProtocolVersion == 1) {
|
---|
483 | require_once 'Zend/Gdata/App/VersionException.php';
|
---|
484 | throw new Zend_Gdata_App_VersionException("The $name " .
|
---|
485 | "parameter is only supported in version 2. " .
|
---|
486 | "Please use 'racy'.");
|
---|
487 | }
|
---|
488 | break;
|
---|
489 |
|
---|
490 | case 'uploader':
|
---|
491 | if ($majorProtocolVersion == 1) {
|
---|
492 | require_once 'Zend/Gdata/App/VersionException.php';
|
---|
493 | throw new Zend_Gdata_App_VersionException("The $name " .
|
---|
494 | "parameter is only supported in version 2.");
|
---|
495 | }
|
---|
496 | break;
|
---|
497 |
|
---|
498 | case 'vq':
|
---|
499 | if ($majorProtocolVersion == 2) {
|
---|
500 | $name = 'q';
|
---|
501 | }
|
---|
502 | break;
|
---|
503 | }
|
---|
504 |
|
---|
505 | $queryArray[] = urlencode($name) . '=' . urlencode($value);
|
---|
506 |
|
---|
507 | }
|
---|
508 | if (count($queryArray) > 0) {
|
---|
509 | return '?' . implode('&', $queryArray);
|
---|
510 | } else {
|
---|
511 | return '';
|
---|
512 | }
|
---|
513 | }
|
---|
514 |
|
---|
515 | /**
|
---|
516 | * Returns the generated full query URL, optionally modifying it based on
|
---|
517 | * the protocol version.
|
---|
518 | *
|
---|
519 | * @param integer $majorProtocolVersion The major protocol version
|
---|
520 | * @param integer $minorProtocolVersion The minor protocol version
|
---|
521 | * @return string The URL
|
---|
522 | */
|
---|
523 | public function getQueryUrl($majorProtocolVersion = null,
|
---|
524 | $minorProtocolVersion = null)
|
---|
525 | {
|
---|
526 | if (isset($this->_url)) {
|
---|
527 | $url = $this->_url;
|
---|
528 | } else {
|
---|
529 | $url = Zend_Gdata_YouTube::VIDEO_URI;
|
---|
530 | }
|
---|
531 | if ($this->getCategory() !== null) {
|
---|
532 | $url .= '/-/' . $this->getCategory();
|
---|
533 | }
|
---|
534 | $url = $url . $this->getQueryString($majorProtocolVersion,
|
---|
535 | $minorProtocolVersion);
|
---|
536 | return $url;
|
---|
537 | }
|
---|
538 |
|
---|
539 | }
|
---|