# Source for file Iterator.php

Documentation is available at Iterator.php

1. <?php
2. /*
3. * $Id: Iterator.php 1323 2007-05-10 23:46:09Z zYne $
4. *
5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16. *
17. * This software consists of voluntary contributions made by many individuals
18. * and is licensed under the LGPL. For more information, see
19. * <http://www.phpdoctrine.com>.
20. */
21. /**
22. * Doctrine_Collection_Iterator
23. * iterates through Doctrine_Collection
24. *
25. * @package Doctrine
26. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
27. * @category Object Relational Mapping
28. * @link www.phpdoctrine.com
29. * @since 1.0
30. * @version $Revision: 1323 $
31. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
32. */
33. abstract class Doctrine_Collection_Iterator implements Iterator
34. {
35. /**
36. * @var Doctrine_Collection $collection
37. */
38. protected $collection;
39. /**
40. * @var array $keys
41. */
42. protected $keys;
43. /**
44. * @var mixed $key
45. */
46. protected $key;
47. /**
48. * @var integer $index
49. */
50. protected $index;
51. /**
52. * @var integer $count
53. */
54. protected $count;
56. /**
57. * constructor
58. * @var Doctrine_Collection $collection
59. */
60. public function __construct($collection)
61. {
62. $this->collection = $collection;
63. $this->keys = $this->collection->getKeys();
64. $this->count = $this->collection->count();
65. }
66. /**
67. * rewinds the iterator
68. *
69. * @return void
70. */
71. public function rewind()
72. {
73. $this->index = 0;
74. $i = $this->index;
75. if (isset($this->keys[$i])) {
76. $this->key = $this->keys[$i];
77. }
78. }
80. /**
81. * returns the current key
82. *
83. * @return integer
84. */
85. public function key()
86. {
87. return $this->key;
88. }
89. /**
90. * returns the current record
91. *
92. * @return Doctrine_Record
93. */
94. public function current()
95. {
96. return $this->collection->get($this->key);
97. }
98. /**
99. * advances the internal pointer
100. *
101. * @return void
102. */
103. public function next()
104. {
105. $this->index++;
106. $i = $this->index;
107. if (isset($this->keys[$i])) {
108. $this->key = $this->keys[$i];
109. }
110. }
111. }