ArrayCollection.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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-2009 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-2009 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 ArrayAccess , IteratorAggregate, Countable
  32. {
  33. /**
  34. * Data in the ArrayCollection
  35. * @var ArrayObject
  36. */
  37. protected $_source;
  38. /**
  39. * Constructor will build an arracollection from the data supplied
  40. * @param array data
  41. */
  42. public function __construct($data = null)
  43. {
  44. $this->_source = new ArrayObject();
  45. if (!is_null($data)) {
  46. $this->loadSource($data);
  47. }
  48. }
  49. /**
  50. * Value is append to the last element of the ArrayCollection
  51. * @param misc new value to be added to the ArrayCollection
  52. */
  53. public function append($value)
  54. {
  55. if(!is_null($value))
  56. {
  57. $this->_source->append($value);
  58. }
  59. }
  60. /**
  61. * Allow name value pairs to be added to the ArrayCollection
  62. * @param mixed name pair
  63. * @param mixed value pair
  64. */
  65. public function __set($name, $value)
  66. {
  67. if($name == 'externalizedData') {
  68. $this->loadSource($value);
  69. } else {
  70. $this->_source[] = array($name => $value);
  71. }
  72. }
  73. /**
  74. * Get the number of elements in the collection
  75. * @return integer Count
  76. */
  77. public function count()
  78. {
  79. return count($this->_source);
  80. }
  81. /**
  82. * Check if the specified offset exists exists for the key supplied.
  83. *
  84. * @param mixed $offset
  85. * @return bool true if it exists.
  86. */
  87. function offsetExists($offset) {
  88. return isset($this->_source[$offset]);
  89. }
  90. /**
  91. * Value of given offset
  92. *
  93. * @param mixed $offset
  94. * @return mixed
  95. */
  96. function offsetGet($offset) {
  97. return $this->_source[$offset];
  98. }
  99. /**
  100. * Update or add a new value based on the on the offset key. Careful as this will overwrite any existing propery by the same offset id
  101. *
  102. * @param mixed Offset to modify
  103. * @param mixed New value for the offset.
  104. */
  105. function offsetSet($offset,$value) {
  106. if (!is_null($offset)) {
  107. $this->_source[$offset] = $value;
  108. }
  109. }
  110. /**
  111. * Offest to delete from the collection
  112. *
  113. * @param mixed $offset
  114. */
  115. function offsetUnset($offset) {
  116. unset($this->_source[$offset]);
  117. }
  118. /**
  119. * Return the source of the iterator
  120. * @return ArrayObject
  121. */
  122. function getIterator()
  123. {
  124. return $this->_source;
  125. }
  126. /**
  127. * Builds an Array into an ArrayCollection and handles Zend_DB_Table
  128. *
  129. * @param array data to be added to the collection
  130. * @todo Should fire an exception if the data is not an array
  131. */
  132. private function loadSource($data)
  133. {
  134. if (is_array($data)) {
  135. foreach($data as $row) {
  136. if ($row instanceof Zend_Db_Table_Row_Abstract) {
  137. $row = $row->toArray();
  138. }
  139. if (is_object($row)) {
  140. $this->_source[] = $row;
  141. } else if (is_array($row)) {
  142. $source_row = array();
  143. foreach($row as $colkey => $colvalue) {
  144. $source_row[$colkey] = $colvalue;
  145. }
  146. if ($source_row) {
  147. $this->_source[] = $source_row;
  148. }
  149. }
  150. }
  151. } else {
  152. require_once 'Zend/Amf/Server/Exception.php';
  153. throw new Zend_Amf_Server_Exception("Could not load source data into an ArrayCollection must be an Array");
  154. }
  155. }
  156. }