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.
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 * @version $Id: Iterator.php 17189 2009-07-27 17:41:34Z matthew $
27 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
28 * @license http://framework.zend.com/license/new-bsd New BSD License
30 class Zend_Queue_Message_Iterator
implements Iterator
, Countable
33 * The data for the queue message
37 protected $_data = array();
40 * Connected is true if we have a reference to a live
41 * Zend_Queue_Adapter_AdapterInterface object.
42 * This is false after the Message has been deserialized.
46 protected $_connected = true;
49 * Zend_Queue_Adapter_AdapterInterface parent class or instance
51 * @var Zend_Queue_Adapter_AdapterInterface
53 protected $_queue = null;
56 * Name of the class of the Zend_Queue_Adapter_AdapterInterface object.
60 protected $_queueClass = null;
63 * Zend_Queue_Message class name
67 protected $_messageClass = 'Zend_Queue_Message';
74 protected $_pointer = 0;
79 * @param array $options ('queue', 'messageClass', 'data'=>array());
82 public function __construct(array $options = array())
84 if (isset($options['queue'])) {
85 $this->_queue
= $options['queue'];
86 $this->_queueClass
= get_class($this->_queue
);
87 $this->_connected
= true;
89 $this->_connected
= false;
91 if (isset($options['messageClass'])) {
92 $this->_messageClass
= $options['messageClass'];
95 if (!is_array($options['data'])) {
96 require_once 'Zend/Queue/Exception.php';
97 throw new Zend_Queue_Exception('array optionsuration must have $options[\'data\'] = array');
100 // load the message class
101 $classname = $this->_messageClass
;
102 if (!class_exists($classname)) {
103 require_once 'Zend/Loader.php';
104 Zend_Loader
::loadClass($classname);
107 // for each of the messages
108 foreach ($options['data'] as $data) {
109 // construct the message parameters
110 $message = array('data' => $data);
112 // If queue has not been set, then use the default.
113 if (empty($message['queue'])) {
114 $message['queue'] = $this->_queue
;
117 // construct the message and add it to _data[];
118 $this->_data
[] = new $classname($message);
123 * Store queue and data in serialized object
127 public function __sleep()
129 return array('_data', '_queueClass', '_messageClass', '_pointer');
133 * Setup to do on wakeup.
134 * A de-serialized Message should not be assumed to have access to a live
135 * queue connection, so set _connected = false.
139 public function __wakeup()
141 $this->_connected
= false;
145 * Returns all data as an array.
147 * Used for debugging.
151 public function toArray()
153 // @todo This works only if we have iterated through
154 // the result set once to instantiate the messages.
155 foreach ($this->_data
as $i => $message) {
156 $this->_data
[$i] = $message->toArray();
162 * Returns the queue object, or null if this is disconnected message set
164 * @return Zend_Queue|null
166 public function getQueue()
168 return $this->_queue
;
172 * Set the queue object, to re-establish a live connection
173 * to the queue for a Message that has been de-serialized.
175 * @param Zend_Queue_Adapter_AdapterInterface $queue
177 * @throws Zend_Queue_Exception
179 public function setQueue(Zend_Queue
$queue)
181 $this->_queue
= $queue;
182 $this->_connected
= false;
184 // @todo This works only if we have iterated through
185 // the result set once to instantiate the rows.
186 foreach ($this->_data
as $i => $message) {
187 $this->_connected
= $this->_connected ||
$message->setQueue($queue);
190 return $this->_connected
;
194 * Query the class name of the Queue object for which this
195 * Message was created.
199 public function getQueueClass()
201 return $this->_queueClass
;
205 * Iterator implementation
209 * Rewind the Iterator to the first element.
210 * Similar to the reset() function for arrays in PHP.
211 * Required by interface Iterator.
215 public function rewind()
221 * Return the current element.
222 * Similar to the current() function for arrays in PHP
223 * Required by interface Iterator.
225 * @return Zend_Queue_Message current element from the collection
227 public function current()
229 return (($this->valid() === false)
231 : $this->_data
[$this->_pointer
]); // return the messages object
235 * Return the identifying key of the current element.
236 * Similar to the key() function for arrays in PHP.
237 * Required by interface Iterator.
241 public function key()
243 return $this->_pointer
;
247 * Move forward to next element.
248 * Similar to the next() function for arrays in PHP.
249 * Required by interface Iterator.
253 public function next()
259 * Check if there is a current element after calls to rewind() or next().
260 * Used to check if we've iterated to the end of the collection.
261 * Required by interface Iterator.
263 * @return bool False if there's nothing more to iterate over
265 public function valid()
267 return $this->_pointer
< count($this);
271 * Countable Implementation
275 * Returns the number of elements in the collection.
277 * Implements Countable::count()
281 public function count()
283 return count($this->_data
);