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