Source of file Event.php
Size: 4,632 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/EventManager/Event.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\EventManager; use ArrayAccess; /** * Representation of an event * * Encapsulates the target context and parameters passed, and provides some * behavior for interacting with the event manager. */ class Event implements EventInterface { /** * @var string Event name */ protected $name; /** * @var string|object The event target */ protected $target; /** * @var array|ArrayAccess|object The event parameters */ protected $params = array(); /** * @var bool Whether or not to stop propagation */ protected $stopPropagation = false; /** * Constructor * * Accept a target and its parameters. * * @param string $name Event name * @param string|object $target * @param array|ArrayAccess $params */ public function __construct($name = null, $target = null, $params = null) { if (null !== $name) { $this->setName($name); } if (null !== $target) { $this->setTarget($target); } if (null !== $params) { $this->setParams($params); } } /** * Get event name * * @return string */ public function getName() { return $this->name; } /** * Get the event target * * This may be either an object, or the name of a static method. * * @return string|object */ public function getTarget() { return $this->target; } /** * Set parameters * * Overwrites parameters * * @param array|ArrayAccess|object $params * @return Event * @throws Exception\InvalidArgumentException */ public function setParams($params) { if (!is_array($params) && !is_object($params)) { throw new Exception\InvalidArgumentException(sprintf( 'Event parameters must be an array or object; received "%s"', gettype($params) )); } $this->params = $params; return $this; } /** * Get all parameters * * @return array|object|ArrayAccess */ public function getParams() { return $this->params; } /** * Get an individual parameter * * If the parameter does not exist, the $default value will be returned. * * @param string|int $name * @param mixed $default * @return mixed */ public function getParam($name, $default = null) { // Check in params that are arrays or implement array access if (is_array($this->params) || $this->params instanceof ArrayAccess) { if (!isset($this->params[$name])) { return $default; } return $this->params[$name]; } // Check in normal objects if (!isset($this->params->{$name})) { return $default; } return $this->params->{$name}; } /** * Set the event name * * @param string $name * @return Event */ public function setName($name) { $this->name = (string) $name; return $this; } /** * Set the event target/context * * @param null|string|object $target * @return Event */ public function setTarget($target) { $this->target = $target; return $this; } /** * Set an individual parameter to a value * * @param string|int $name * @param mixed $value * @return Event */ public function setParam($name, $value) { if (is_array($this->params) || $this->params instanceof ArrayAccess) { // Arrays or objects implementing array access $this->params[$name] = $value; } else { // Objects $this->params->{$name} = $value; } return $this; } /** * Stop further event propagation * * @param bool $flag * @return void */ public function stopPropagation($flag = true) { $this->stopPropagation = (bool) $flag; } /** * Is propagation stopped? * * @return bool */ public function propagationIsStopped() { return $this->stopPropagation; } } |