File indexing completed on 2024-12-22 05:36:57

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Queue
0017  * @subpackage Message
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * Class for managing queue messages
0025  *
0026  * @category   Zend
0027  * @package    Zend_Queue
0028  * @subpackage Message
0029  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0030  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0031  */
0032 class Zend_Queue_Message
0033 {
0034     /**
0035      * The data for the queue message
0036      *
0037      * @var array
0038      */
0039     protected $_data = array();
0040 
0041      /**
0042      * Connected is true if we have a reference to a live
0043      * Zend_Queue_Adapter_Abstract object.
0044      * This is false after the Message has been deserialized.
0045      *
0046      * @var boolean
0047      */
0048     protected $_connected = true;
0049 
0050     /**
0051      * Zend_Queue parent class or instance
0052      *
0053      * @var Zend_Queue
0054      */
0055     protected $_queue = null;
0056 
0057     /**
0058      * Name of the class of the Zend_Queue
0059      *
0060      * @var string
0061      */
0062     protected $_queueClass = null;
0063 
0064     /**
0065      * Constructor
0066      *
0067      * @param  array $options
0068      * @throws Zend_Queue_Exception
0069      */
0070     public function __construct(array $options = array())
0071     {
0072         if (isset($options['queue'])) {
0073             if ($options['queue'] instanceof Zend_Queue) {
0074                 $this->_queue      = $options['queue'];
0075                 $this->_queueClass = get_class($this->_queue);
0076             } else {
0077                 $result = gettype($options['queue']);
0078                 if ($result === 'object') {
0079                     $result = get_class($options['queue']);
0080                 }
0081 
0082                 // require_once 'Zend/Queue/Exception.php';
0083                 throw new Zend_Queue_Exception(
0084                     '$options[\'queue\'] = '
0085                     . $result
0086                     . ': must be instanceof Zend_Queue'
0087                 );
0088             }
0089         }
0090         if (isset($options['data'])) {
0091             if (!is_array($options['data'])) {
0092                 // require_once 'Zend/Queue/Exception.php';
0093                 throw new Zend_Queue_Exception('Data must be an array');
0094             }
0095             $this->_data = $options['data'];
0096         }
0097     }
0098 
0099     /**
0100      * Retrieve message field value
0101      *
0102      * @param  string $key The user-specified key name.
0103      * @return string      The corresponding key value.
0104      * @throws Zend_Queue_Exception if the $key is not a column in the message.
0105      */
0106     public function __get($key)
0107     {
0108         if (!array_key_exists($key, $this->_data)) {
0109             // require_once 'Zend/Queue/Exception.php';
0110             throw new Zend_Queue_Exception("Specified field \"$key\" is not in the message");
0111         }
0112         return $this->_data[$key];
0113     }
0114 
0115     /**
0116      * Set message field value
0117      *
0118      * @param  string $key   The message key.
0119      * @param  mixed  $value The value for the property.
0120      * @return void
0121      * @throws Zend_Queue_Exception
0122      */
0123     public function __set($key, $value)
0124     {
0125         if (!array_key_exists($key, $this->_data)) {
0126             // require_once 'Zend/Queue/Exception.php';
0127             throw new Zend_Queue_Exception("Specified field \"$key\" is not in the message");
0128         }
0129         $this->_data[$key] = $value;
0130     }
0131 
0132     /**
0133      * Test existence of message field
0134      *
0135      * @param  string  $key The column key.
0136      * @return boolean
0137      */
0138     public function __isset($key)
0139     {
0140         return array_key_exists($key, $this->_data);
0141     }
0142 
0143     /*
0144      * Serialize
0145      */
0146 
0147     /**
0148      * Store queue and data in serialized object
0149      *
0150      * @return array
0151      */
0152     public function __sleep()
0153     {
0154         return array('_queueClass', '_data');
0155     }
0156 
0157     /**
0158      * Setup to do on wakeup.
0159      * A de-serialized Message should not be assumed to have access to a live
0160      * queue connection, so set _connected = false.
0161      *
0162      * @return void
0163      */
0164     public function __wakeup()
0165     {
0166         $this->_connected = false;
0167     }
0168 
0169      /**
0170      * Returns the queue object, or null if this is disconnected message
0171      *
0172      * @return Zend_Queue|null
0173      */
0174     public function getQueue()
0175     {
0176         return $this->_queue;
0177     }
0178 
0179     /**
0180      * Set the queue object, to re-establish a live connection
0181      * to the queue for a Message that has been de-serialized.
0182      *
0183      * @param  Zend_Queue $queue
0184      * @return boolean
0185      */
0186     public function setQueue(Zend_Queue $queue)
0187     {
0188         $queueClass        = get_class($queue);
0189         $this->_queue      = $queue;
0190         $this->_queueClass = $queueClass;
0191         $this->_connected  = true;
0192         return true;
0193     }
0194 
0195     /**
0196      * Query the class name of the Queue object for which this
0197      * Message was created.
0198      *
0199      * @return string
0200      */
0201     public function getQueueClass()
0202     {
0203         return $this->_queueClass;
0204     }
0205 
0206     /**
0207      * Returns the column/value data as an array.
0208      *
0209      * @return array
0210      */
0211     public function toArray()
0212     {
0213         return $this->_data;
0214     }
0215 
0216     /**
0217      * Sets all data in the row from an array.
0218      *
0219      * @param  array $data
0220      * @return Zend_Queue_Message Provides a fluent interface
0221      */
0222     public function setFromArray(array $data)
0223     {
0224         foreach ($data as $columnName => $value) {
0225             $this->$columnName = $value;
0226         }
0227 
0228         return $this;
0229     }
0230 }