/zf/library/Zend/Gdata/YouTube/VideoQuery.php

http://github.com/eryx/php-framework-benchmark · PHP · 540 lines · 336 code · 36 blank · 168 comment · 58 complexity · 04a983afdc1adde822bdc5a7b485ff84 MD5 · raw file

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