1: <?php
2: /**
3: * This set of classes implements a Flyweight pattern
4: * (http://en.wikipedia.org/wiki/Flyweight_pattern). Refactor/rename
5: * some based on this fact?
6: *
7: * @package Lens
8: */
9:
10: /**
11: */
12: class Horde_Lens_Iterator implements OuterIterator {
13:
14: /**
15: * The Iterator to decorate.
16: * @var Iterator
17: */
18: private $_i;
19:
20: /**
21: * The Decorator that will observe each element of the iterator.
22: * @var Horde_Lens_Interface
23: */
24: protected $_d;
25:
26: /**
27: * Constructs a decorator around an iterator using a single
28: * Horde_Lens_Interface object, which decorates the current()
29: * element of the iterator. The decorator is like a lens,
30: * decotrating one element at a time, instead of having a
31: * decorator for every element in the list.
32: *
33: * @param Iterator $i The iterator to decorate.
34: */
35: public function __construct(Iterator $i, $d = null)
36: {
37: $this->_i = $i;
38: if ($d !== null) {
39: $this->setLens($d);
40: }
41: }
42:
43: /**
44: * Set or change the Lens modifying the inner iterator. Sets the
45: * current object of the lens automatically and returns the lens.
46: */
47: public function setLens(Horde_Lens_Interface $d)
48: {
49: $this->_d = $d;
50: return $this->current();
51: }
52:
53: /**
54: * Rewind the inner iterator.
55: */
56: function rewind()
57: {
58: $this->_i->rewind();
59: }
60:
61: /**
62: * Move to next element.
63: *
64: * @return void
65: */
66: function next()
67: {
68: $this->_i->next();
69: }
70:
71: /**
72: * @return Whether more elements are available.
73: */
74: function valid()
75: {
76: return $this->_i->valid();
77: }
78:
79: /**
80: * @return The current key.
81: */
82: function key()
83: {
84: return $this->_i->key();
85: }
86:
87: /**
88: * @return The current value.
89: */
90: function current()
91: {
92: return $this->_d->decorate($this->_i->current());
93: }
94:
95: /**
96: * @return Iterator The inner iterator.
97: */
98: function getInnerIterator()
99: {
100: return $this->_i;
101: }
102:
103: /**
104: * Aggregate the inner iterator.
105: *
106: * @param func Name of method to invoke.
107: * @param params Array of parameters to pass to method.
108: */
109: function __call($func, $params)
110: {
111: return call_user_func_array(array($this->_i, $func), $params);
112: }
113:
114: }
115: