ArrayCollection.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Amf
  17. * @subpackage Value
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * Type encapsulating Flex ArrayCollection
  23. *
  24. * Corresponds to flex.messaging.io.ArrayCollection
  25. *
  26. * @package Zend_Amf
  27. * @subpackage Value
  28. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Amf_Value_Messaging_ArrayCollection implements Iterator
  32. {
  33. /**
  34. * Current index of source
  35. * @var integer
  36. */
  37. protected $_sourceIndex = 0;
  38. /**
  39. * Data in the ArrayCollection
  40. * @var unknown_type
  41. */
  42. protected $_source;
  43. /**
  44. * Constructor will build an arracollection from the data supplied
  45. * @param array data
  46. */
  47. public function __construct($data = null)
  48. {
  49. $this->_source = array();
  50. if (!is_null($data)) {
  51. $this->loadSource($data);
  52. }
  53. }
  54. /**
  55. * Get the number of elements in the collection
  56. * @return integer Count
  57. */
  58. public function count()
  59. {
  60. return count($this->_source);
  61. }
  62. /**
  63. * Reset the iterator to the beginning of the data
  64. * @return void
  65. */
  66. public function rewind()
  67. {
  68. $this->_sourceIndex = 0;
  69. }
  70. /**
  71. * Returns the data at the current index in the collection
  72. *
  73. * @return mixed the current row or null if no rows exist.
  74. */
  75. public function current()
  76. {
  77. if(isset($this->_source[$this->_sourceIndex])) {
  78. return $this->_source[$this->_sourceIndex];
  79. } else {
  80. return null;
  81. }
  82. }
  83. /**
  84. * Returns the collections current index number
  85. * @return mixed the current row number (starts at 0), null if there is no data
  86. */
  87. public function key()
  88. {
  89. return $this->_sourceIndex;
  90. }
  91. /**
  92. * @return mixed the next row number collection, or null if not another row.
  93. */
  94. public function next()
  95. {
  96. return ++$this->_sourceIndex;
  97. }
  98. /**
  99. * checks if the iterator is valid
  100. * @return boolean is the iterator valid
  101. */
  102. public function valid()
  103. {
  104. return 0 <= $this->_sourceIndex && $this->_sourceIndex < $this->count();
  105. }
  106. /**
  107. * Allow name value pairs to be added to the ArrayCollection
  108. * @param mixed name pair
  109. * @param mixed value pair
  110. */
  111. public function __set($name, $value)
  112. {
  113. if($name == 'externalizedData') {
  114. $this->loadSource($value);
  115. } else {
  116. $this->_source[] = array($name => $value);
  117. }
  118. }
  119. /**
  120. * Builds an Array into an ArrayCollection and handles Zend_DB_Table
  121. *
  122. * @param array data to be added to the collection
  123. * @todo Should fire an exception if the data is not an array
  124. */
  125. private function loadSource($data)
  126. {
  127. if (is_array($data)) {
  128. foreach($data as $row) {
  129. if ($row instanceof Zend_Db_Table_Row_Abstract) {
  130. $row = $row->toArray();
  131. }
  132. if (is_object($row)) {
  133. $this->_source[] = $row;
  134. } else if (is_array($row)) {
  135. $source_row = array();
  136. foreach($row as $colkey => $colvalue) {
  137. $source_row[$colkey] = $colvalue;
  138. }
  139. if ($source_row) {
  140. $this->_source[] = $source_row;
  141. }
  142. }
  143. }
  144. }
  145. }
  146. }