File indexing completed on 2025-01-26 05:25:26
0001 <?php 0002 0003 /** 0004 * Zend Framework 0005 * 0006 * LICENSE 0007 * 0008 * This source file is subject to the new BSD license that is bundled 0009 * with this package in the file LICENSE.txt. 0010 * It is also available through the world-wide-web at this URL: 0011 * http://framework.zend.com/license/new-bsd 0012 * If you did not receive a copy of the license and are unable to 0013 * obtain it through the world-wide-web, please send an email 0014 * to license@zend.com so we can send you a copy immediately. 0015 * 0016 * @category Zend 0017 * @package Zend_Service 0018 * @subpackage Delicious 0019 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0020 * @license http://framework.zend.com/license/new-bsd New BSD License 0021 * @version $Id$ 0022 */ 0023 0024 0025 /** 0026 * List of posts retrived from the del.icio.us web service 0027 * 0028 * @category Zend 0029 * @package Zend_Service 0030 * @subpackage Delicious 0031 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0032 * @license http://framework.zend.com/license/new-bsd New BSD License 0033 */ 0034 class Zend_Service_Delicious_PostList implements Countable, Iterator, ArrayAccess 0035 { 0036 /** 0037 * @var array Array of Zend_Service_Delicious_Post 0038 */ 0039 protected $_posts = array(); 0040 0041 /** 0042 * @var Zend_Service_Delicious Service that has downloaded the post list 0043 */ 0044 protected $_service; 0045 0046 /** 0047 * @var int Iterator key 0048 */ 0049 protected $_iteratorKey = 0; 0050 0051 /** 0052 * @param Zend_Service_Delicious $service Service that has downloaded the post 0053 * @param DOMNodeList|array $posts 0054 * @return void 0055 */ 0056 public function __construct(Zend_Service_Delicious $service, $posts = null) 0057 { 0058 $this->_service = $service; 0059 if ($posts instanceof DOMNodeList) { 0060 $this->_constructFromNodeList($posts); 0061 } else if (is_array($posts)) { 0062 $this->_constructFromArray($posts); 0063 } 0064 } 0065 0066 /** 0067 * Transforms DOMNodeList to array of posts 0068 * 0069 * @param DOMNodeList $nodeList 0070 * @return void 0071 */ 0072 private function _constructFromNodeList(DOMNodeList $nodeList) 0073 { 0074 for ($i = 0; $i < $nodeList->length; $i++) { 0075 $curentNode = $nodeList->item($i); 0076 if($curentNode->nodeName == 'post') { 0077 $this->_addPost(new Zend_Service_Delicious_Post($this->_service, $curentNode)); 0078 } 0079 } 0080 } 0081 0082 /** 0083 * Transforms the Array to array of posts 0084 * 0085 * @param array $postList 0086 * @return void 0087 */ 0088 private function _constructFromArray(array $postList) 0089 { 0090 foreach ($postList as $f_post) { 0091 $this->_addPost(new Zend_Service_Delicious_SimplePost($f_post)); 0092 } 0093 } 0094 0095 /** 0096 * Add a post 0097 * 0098 * @param Zend_Service_Delicious_SimplePost $post 0099 * @return Zend_Service_Delicious_PostList 0100 */ 0101 protected function _addPost(Zend_Service_Delicious_SimplePost $post) 0102 { 0103 $this->_posts[] = $post; 0104 0105 return $this; 0106 } 0107 0108 /** 0109 * Filter list by list of tags 0110 * 0111 * @param array $tags 0112 * @return Zend_Service_Delicious_PostList 0113 */ 0114 public function withTags(array $tags) 0115 { 0116 $postList = new self($this->_service); 0117 0118 foreach ($this->_posts as $post) { 0119 if (count(array_diff($tags, $post->getTags())) == 0) { 0120 $postList->_addPost($post); 0121 } 0122 } 0123 0124 return $postList; 0125 } 0126 0127 /** 0128 * Filter list by tag 0129 * 0130 * @param string $tag 0131 * @return Zend_Service_Delicious_PostList 0132 */ 0133 public function withTag($tag) 0134 { 0135 return $this->withTags(func_get_args()); 0136 } 0137 0138 /** 0139 * Filter list by urls matching a regular expression 0140 * 0141 * @param string $regexp 0142 * @return Zend_Service_Delicious_PostList 0143 */ 0144 public function withUrl($regexp) 0145 { 0146 $postList = new self($this->_service); 0147 0148 foreach ($this->_posts as $post) { 0149 if (preg_match($regexp, $post->getUrl())) { 0150 $postList->_addPost($post); 0151 } 0152 } 0153 0154 return $postList; 0155 } 0156 0157 /** 0158 * Return number of posts 0159 * 0160 * Implement Countable::count() 0161 * 0162 * @return int 0163 */ 0164 public function count() 0165 { 0166 return count($this->_posts); 0167 } 0168 0169 /** 0170 * Return the current element 0171 * 0172 * Implement Iterator::current() 0173 * 0174 * @return Zend_Service_Delicious_SimplePost 0175 */ 0176 public function current() 0177 { 0178 return $this->_posts[$this->_iteratorKey]; 0179 } 0180 0181 /** 0182 * Return the key of the current element 0183 * 0184 * Implement Iterator::key() 0185 * 0186 * @return int 0187 */ 0188 public function key() 0189 { 0190 return $this->_iteratorKey; 0191 } 0192 0193 /** 0194 * Move forward to next element 0195 * 0196 * Implement Iterator::next() 0197 * 0198 * @return void 0199 */ 0200 public function next() 0201 { 0202 $this->_iteratorKey += 1; 0203 } 0204 0205 /** 0206 * Rewind the Iterator to the first element 0207 * 0208 * Implement Iterator::rewind() 0209 * 0210 * @return void 0211 */ 0212 public function rewind() 0213 { 0214 $this->_iteratorKey = 0; 0215 } 0216 0217 /** 0218 * Check if there is a current element after calls to rewind() or next() 0219 * 0220 * Implement Iterator::valid() 0221 * 0222 * @return bool 0223 */ 0224 public function valid() 0225 { 0226 $numItems = $this->count(); 0227 0228 if ($numItems > 0 && $this->_iteratorKey < $numItems) { 0229 return true; 0230 } else { 0231 return false; 0232 } 0233 } 0234 0235 /** 0236 * Whether the offset exists 0237 * 0238 * Implement ArrayAccess::offsetExists() 0239 * 0240 * @param int $offset 0241 * @return bool 0242 */ 0243 public function offsetExists($offset) 0244 { 0245 return ($offset < $this->count()); 0246 } 0247 0248 /** 0249 * Return value at given offset 0250 * 0251 * Implement ArrayAccess::offsetGet() 0252 * 0253 * @param int $offset 0254 * @throws OutOfBoundsException 0255 * @return Zend_Service_Delicious_SimplePost 0256 */ 0257 public function offsetGet($offset) 0258 { 0259 if ($this->offsetExists($offset)) { 0260 return $this->_posts[$offset]; 0261 } else { 0262 throw new OutOfBoundsException('Illegal index'); 0263 } 0264 } 0265 0266 /** 0267 * Throws exception because all values are read-only 0268 * 0269 * Implement ArrayAccess::offsetSet() 0270 * 0271 * @param int $offset 0272 * @param string $value 0273 * @throws Zend_Service_Delicious_Exception 0274 */ 0275 public function offsetSet($offset, $value) 0276 { 0277 /** 0278 * @see Zend_Service_Delicious_Exception 0279 */ 0280 // require_once 'Zend/Service/Delicious/Exception.php'; 0281 throw new Zend_Service_Delicious_Exception('You are trying to set read-only property'); 0282 } 0283 0284 /** 0285 * Throws exception because all values are read-only 0286 * 0287 * Implement ArrayAccess::offsetUnset() 0288 * 0289 * @param int $offset 0290 * @throws Zend_Service_Delicious_Exception 0291 */ 0292 public function offsetUnset($offset) 0293 { 0294 /** 0295 * @see Zend_Service_Delicious_Exception 0296 */ 0297 // require_once 'Zend/Service/Delicious/Exception.php'; 0298 throw new Zend_Service_Delicious_Exception('You are trying to unset read-only property'); 0299 } 0300 }