1: <?php
2: /**
3: * @category Horde
4: * @package Rdo
5: */
6:
7: /**
8: * Iterator for Horde_Rdo_Base objects that allows relationships and
9: * decorated objects to be handled gracefully.
10: *
11: * @category Horde
12: * @package Rdo
13: */
14: class Horde_Rdo_Iterator implements Iterator {
15:
16: /**
17: * @var Horde_Rdo_Base
18: */
19: private $_rdo;
20:
21: /**
22: * List of keys that we'll iterator over. This is the combined
23: * list of the fields, lazyFields, relationships, and
24: * lazyRelationships properties from the objects Horde_Rdo_Mapper.
25: */
26: private $_keys = array();
27:
28: /**
29: * Current index
30: *
31: * @var mixed
32: */
33: private $_index = null;
34:
35: /**
36: * Are we inside the array bounds?
37: *
38: * @var boolean
39: */
40: private $_valid = false;
41:
42: /**
43: * New Horde_Rdo_Iterator for iterating over Rdo objects.
44: *
45: * @param Horde_Rdo_Base $rdo The object to iterate over
46: */
47: public function __construct($rdo)
48: {
49: $this->_rdo = $rdo;
50:
51: $m = $rdo->getMapper();
52: $this->_keys = array_merge($m->fields,
53: $m->lazyFields,
54: array_keys($m->relationships),
55: array_keys($m->lazyRelationships));
56: }
57:
58: /**
59: * Reset to the first key.
60: */
61: public function rewind()
62: {
63: $this->_valid = (false !== reset($this->_keys));
64: }
65:
66: /**
67: * Return the current value.
68: *
69: * @return mixed The current value
70: */
71: public function current()
72: {
73: $key = $this->key();
74: return $this->_rdo->$key;
75: }
76:
77: /**
78: * Return the current key.
79: *
80: * @return mixed The current key
81: */
82: public function key()
83: {
84: return current($this->_keys);
85: }
86:
87: /**
88: * Move to the next key in the iterator.
89: */
90: public function next()
91: {
92: $this->_valid = (false !== next($this->_keys));
93: }
94:
95: /**
96: * Check array bounds.
97: *
98: * @return boolean Inside array bounds?
99: */
100: public function valid()
101: {
102: return $this->_valid;
103: }
104:
105: }
106: