File indexing completed on 2024-05-12 06:02:28

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_EventManager
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  */
0020 
0021 // require_once 'Zend/EventManager/EventDescription.php';
0022 
0023 /**
0024  * Representation of an event
0025  *
0026  * Encapsulates the target context and parameters passed, and provides some 
0027  * behavior for interacting with the event manager.
0028  *
0029  * @category   Zend
0030  * @package    Zend_EventManager
0031  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0032  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0033  */
0034 class Zend_EventManager_Event implements Zend_EventManager_EventDescription
0035 {
0036     /**
0037      * @var string Event name
0038      */
0039     protected $name;
0040 
0041     /**
0042      * @var string|object The event target
0043      */
0044     protected $target;
0045 
0046     /**
0047      * @var array|ArrayAccess|object The event parameters
0048      */
0049     protected $params = array();
0050 
0051     /**
0052      * @var bool Whether or not to stop propagation
0053      */
0054     protected $stopPropagation = false;
0055 
0056     /**
0057      * Constructor
0058      *
0059      * Accept a target and its parameters.
0060      * 
0061      * @param  string $name Event name
0062      * @param  string|object $target 
0063      * @param  array|ArrayAccess $params 
0064      * @return void
0065      */
0066     public function __construct($name = null, $target = null, $params = null)
0067     {
0068         if (null !== $name) {
0069             $this->setName($name);
0070         }
0071 
0072         if (null !== $target) {
0073             $this->setTarget($target);
0074         }
0075 
0076         if (null !== $params) {
0077             $this->setParams($params);
0078         }
0079     }
0080 
0081     /**
0082      * Get event name
0083      * 
0084      * @return string
0085      */
0086     public function getName()
0087     {
0088         return $this->name;
0089     }
0090 
0091     /**
0092      * Get the event target
0093      *
0094      * This may be either an object, or the name of a static method.
0095      * 
0096      * @return string|object
0097      */
0098     public function getTarget()
0099     {
0100         return $this->target;
0101     }
0102 
0103     /**
0104      * Set parameters
0105      *
0106      * Overwrites parameters
0107      * 
0108      * @param  array|ArrayAccess|object $params 
0109      * @return Event
0110      */
0111     public function setParams($params)
0112     {
0113         if (!is_array($params) && !is_object($params)) {
0114             // require_once 'Zend/EventManager/Exception/InvalidArgumentException.php';
0115             throw new Zend_EventManager_Exception_InvalidArgumentException(sprintf(
0116                 'Event parameters must be an array or object; received "%s"',
0117                 (is_object($params) ? get_class($params) : gettype($params))
0118             ));
0119         }
0120 
0121         $this->params = $params;
0122         return $this;
0123     }
0124 
0125     /**
0126      * Get all parameters
0127      * 
0128      * @return array|object|ArrayAccess
0129      */
0130     public function getParams()
0131     {
0132         return $this->params;
0133     }
0134 
0135     /**
0136      * Get an individual parameter
0137      *
0138      * If the parameter does not exist, the $default value will be returned.
0139      * 
0140      * @param  string|int $name 
0141      * @param  mixed $default 
0142      * @return mixed
0143      */
0144     public function getParam($name, $default = null)
0145     {
0146         // Check in params that are arrays or implement array access
0147         if (is_array($this->params) || $this->params instanceof ArrayAccess) {
0148             if (!isset($this->params[$name])) {
0149                 return $default;
0150             }
0151 
0152             return $this->params[$name];
0153         }
0154 
0155         // Check in normal objects
0156         if (!isset($this->params->{$name})) {
0157             return $default;
0158         }
0159         return $this->params->{$name};
0160     }
0161 
0162     /**
0163      * Set the event name
0164      * 
0165      * @param  string $name 
0166      * @return Zend_EventManager_Event
0167      */
0168     public function setName($name)
0169     {
0170         $this->name = (string) $name;
0171         return $this;
0172     }
0173 
0174     /**
0175      * Set the event target/context
0176      * 
0177      * @param  null|string|object $target 
0178      * @return Zend_EventManager_Event
0179      */
0180     public function setTarget($target)
0181     {
0182         $this->target = $target;
0183         return $this;
0184     }
0185 
0186     /**
0187      * Set an individual parameter to a value
0188      * 
0189      * @param  string|int $name 
0190      * @param  mixed $value 
0191      * @return Zend_EventManager_Event
0192      */
0193     public function setParam($name, $value)
0194     {
0195         if (is_array($this->params) || $this->params instanceof ArrayAccess) {
0196             // Arrays or objects implementing array access
0197             $this->params[$name] = $value;
0198         } else {
0199             // Objects
0200             $this->params->{$name} = $value;
0201         }
0202         return $this;
0203     }
0204 
0205     /**
0206      * Stop further event propagation
0207      * 
0208      * @param  bool $flag 
0209      * @return void
0210      */
0211     public function stopPropagation($flag = true)
0212     {
0213         $this->stopPropagation = (bool) $flag;
0214     }
0215 
0216     /**
0217      * Is propagation stopped?
0218      * 
0219      * @return bool
0220      */
0221     public function propagationIsStopped()
0222     {
0223         return $this->stopPropagation;
0224     }
0225 }