PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/libraries/3rdparty/Zend/Queue/Message/Iterator.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 285 lines | 97 code | 28 blank | 160 comment | 7 complexity | f7a2ba2cc041640d2dc5512bdedfcf2d MD5 | raw file
  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_Queue
  17. * @subpackage Message
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Iterator.php 24593 2012-01-05 20:35:02Z matthew $
  21. */
  22. /**
  23. * @category Zend
  24. * @package Zend_Queue
  25. * @subpackage Message
  26. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Queue_Message_Iterator implements Iterator, Countable
  30. {
  31. /**
  32. * The data for the queue message
  33. *
  34. * @var array
  35. */
  36. protected $_data = array();
  37. /**
  38. * Connected is true if we have a reference to a live
  39. * Zend_Queue_Adapter_AdapterInterface object.
  40. * This is false after the Message has been deserialized.
  41. *
  42. * @var boolean
  43. */
  44. protected $_connected = true;
  45. /**
  46. * Zend_Queue_Adapter_AdapterInterface parent class or instance
  47. *
  48. * @var Zend_Queue_Adapter_AdapterInterface
  49. */
  50. protected $_queue = null;
  51. /**
  52. * Name of the class of the Zend_Queue_Adapter_AdapterInterface object.
  53. *
  54. * @var string
  55. */
  56. protected $_queueClass = null;
  57. /**
  58. * Zend_Queue_Message class name
  59. *
  60. * @var string
  61. */
  62. protected $_messageClass = 'Zend_Queue_Message';
  63. /**
  64. * Iterator pointer.
  65. *
  66. * @var integer
  67. */
  68. protected $_pointer = 0;
  69. /**
  70. * Constructor
  71. *
  72. * @param array $options ('queue', 'messageClass', 'data'=>array());
  73. * @return void
  74. */
  75. public function __construct(array $options = array())
  76. {
  77. if (isset($options['queue'])) {
  78. $this->_queue = $options['queue'];
  79. $this->_queueClass = get_class($this->_queue);
  80. $this->_connected = true;
  81. } else {
  82. $this->_connected = false;
  83. }
  84. if (isset($options['messageClass'])) {
  85. $this->_messageClass = $options['messageClass'];
  86. }
  87. if (!is_array($options['data'])) {
  88. require_once 'Zend/Queue/Exception.php';
  89. throw new Zend_Queue_Exception('array optionsuration must have $options[\'data\'] = array');
  90. }
  91. // load the message class
  92. $classname = $this->_messageClass;
  93. if (!class_exists($classname)) {
  94. require_once 'Zend/Loader.php';
  95. Zend_Loader::loadClass($classname);
  96. }
  97. // for each of the messages
  98. foreach ($options['data'] as $data) {
  99. // construct the message parameters
  100. $message = array('data' => $data);
  101. // If queue has not been set, then use the default.
  102. if (empty($message['queue'])) {
  103. $message['queue'] = $this->_queue;
  104. }
  105. // construct the message and add it to _data[];
  106. $this->_data[] = new $classname($message);
  107. }
  108. }
  109. /**
  110. * Store queue and data in serialized object
  111. *
  112. * @return array
  113. */
  114. public function __sleep()
  115. {
  116. return array('_data', '_queueClass', '_messageClass', '_pointer');
  117. }
  118. /**
  119. * Setup to do on wakeup.
  120. * A de-serialized Message should not be assumed to have access to a live
  121. * queue connection, so set _connected = false.
  122. *
  123. * @return void
  124. */
  125. public function __wakeup()
  126. {
  127. $this->_connected = false;
  128. }
  129. /**
  130. * Returns all data as an array.
  131. *
  132. * Used for debugging.
  133. *
  134. * @return array
  135. */
  136. public function toArray()
  137. {
  138. // @todo This works only if we have iterated through
  139. // the result set once to instantiate the messages.
  140. foreach ($this->_data as $i => $message) {
  141. $this->_data[$i] = $message->toArray();
  142. }
  143. return $this->_data;
  144. }
  145. /**
  146. * Returns the queue object, or null if this is disconnected message set
  147. *
  148. * @return Zend_Queue|null
  149. */
  150. public function getQueue()
  151. {
  152. return $this->_queue;
  153. }
  154. /**
  155. * Set the queue object, to re-establish a live connection
  156. * to the queue for a Message that has been de-serialized.
  157. *
  158. * @param Zend_Queue_Adapter_AdapterInterface $queue
  159. * @return boolean
  160. * @throws Zend_Queue_Exception
  161. */
  162. public function setQueue(Zend_Queue $queue)
  163. {
  164. $this->_queue = $queue;
  165. $this->_connected = false;
  166. // @todo This works only if we have iterated through
  167. // the result set once to instantiate the rows.
  168. foreach ($this->_data as $i => $message) {
  169. $this->_connected = $this->_connected || $message->setQueue($queue);
  170. }
  171. return $this->_connected;
  172. }
  173. /**
  174. * Query the class name of the Queue object for which this
  175. * Message was created.
  176. *
  177. * @return string
  178. */
  179. public function getQueueClass()
  180. {
  181. return $this->_queueClass;
  182. }
  183. /*
  184. * Iterator implementation
  185. */
  186. /**
  187. * Rewind the Iterator to the first element.
  188. * Similar to the reset() function for arrays in PHP.
  189. * Required by interface Iterator.
  190. *
  191. * @return void
  192. */
  193. public function rewind()
  194. {
  195. $this->_pointer = 0;
  196. }
  197. /**
  198. * Return the current element.
  199. * Similar to the current() function for arrays in PHP
  200. * Required by interface Iterator.
  201. *
  202. * @return Zend_Queue_Message current element from the collection
  203. */
  204. public function current()
  205. {
  206. return (($this->valid() === false)
  207. ? null
  208. : $this->_data[$this->_pointer]); // return the messages object
  209. }
  210. /**
  211. * Return the identifying key of the current element.
  212. * Similar to the key() function for arrays in PHP.
  213. * Required by interface Iterator.
  214. *
  215. * @return integer
  216. */
  217. public function key()
  218. {
  219. return $this->_pointer;
  220. }
  221. /**
  222. * Move forward to next element.
  223. * Similar to the next() function for arrays in PHP.
  224. * Required by interface Iterator.
  225. *
  226. * @return void
  227. */
  228. public function next()
  229. {
  230. ++$this->_pointer;
  231. }
  232. /**
  233. * Check if there is a current element after calls to rewind() or next().
  234. * Used to check if we've iterated to the end of the collection.
  235. * Required by interface Iterator.
  236. *
  237. * @return bool False if there's nothing more to iterate over
  238. */
  239. public function valid()
  240. {
  241. return $this->_pointer < count($this);
  242. }
  243. /*
  244. * Countable Implementation
  245. */
  246. /**
  247. * Returns the number of elements in the collection.
  248. *
  249. * Implements Countable::count()
  250. *
  251. * @return integer
  252. */
  253. public function count()
  254. {
  255. return count($this->_data);
  256. }
  257. }