/lib/zend/Zend/Service/Delicious/PostList.php

https://github.com/nigeldaley/moodle · PHP · 300 lines · 111 code · 28 blank · 161 comment · 14 complexity · e5b47d5a147da6a7b0545a0cee2c586a 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_Service
  17. * @subpackage Delicious
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * List of posts retrived from the del.icio.us web service
  24. *
  25. * @category Zend
  26. * @package Zend_Service
  27. * @subpackage Delicious
  28. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Service_Delicious_PostList implements Countable, Iterator, ArrayAccess
  32. {
  33. /**
  34. * @var array Array of Zend_Service_Delicious_Post
  35. */
  36. protected $_posts = array();
  37. /**
  38. * @var Zend_Service_Delicious Service that has downloaded the post list
  39. */
  40. protected $_service;
  41. /**
  42. * @var int Iterator key
  43. */
  44. protected $_iteratorKey = 0;
  45. /**
  46. * @param Zend_Service_Delicious $service Service that has downloaded the post
  47. * @param DOMNodeList|array $posts
  48. * @return void
  49. */
  50. public function __construct(Zend_Service_Delicious $service, $posts = null)
  51. {
  52. $this->_service = $service;
  53. if ($posts instanceof DOMNodeList) {
  54. $this->_constructFromNodeList($posts);
  55. } else if (is_array($posts)) {
  56. $this->_constructFromArray($posts);
  57. }
  58. }
  59. /**
  60. * Transforms DOMNodeList to array of posts
  61. *
  62. * @param DOMNodeList $nodeList
  63. * @return void
  64. */
  65. private function _constructFromNodeList(DOMNodeList $nodeList)
  66. {
  67. for ($i = 0; $i < $nodeList->length; $i++) {
  68. $curentNode = $nodeList->item($i);
  69. if($curentNode->nodeName == 'post') {
  70. $this->_addPost(new Zend_Service_Delicious_Post($this->_service, $curentNode));
  71. }
  72. }
  73. }
  74. /**
  75. * Transforms the Array to array of posts
  76. *
  77. * @param array $postList
  78. * @return void
  79. */
  80. private function _constructFromArray(array $postList)
  81. {
  82. foreach ($postList as $f_post) {
  83. $this->_addPost(new Zend_Service_Delicious_SimplePost($f_post));
  84. }
  85. }
  86. /**
  87. * Add a post
  88. *
  89. * @param Zend_Service_Delicious_SimplePost $post
  90. * @return Zend_Service_Delicious_PostList
  91. */
  92. protected function _addPost(Zend_Service_Delicious_SimplePost $post)
  93. {
  94. $this->_posts[] = $post;
  95. return $this;
  96. }
  97. /**
  98. * Filter list by list of tags
  99. *
  100. * @param array $tags
  101. * @return Zend_Service_Delicious_PostList
  102. */
  103. public function withTags(array $tags)
  104. {
  105. $postList = new self($this->_service);
  106. foreach ($this->_posts as $post) {
  107. if (count(array_diff($tags, $post->getTags())) == 0) {
  108. $postList->_addPost($post);
  109. }
  110. }
  111. return $postList;
  112. }
  113. /**
  114. * Filter list by tag
  115. *
  116. * @param string $tag
  117. * @return Zend_Service_Delicious_PostList
  118. */
  119. public function withTag($tag)
  120. {
  121. return $this->withTags(func_get_args());
  122. }
  123. /**
  124. * Filter list by urls matching a regular expression
  125. *
  126. * @param string $regexp
  127. * @return Zend_Service_Delicious_PostList
  128. */
  129. public function withUrl($regexp)
  130. {
  131. $postList = new self($this->_service);
  132. foreach ($this->_posts as $post) {
  133. if (preg_match($regexp, $post->getUrl())) {
  134. $postList->_addPost($post);
  135. }
  136. }
  137. return $postList;
  138. }
  139. /**
  140. * Return number of posts
  141. *
  142. * Implement Countable::count()
  143. *
  144. * @return int
  145. */
  146. public function count()
  147. {
  148. return count($this->_posts);
  149. }
  150. /**
  151. * Return the current element
  152. *
  153. * Implement Iterator::current()
  154. *
  155. * @return Zend_Service_Delicious_SimplePost
  156. */
  157. public function current()
  158. {
  159. return $this->_posts[$this->_iteratorKey];
  160. }
  161. /**
  162. * Return the key of the current element
  163. *
  164. * Implement Iterator::key()
  165. *
  166. * @return int
  167. */
  168. public function key()
  169. {
  170. return $this->_iteratorKey;
  171. }
  172. /**
  173. * Move forward to next element
  174. *
  175. * Implement Iterator::next()
  176. *
  177. * @return void
  178. */
  179. public function next()
  180. {
  181. $this->_iteratorKey += 1;
  182. }
  183. /**
  184. * Rewind the Iterator to the first element
  185. *
  186. * Implement Iterator::rewind()
  187. *
  188. * @return void
  189. */
  190. public function rewind()
  191. {
  192. $this->_iteratorKey = 0;
  193. }
  194. /**
  195. * Check if there is a current element after calls to rewind() or next()
  196. *
  197. * Implement Iterator::valid()
  198. *
  199. * @return bool
  200. */
  201. public function valid()
  202. {
  203. $numItems = $this->count();
  204. if ($numItems > 0 && $this->_iteratorKey < $numItems) {
  205. return true;
  206. } else {
  207. return false;
  208. }
  209. }
  210. /**
  211. * Whether the offset exists
  212. *
  213. * Implement ArrayAccess::offsetExists()
  214. *
  215. * @param int $offset
  216. * @return bool
  217. */
  218. public function offsetExists($offset)
  219. {
  220. return ($offset < $this->count());
  221. }
  222. /**
  223. * Return value at given offset
  224. *
  225. * Implement ArrayAccess::offsetGet()
  226. *
  227. * @param int $offset
  228. * @throws OutOfBoundsException
  229. * @return Zend_Service_Delicious_SimplePost
  230. */
  231. public function offsetGet($offset)
  232. {
  233. if ($this->offsetExists($offset)) {
  234. return $this->_posts[$offset];
  235. } else {
  236. throw new OutOfBoundsException('Illegal index');
  237. }
  238. }
  239. /**
  240. * Throws exception because all values are read-only
  241. *
  242. * Implement ArrayAccess::offsetSet()
  243. *
  244. * @param int $offset
  245. * @param string $value
  246. * @throws Zend_Service_Delicious_Exception
  247. */
  248. public function offsetSet($offset, $value)
  249. {
  250. /**
  251. * @see Zend_Service_Delicious_Exception
  252. */
  253. require_once 'Zend/Service/Delicious/Exception.php';
  254. throw new Zend_Service_Delicious_Exception('You are trying to set read-only property');
  255. }
  256. /**
  257. * Throws exception because all values are read-only
  258. *
  259. * Implement ArrayAccess::offsetUnset()
  260. *
  261. * @param int $offset
  262. * @throws Zend_Service_Delicious_Exception
  263. */
  264. public function offsetUnset($offset)
  265. {
  266. /**
  267. * @see Zend_Service_Delicious_Exception
  268. */
  269. require_once 'Zend/Service/Delicious/Exception.php';
  270. throw new Zend_Service_Delicious_Exception('You are trying to unset read-only property');
  271. }
  272. }