Iterator.class.php 1.72 KB
Newer Older
doctrine's avatar
doctrine committed
1 2 3 4
<?php
/**
 * Doctrine_Iterator
 * iterates through Doctrine_Collection
5 6 7 8
 *
 * @package     Doctrine ORM
 * @url         www.phpdoctrine.com
 * @license     LGPL
doctrine's avatar
doctrine committed
9 10 11
 */
abstract class Doctrine_Iterator implements Iterator {
    /**
12
     * @var Doctrine_Collection $collection         
doctrine's avatar
doctrine committed
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
     */
    protected $collection;
    /**
     * @var array $keys
     */
    protected $keys;
    /**
     * @var mixed $key
     */
    protected $key;
    /**
     * @var integer $index
     */
    protected $index;
    /**
     * @var integer $count
     */
    protected $count;

    /**
     * constructor
     * @var Doctrine_Collection $collection
     */
    public function __construct(Doctrine_Collection $collection) {
        $this->collection = $collection;
        $this->keys       = $this->collection->getKeys();
        $this->count      = $this->collection->count();
    }
    /**
42 43
     * rewinds the iterator
     *
doctrine's avatar
doctrine committed
44 45 46 47 48 49 50 51 52 53
     * @return void
     */
    public function rewind() {
        $this->index = 0;
        $i = $this->index;
        if(isset($this->keys[$i]))
            $this->key   = $this->keys[$i];
    }

    /**
54 55 56
     * returns the current key
     *
     * @return integer
doctrine's avatar
doctrine committed
57 58 59 60 61
     */
    public function key() {
        return $this->key;
    }
    /**
62 63 64
     * returns the current record
     *
     * @return Doctrine_Record
doctrine's avatar
doctrine committed
65 66 67 68 69
     */
    public function current() {
        return $this->collection->get($this->key);
    }
    /**
70 71
     * advances the internal pointer
     *
doctrine's avatar
doctrine committed
72 73 74 75 76 77 78 79 80 81 82 83
     * @return void
     */
    public function next() {
        $this->index++;
        $i = $this->index;
        if(isset($this->keys[$i]))
            $this->key   = $this->keys[$i];
    }
}


?>