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.
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
27 require_once('Zend/Gdata/YouTube.php');
32 require_once('Zend/Gdata/Query.php');
35 * Assists in constructing queries for YouTube videos
37 * @link http://code.google.com/apis/youtube/
42 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
43 * @license http://framework.zend.com/license/new-bsd New BSD License
45 class Zend_Gdata_YouTube_VideoQuery extends Zend_Gdata_Query
49 * Create Gdata_YouTube_VideoQuery object
51 public function __construct($url = null)
53 parent::__construct($url);
57 * Sets the type of feed this query should be used to search
59 * @param string $feedType The type of feed
60 * @param string $videoId The ID of the video associated with this query
61 * @param string $entry The ID of the entry associated with this query
63 public function setFeedType($feedType, $videoId = null, $entry = null)
67 $this->_url = Zend_Gdata_YouTube::STANDARD_TOP_RATED_URI;
70 $this->_url = Zend_Gdata_YouTube::STANDARD_MOST_VIEWED_URI;
72 case 'recently featured':
73 $this->_url = Zend_Gdata_YouTube::STANDARD_RECENTLY_FEATURED_URI;
76 $this->_url = Zend_Gdata_YouTube::STANDARD_WATCH_ON_MOBILE_URI;
79 if ($videoId === null) {
80 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
81 throw new Zend_Gdata_App_InvalidArgumentException(
82 'Video ID must be set for feed of type: ' . $feedType);
84 $this->_url = Zend_Gdata_YouTube::VIDEO_URI . '/' . $videoId .
89 if ($videoId === null) {
90 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
91 throw new Zend_Gdata_App_Exception(
92 'Video ID must be set for feed of type: ' . $feedType);
94 $this->_url = Zend_Gdata_YouTube::VIDEO_URI . '/' . $videoId .
99 if ($videoId === null) {
100 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
101 throw new Zend_Gdata_App_Exception(
102 'Video ID must be set for feed of type: ' . $feedType);
104 $this->_url = Zend_Gdata_YouTube::VIDEO_URI . '/' .
105 $videoId . 'comments';
106 if ($entry !== null) {
107 $this->_url .= '/' . $entry;
112 require_once 'Zend/Gdata/App/Exception.php';
113 throw new Zend_Gdata_App_Exception('Unknown feed type');
119 * Sets the location parameter for the query
121 * @param string $value
122 * @throws Zend_Gdata_App_InvalidArgumentException
123 * @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface
125 public function setLocation($value)
129 unset($this->_params['location']);
131 $parameters = explode(',', $value);
132 if (count($parameters) != 2) {
133 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
134 throw new Zend_Gdata_App_InvalidArgumentException(
135 'You must provide 2 coordinates to the location ' .
139 foreach($parameters as $param) {
140 $temp = trim($param);
141 // strip off the optional exclamation mark for numeric check
142 if (substr($temp, -1) == '!') {
143 $temp = substr($temp, 0, -1);
145 if (!is_numeric($temp)) {
146 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
147 throw new Zend_Gdata_App_InvalidArgumentException(
148 'Value provided to location parameter must' .
149 ' be in the form of two coordinates');
152 $this->_params['location'] = $value;
157 * Get the value of the location parameter
159 * @return string|null Return the location if it exists, null otherwise.
161 public function getLocation()
163 if (array_key_exists('location', $this->_params)) {
164 return $this->_params['location'];
172 * Sets the location-radius parameter for the query
174 * @param string $value
175 * @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface
177 public function setLocationRadius($value)
181 unset($this->_params['location-radius']);
183 $this->_params['location-radius'] = $value;
188 * Get the value of the location-radius parameter
190 * @return string|null Return the location-radius if it exists,
193 public function getLocationRadius()
195 if (array_key_exists('location-radius', $this->_params)) {
196 return $this->_params['location-radius'];
203 * Sets the time period over which this query should apply
205 * @param string $value
206 * @throws Zend_Gdata_App_InvalidArgumentException
207 * @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface
209 public function setTime($value = null)
213 $this->_params['time'] = 'today';
216 $this->_params['time'] = 'this_week';
219 $this->_params['time'] = 'this_month';
222 $this->_params['time'] = 'all_time';
225 unset($this->_params['time']);
227 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
228 throw new Zend_Gdata_App_InvalidArgumentException(
229 'Unknown time value');
236 * Sets the value of the uploader parameter
238 * @param string $value The value of the uploader parameter. Currently this
239 * can only be set to the value of 'partner'.
240 * @throws Zend_Gdata_App_InvalidArgumentException
241 * @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface
243 public function setUploader($value = null)
247 $this->_params['uploader'] = 'partner';
250 unset($this->_params['uploader']);
253 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
254 throw new Zend_Gdata_App_InvalidArgumentException(
255 'Unknown value for uploader');
261 * Sets the formatted video query (vq) URL param value
263 * @param string $value
264 * @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface
266 public function setVideoQuery($value = null)
268 if ($value != null) {
269 $this->_params['vq'] = $value;
271 unset($this->_params['vq']);
277 * Sets the param to return videos of a specific format
279 * @param string $value
280 * @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface
282 public function setFormat($value = null)
284 if ($value != null) {
285 $this->_params['format'] = $value;
287 unset($this->_params['format']);
293 * Sets whether or not to include racy videos in the search results
295 * @param string $value
296 * @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface
298 public function setRacy($value = null)
302 $this->_params['racy'] = $value;
305 $this->_params['racy'] = $value;
308 unset($this->_params['racy']);
315 * Whether or not to include racy videos in the search results
317 * @return string|null The value of racy if it exists, null otherwise.
319 public function getRacy()
321 if (array_key_exists('racy', $this->_params)) {
322 return $this->_params['racy'];
329 * Set the safeSearch parameter
331 * @param string $value The value of the parameter, currently only 'none',
332 * 'moderate' or 'strict' are allowed values.
333 * @throws Zend_Gdata_App_InvalidArgumentException
334 * @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface
336 public function setSafeSearch($value)
340 $this->_params['safeSearch'] = 'none';
343 $this->_params['safeSearch'] = 'moderate';
346 $this->_params['safeSearch'] = 'strict';
349 unset($this->_params['safeSearch']);
351 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
352 throw new Zend_Gdata_App_InvalidArgumentException(
353 'The safeSearch parameter only supports the values '.
354 '\'none\', \'moderate\' or \'strict\'.');
359 * Return the value of the safeSearch parameter
361 * @return string|null The value of the safeSearch parameter if it has been
362 * set, null otherwise.
364 public function getSafeSearch()
366 if (array_key_exists('safeSearch', $this->_params)) {
367 return $this->_params['safeSearch'];
373 * Set the value of the orderby parameter
375 * @param string $value
376 * @return Zend_Gdata_YouTube_Query Provides a fluent interface
378 public function setOrderBy($value)
380 if ($value != null) {
381 $this->_params['orderby'] = $value;
383 unset($this->_params['orderby']);
389 * Return the value of the format parameter
391 * @return string|null The value of format if it exists, null otherwise.
393 public function getFormat()
395 if (array_key_exists('format', $this->_params)) {
396 return $this->_params['format'];
403 * Return the value of the video query that has been set
405 * @return string|null The value of the video query if it exists,
408 public function getVideoQuery()
410 if (array_key_exists('vq', $this->_params)) {
411 return $this->_params['vq'];
418 * Return the value of the time parameter
420 * @return string|null The time parameter if it exists, null otherwise.
422 public function getTime()
424 if (array_key_exists('time', $this->_params)) {
425 return $this->_params['time'];
432 * Return the value of the orderby parameter if it exists
434 * @return string|null The value of orderby if it exists, null otherwise.
436 public function getOrderBy()
438 if (array_key_exists('orderby', $this->_params)) {
439 return $this->_params['orderby'];
446 * Generate the query string from the URL parameters, optionally modifying
447 * them based on protocol version.
449 * @param integer $majorProtocolVersion The major protocol version
450 * @param integer $minorProtocolVersion The minor protocol version
451 * @throws Zend_Gdata_App_VersionException
452 * @return string querystring
454 public function getQueryString($majorProtocolVersion = null,
455 $minorProtocolVersion = null)
457 $queryArray = array();
459 foreach ($this->_params as $name => $value) {
460 if (substr($name, 0, 1) == '_') {
465 case 'location-radius':
466 if ($majorProtocolVersion == 1) {
467 require_once 'Zend/Gdata/App/VersionException.php';
468 throw new Zend_Gdata_App_VersionException("The $name " .
469 "parameter is only supported in version 2.");
474 if ($majorProtocolVersion == 2) {
475 require_once 'Zend/Gdata/App/VersionException.php';
476 throw new Zend_Gdata_App_VersionException("The $name " .
477 "parameter is not supported in version 2. " .
478 "Please use 'safeSearch'.");
483 if ($majorProtocolVersion == 1) {
484 require_once 'Zend/Gdata/App/VersionException.php';
485 throw new Zend_Gdata_App_VersionException("The $name " .
486 "parameter is only supported in version 2. " .
487 "Please use 'racy'.");
492 if ($majorProtocolVersion == 1) {
493 require_once 'Zend/Gdata/App/VersionException.php';
494 throw new Zend_Gdata_App_VersionException("The $name " .
495 "parameter is only supported in version 2.");
500 if ($majorProtocolVersion == 2) {
506 $queryArray[] = urlencode($name) . '=' . urlencode($value);
509 if (count($queryArray) > 0) {
510 return '?' . implode('&', $queryArray);
517 * Returns the generated full query URL, optionally modifying it based on
518 * the protocol version.
520 * @param integer $majorProtocolVersion The major protocol version
521 * @param integer $minorProtocolVersion The minor protocol version
522 * @return string The URL
524 public function getQueryUrl($majorProtocolVersion = null,
525 $minorProtocolVersion = null)
527 if (isset($this->_url)) {
530 $url = Zend_Gdata_YouTube::VIDEO_URI;
532 if ($this->getCategory() !== null) {
533 $url .= '/-/' . $this->getCategory();
535 $url = $url . $this->getQueryString($majorProtocolVersion,
536 $minorProtocolVersion);