Guest User

Untitled

a guest
Sep 1st, 2010
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.18 KB | None | 0 0
  1. <?php
  2.  
  3. class Core_View_Type_Array implements ArrayAccess, Countable, IteratorAggregate {
  4.  
  5.     /* ... */
  6.  
  7.     protected $iterator = null;
  8.  
  9.     /* ... */
  10.  
  11.     public function getIterator($isNew = true) {
  12.         if ($isNew || is_null($this->iterator)) {
  13.             $this->iterator = new Core_View_Type_Array_Iterator(clone $this);
  14.         }
  15.         return $this->iterator;
  16.     }
  17.  
  18.     /* ... */
  19.  
  20. }
  21.  
  22. class Core_View_Type_Array_Iterator implements Iterator {
  23.  
  24.     protected $array;
  25.     protected $keys;
  26.     protected $current;
  27.     protected $length;
  28.  
  29.     public function __construct(ArrayAccess $array) {
  30.         $this->array = $array;
  31.         $this->keys = $this->array->keys();
  32.         $this->length = $this->array->count();
  33.         $this->rewind();
  34.     }
  35.  
  36.     public function current() {
  37.         return $this->array->offsetGet($this->keys[$this->current]);
  38.     }
  39.  
  40.     public function key() {
  41.         return $this->keys[$this->current];
  42.     }
  43.  
  44.     public function next() {
  45.         $this->current++;
  46.     }
  47.  
  48.     public function rewind() {
  49.         $this->current = 0;
  50.     }
  51.  
  52.     public function valid() {
  53.         return $this->current < $this->length;
  54.     }
  55.  
  56.     public function isFirst() {
  57.         return $this->current == 0;
  58.     }
  59.    
  60.     public function isLast() {
  61.         return $this->current + 1 == $this->length;
  62.     }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment