VideoQuery.php 17 KB

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