Query.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 Gdata
  18. * @copyright Copyright (c) 2005-2009 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_App_Util
  23. */
  24. require_once 'Zend/Gdata/App/Util.php';
  25. /**
  26. * Provides a mechanism to build a query URL for Gdata services.
  27. * Queries are not defined for APP, but are provided by Gdata services
  28. * as an extension.
  29. *
  30. * @category Zend
  31. * @package Zend_Gdata
  32. * @subpackage Gdata
  33. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Gdata_Query
  37. {
  38. /**
  39. * Query parameters.
  40. *
  41. * @var array
  42. */
  43. protected $_params = array();
  44. /**
  45. * Default URL
  46. *
  47. * @var string
  48. */
  49. protected $_defaultFeedUri = null;
  50. /**
  51. * Base URL
  52. * TODO: Add setters and getters
  53. *
  54. * @var string
  55. */
  56. protected $_url = null;
  57. /**
  58. * Category for the query
  59. *
  60. * @var string
  61. */
  62. protected $_category = null;
  63. /**
  64. * Create Gdata_Query object
  65. */
  66. public function __construct($url = null)
  67. {
  68. $this->_url = $url;
  69. }
  70. /**
  71. * @return string querystring
  72. */
  73. public function getQueryString()
  74. {
  75. $queryArray = array();
  76. foreach ($this->_params as $name => $value) {
  77. if (substr($name, 0, 1) == '_') {
  78. continue;
  79. }
  80. $queryArray[] = urlencode($name) . '=' . urlencode($value);
  81. }
  82. if (count($queryArray) > 0) {
  83. return '?' . implode('&', $queryArray);
  84. } else {
  85. return '';
  86. }
  87. }
  88. /**
  89. *
  90. */
  91. public function resetParameters()
  92. {
  93. $this->_params = array();
  94. }
  95. /**
  96. * @return string url
  97. */
  98. public function getQueryUrl()
  99. {
  100. if ($this->_url == null) {
  101. $url = $this->_defaultFeedUri;
  102. } else {
  103. $url = $this->_url;
  104. }
  105. if ($this->getCategory() !== null) {
  106. $url .= '/-/' . $this->getCategory();
  107. }
  108. $url .= $this->getQueryString();
  109. return $url;
  110. }
  111. /**
  112. * @param string $name
  113. * @param string $value
  114. * @return Zend_Gdata_Query Provides a fluent interface
  115. */
  116. public function setParam($name, $value)
  117. {
  118. $this->_params[$name] = $value;
  119. return $this;
  120. }
  121. /**
  122. * @param string $name
  123. */
  124. public function getParam($name)
  125. {
  126. return $this->_params[$name];
  127. }
  128. /**
  129. * @param string $value
  130. * @return Zend_Gdata_Query Provides a fluent interface
  131. */
  132. public function setAlt($value)
  133. {
  134. if ($value != null) {
  135. $this->_params['alt'] = $value;
  136. } else {
  137. unset($this->_params['alt']);
  138. }
  139. return $this;
  140. }
  141. /**
  142. * @param int $value
  143. * @return Zend_Gdata_Query Provides a fluent interface
  144. */
  145. public function setMaxResults($value)
  146. {
  147. if ($value != null) {
  148. $this->_params['max-results'] = $value;
  149. } else {
  150. unset($this->_params['max-results']);
  151. }
  152. return $this;
  153. }
  154. /**
  155. * @param string $value
  156. * @return Zend_Gdata_Query Provides a fluent interface
  157. */
  158. public function setQuery($value)
  159. {
  160. if ($value != null) {
  161. $this->_params['q'] = $value;
  162. } else {
  163. unset($this->_params['q']);
  164. }
  165. return $this;
  166. }
  167. /**
  168. * @param int $value
  169. * @return Zend_Gdata_Query Provides a fluent interface
  170. */
  171. public function setStartIndex($value)
  172. {
  173. if ($value != null) {
  174. $this->_params['start-index'] = $value;
  175. } else {
  176. unset($this->_params['start-index']);
  177. }
  178. return $this;
  179. }
  180. /**
  181. * @param string $value
  182. * @return Zend_Gdata_Query Provides a fluent interface
  183. */
  184. public function setUpdatedMax($value)
  185. {
  186. if ($value != null) {
  187. $this->_params['updated-max'] = Zend_Gdata_App_Util::formatTimestamp($value);
  188. } else {
  189. unset($this->_params['updated-max']);
  190. }
  191. return $this;
  192. }
  193. /**
  194. * @param string $value
  195. * @return Zend_Gdata_Query Provides a fluent interface
  196. */
  197. public function setUpdatedMin($value)
  198. {
  199. if ($value != null) {
  200. $this->_params['updated-min'] = Zend_Gdata_App_Util::formatTimestamp($value);
  201. } else {
  202. unset($this->_params['updated-min']);
  203. }
  204. return $this;
  205. }
  206. /**
  207. * @param string $value
  208. * @return Zend_Gdata_Query Provides a fluent interface
  209. */
  210. public function setPublishedMax($value)
  211. {
  212. if ($value !== null) {
  213. $this->_params['published-max'] = Zend_Gdata_App_Util::formatTimestamp($value);
  214. } else {
  215. unset($this->_params['published-max']);
  216. }
  217. return $this;
  218. }
  219. /**
  220. * @param string $value
  221. * @return Zend_Gdata_Query Provides a fluent interface
  222. */
  223. public function setPublishedMin($value)
  224. {
  225. if ($value != null) {
  226. $this->_params['published-min'] = Zend_Gdata_App_Util::formatTimestamp($value);
  227. } else {
  228. unset($this->_params['published-min']);
  229. }
  230. return $this;
  231. }
  232. /**
  233. * @param string $value
  234. * @return Zend_Gdata_Query Provides a fluent interface
  235. */
  236. public function setAuthor($value)
  237. {
  238. if ($value != null) {
  239. $this->_params['author'] = $value;
  240. } else {
  241. unset($this->_params['author']);
  242. }
  243. return $this;
  244. }
  245. /**
  246. * @return string rss or atom
  247. */
  248. public function getAlt()
  249. {
  250. if (array_key_exists('alt', $this->_params)) {
  251. return $this->_params['alt'];
  252. } else {
  253. return null;
  254. }
  255. }
  256. /**
  257. * @return int maxResults
  258. */
  259. public function getMaxResults()
  260. {
  261. if (array_key_exists('max-results', $this->_params)) {
  262. return intval($this->_params['max-results']);
  263. } else {
  264. return null;
  265. }
  266. }
  267. /**
  268. * @return string query
  269. */
  270. public function getQuery()
  271. {
  272. if (array_key_exists('q', $this->_params)) {
  273. return $this->_params['q'];
  274. } else {
  275. return null;
  276. }
  277. }
  278. /**
  279. * @return int startIndex
  280. */
  281. public function getStartIndex()
  282. {
  283. if (array_key_exists('start-index', $this->_params)) {
  284. return intval($this->_params['start-index']);
  285. } else {
  286. return null;
  287. }
  288. }
  289. /**
  290. * @return string updatedMax
  291. */
  292. public function getUpdatedMax()
  293. {
  294. if (array_key_exists('updated-max', $this->_params)) {
  295. return $this->_params['updated-max'];
  296. } else {
  297. return null;
  298. }
  299. }
  300. /**
  301. * @return string updatedMin
  302. */
  303. public function getUpdatedMin()
  304. {
  305. if (array_key_exists('updated-min', $this->_params)) {
  306. return $this->_params['updated-min'];
  307. } else {
  308. return null;
  309. }
  310. }
  311. /**
  312. * @return string publishedMax
  313. */
  314. public function getPublishedMax()
  315. {
  316. if (array_key_exists('published-max', $this->_params)) {
  317. return $this->_params['published-max'];
  318. } else {
  319. return null;
  320. }
  321. }
  322. /**
  323. * @return string publishedMin
  324. */
  325. public function getPublishedMin()
  326. {
  327. if (array_key_exists('published-min', $this->_params)) {
  328. return $this->_params['published-min'];
  329. } else {
  330. return null;
  331. }
  332. }
  333. /**
  334. * @return string author
  335. */
  336. public function getAuthor()
  337. {
  338. if (array_key_exists('author', $this->_params)) {
  339. return $this->_params['author'];
  340. } else {
  341. return null;
  342. }
  343. }
  344. /**
  345. * @param string $value
  346. * @return Zend_Gdata_Query Provides a fluent interface
  347. */
  348. public function setCategory($value)
  349. {
  350. $this->_category = $value;
  351. return $this;
  352. }
  353. /*
  354. * @return string id
  355. */
  356. public function getCategory()
  357. {
  358. return $this->_category;
  359. }
  360. public function __get($name)
  361. {
  362. $method = 'get'.ucfirst($name);
  363. if (method_exists($this, $method)) {
  364. return call_user_func(array(&$this, $method));
  365. } else {
  366. require_once 'Zend/Gdata/App/Exception.php';
  367. throw new Zend_Gdata_App_Exception('Property ' . $name . ' does not exist');
  368. }
  369. }
  370. public function __set($name, $val)
  371. {
  372. $method = 'set'.ucfirst($name);
  373. if (method_exists($this, $method)) {
  374. return call_user_func(array(&$this, $method), $val);
  375. } else {
  376. require_once 'Zend/Gdata/App/Exception.php';
  377. throw new Zend_Gdata_App_Exception('Property ' . $name . ' does not exist');
  378. }
  379. }
  380. }