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