File: Iterator.php

package info (click to toggle)
php-horde-rdo 2.1.0-10
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 472 kB
  • sloc: php: 1,610; xml: 428; sql: 100; sh: 3; makefile: 2
file content (105 lines) | stat: -rw-r--r-- 2,101 bytes parent folder | download | duplicates (5)
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
<?php
/**
 * @category Horde
 * @package Rdo
 */

/**
 * Iterator for Horde_Rdo_Base objects that allows relationships and
 * decorated objects to be handled gracefully.
 *
 * @category Horde
 * @package Rdo
 */
class Horde_Rdo_Iterator implements Iterator {

    /**
     * @var Horde_Rdo_Base
     */
    private $_rdo;

    /**
     * List of keys that we'll iterator over. This is the combined
     * list of the fields, lazyFields, relationships, and
     * lazyRelationships properties from the objects Horde_Rdo_Mapper.
     */
    private $_keys = array();

    /**
     * Current index
     *
     * @var mixed
     */
    private $_index = null;

    /**
     * Are we inside the array bounds?
     *
     * @var boolean
     */
    private $_valid = false;

    /**
     * New Horde_Rdo_Iterator for iterating over Rdo objects.
     *
     * @param Horde_Rdo_Base $rdo The object to iterate over
     */
    public function __construct($rdo)
    {
        $this->_rdo = $rdo;

        $m = $rdo->getMapper();
        $this->_keys = array_merge($m->fields,
                                   $m->lazyFields,
                                   array_keys($m->relationships),
                                   array_keys($m->lazyRelationships));
    }

    /**
     * Reset to the first key.
     */
    public function rewind()
    {
        $this->_valid = (false !== reset($this->_keys));
    }

    /**
     * Return the current value.
     *
     * @return mixed The current value
     */
    public function current()
    {
        $key = $this->key();
        return $this->_rdo->$key;
    }

    /**
     * Return the current key.
     *
     * @return mixed The current key
     */
    public function key()
    {
        return current($this->_keys);
    }

    /**
     * Move to the next key in the iterator.
     */
    public function next()
    {
        $this->_valid = (false !== next($this->_keys));
    }

    /**
     * Check array bounds.
     *
     * @return boolean Inside array bounds?
     */
    public function valid()
    {
        return $this->_valid;
    }

}