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