Message.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Class for managing queue messages
  24. *
  25. * @category Zend
  26. * @package Zend_Queue
  27. * @subpackage Message
  28. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Queue_Message
  32. {
  33. /**
  34. * The data for the queue message
  35. *
  36. * @var array
  37. */
  38. protected $_data = array();
  39. /**
  40. * Connected is true if we have a reference to a live
  41. * Zend_Queue_Adapter_Abstract object.
  42. * This is false after the Message has been deserialized.
  43. *
  44. * @var boolean
  45. */
  46. protected $_connected = true;
  47. /**
  48. * Zend_Queue parent class or instance
  49. *
  50. * @var Zend_Queue
  51. */
  52. protected $_queue = null;
  53. /**
  54. * Name of the class of the Zend_Queue
  55. *
  56. * @var string
  57. */
  58. protected $_queueClass = null;
  59. /**
  60. * Constructor
  61. *
  62. * @param array $options
  63. * @throws Zend_Queue_Exception
  64. */
  65. public function __construct(array $options = array())
  66. {
  67. if (isset($options['queue'])) {
  68. if ($options['queue'] instanceof Zend_Queue) {
  69. $this->_queue = $options['queue'];
  70. $this->_queueClass = get_class($this->_queue);
  71. } else {
  72. $result = gettype($options['queue']);
  73. if ($result === 'object') {
  74. $result = get_class($options['queue']);
  75. }
  76. require_once 'Zend/Queue/Exception.php';
  77. throw new Zend_Queue_Exception(
  78. '$options[\'queue\'] = '
  79. . $result
  80. . ': must be instanceof Zend_Queue'
  81. );
  82. }
  83. }
  84. if (isset($options['data'])) {
  85. if (!is_array($options['data'])) {
  86. require_once 'Zend/Queue/Exception.php';
  87. throw new Zend_Queue_Exception('Data must be an array');
  88. }
  89. $this->_data = $options['data'];
  90. }
  91. }
  92. /**
  93. * Retrieve message field value
  94. *
  95. * @param string $key The user-specified key name.
  96. * @return string The corresponding key value.
  97. * @throws Zend_Queue_Exception if the $key is not a column in the message.
  98. */
  99. public function __get($key)
  100. {
  101. if (!array_key_exists($key, $this->_data)) {
  102. require_once 'Zend/Queue/Exception.php';
  103. throw new Zend_Queue_Exception("Specified field \"$key\" is not in the message");
  104. }
  105. return $this->_data[$key];
  106. }
  107. /**
  108. * Set message field value
  109. *
  110. * @param string $key The message key.
  111. * @param mixed $value The value for the property.
  112. * @return void
  113. * @throws Zend_Queue_Exception
  114. */
  115. public function __set($key, $value)
  116. {
  117. if (!array_key_exists($key, $this->_data)) {
  118. require_once 'Zend/Queue/Exception.php';
  119. throw new Zend_Queue_Exception("Specified field \"$key\" is not in the message");
  120. }
  121. $this->_data[$key] = $value;
  122. }
  123. /**
  124. * Test existence of message field
  125. *
  126. * @param string $key The column key.
  127. * @return boolean
  128. */
  129. public function __isset($key)
  130. {
  131. return array_key_exists($key, $this->_data);
  132. }
  133. /*
  134. * Serialize
  135. */
  136. /**
  137. * Store queue and data in serialized object
  138. *
  139. * @return array
  140. */
  141. public function __sleep()
  142. {
  143. return array('_queueClass', '_data');
  144. }
  145. /**
  146. * Setup to do on wakeup.
  147. * A de-serialized Message should not be assumed to have access to a live
  148. * queue connection, so set _connected = false.
  149. *
  150. * @return void
  151. */
  152. public function __wakeup()
  153. {
  154. $this->_connected = false;
  155. }
  156. /**
  157. * Returns the queue object, or null if this is disconnected message
  158. *
  159. * @return Zend_Queue|null
  160. */
  161. public function getQueue()
  162. {
  163. return $this->_queue;
  164. }
  165. /**
  166. * Set the queue object, to re-establish a live connection
  167. * to the queue for a Message that has been de-serialized.
  168. *
  169. * @param Zend_Queue $queue
  170. * @return boolean
  171. */
  172. public function setQueue(Zend_Queue $queue)
  173. {
  174. $queueClass = get_class($queue);
  175. $this->_queue = $queue;
  176. $this->_queueClass = $queueClass;
  177. $this->_connected = true;
  178. return true;
  179. }
  180. /**
  181. * Query the class name of the Queue object for which this
  182. * Message was created.
  183. *
  184. * @return string
  185. */
  186. public function getQueueClass()
  187. {
  188. return $this->_queueClass;
  189. }
  190. /**
  191. * Returns the column/value data as an array.
  192. *
  193. * @return array
  194. */
  195. public function toArray()
  196. {
  197. return $this->_data;
  198. }
  199. /**
  200. * Sets all data in the row from an array.
  201. *
  202. * @param array $data
  203. * @return Zend_Queue_Message Provides a fluent interface
  204. */
  205. public function setFromArray(array $data)
  206. {
  207. foreach ($data as $columnName => $value) {
  208. $this->$columnName = $value;
  209. }
  210. return $this;
  211. }
  212. }