*prechod na novsiu verziu ZF
[sport-group.git] / library / Zend / Queue / Message / Iterator.php
blob0882b2b87cee7af27344788a590b891e6a19afa9
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
16 * @package Zend_Queue
17 * @subpackage Message
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 $
23 /**
24 * @category Zend
25 * @package Zend_Queue
26 * @subpackage Message
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
32 /**
33 * The data for the queue message
35 * @var array
37 protected $_data = array();
39 /**
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.
44 * @var boolean
46 protected $_connected = true;
48 /**
49 * Zend_Queue_Adapter_AdapterInterface parent class or instance
51 * @var Zend_Queue_Adapter_AdapterInterface
53 protected $_queue = null;
55 /**
56 * Name of the class of the Zend_Queue_Adapter_AdapterInterface object.
58 * @var string
60 protected $_queueClass = null;
62 /**
63 * Zend_Queue_Message class name
65 * @var string
67 protected $_messageClass = 'Zend_Queue_Message';
69 /**
70 * Iterator pointer.
72 * @var integer
74 protected $_pointer = 0;
76 /**
77 * Constructor
79 * @param array $options ('queue', 'messageClass', 'data'=>array());
80 * @return void
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;
88 } else {
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
125 * @return array
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.
137 * @return void
139 public function __wakeup()
141 $this->_connected = false;
145 * Returns all data as an array.
147 * Used for debugging.
149 * @return array
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();
158 return $this->_data;
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
176 * @return boolean
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.
197 * @return string
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.
213 * @return void
215 public function rewind()
217 $this->_pointer = 0;
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)
230 ? null
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.
239 * @return integer
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.
251 * @return void
253 public function next()
255 ++$this->_pointer;
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()
279 * @return integer
281 public function count()
283 return count($this->_data);