Event.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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_EventManager
  17. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. require_once 'Zend/EventManager/EventDescription.php';
  21. /**
  22. * Representation of an event
  23. *
  24. * Encapsulates the target context and parameters passed, and provides some
  25. * behavior for interacting with the event manager.
  26. *
  27. * @category Zend
  28. * @package Zend_EventManager
  29. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_EventManager_Event implements Zend_EventManager_EventDescription
  33. {
  34. /**
  35. * @var string Event name
  36. */
  37. protected $name;
  38. /**
  39. * @var string|object The event target
  40. */
  41. protected $target;
  42. /**
  43. * @var array|ArrayAccess|object The event parameters
  44. */
  45. protected $params = array();
  46. /**
  47. * @var bool Whether or not to stop propagation
  48. */
  49. protected $stopPropagation = false;
  50. /**
  51. * Constructor
  52. *
  53. * Accept a target and its parameters.
  54. *
  55. * @param string $name Event name
  56. * @param string|object $target
  57. * @param array|ArrayAccess $params
  58. * @return void
  59. */
  60. public function __construct($name = null, $target = null, $params = null)
  61. {
  62. if (null !== $name) {
  63. $this->setName($name);
  64. }
  65. if (null !== $target) {
  66. $this->setTarget($target);
  67. }
  68. if (null !== $params) {
  69. $this->setParams($params);
  70. }
  71. }
  72. /**
  73. * Get event name
  74. *
  75. * @return string
  76. */
  77. public function getName()
  78. {
  79. return $this->name;
  80. }
  81. /**
  82. * Get the event target
  83. *
  84. * This may be either an object, or the name of a static method.
  85. *
  86. * @return string|object
  87. */
  88. public function getTarget()
  89. {
  90. return $this->target;
  91. }
  92. /**
  93. * Set parameters
  94. *
  95. * Overwrites parameters
  96. *
  97. * @param array|ArrayAccess|object $params
  98. * @return Event
  99. */
  100. public function setParams($params)
  101. {
  102. if (!is_array($params) && !is_object($params)) {
  103. require_once 'Zend/EventManager/Exception/InvalidArgumentException.php';
  104. throw new Zend_EventManager_Exception_InvalidArgumentException(sprintf(
  105. 'Event parameters must be an array or object; received "%s"',
  106. (is_object($params) ? get_class($params) : gettype($params))
  107. ));
  108. }
  109. $this->params = $params;
  110. return $this;
  111. }
  112. /**
  113. * Get all parameters
  114. *
  115. * @return array|object|ArrayAccess
  116. */
  117. public function getParams()
  118. {
  119. return $this->params;
  120. }
  121. /**
  122. * Get an individual parameter
  123. *
  124. * If the parameter does not exist, the $default value will be returned.
  125. *
  126. * @param string|int $name
  127. * @param mixed $default
  128. * @return mixed
  129. */
  130. public function getParam($name, $default = null)
  131. {
  132. // Check in params that are arrays or implement array access
  133. if (is_array($this->params) || $this->params instanceof ArrayAccess) {
  134. if (!isset($this->params[$name])) {
  135. return $default;
  136. }
  137. return $this->params[$name];
  138. }
  139. // Check in normal objects
  140. if (!isset($this->params->{$name})) {
  141. return $default;
  142. }
  143. return $this->params->{$name};
  144. }
  145. /**
  146. * Set the event name
  147. *
  148. * @param string $name
  149. * @return Zend_EventManager_Event
  150. */
  151. public function setName($name)
  152. {
  153. $this->name = (string) $name;
  154. return $this;
  155. }
  156. /**
  157. * Set the event target/context
  158. *
  159. * @param null|string|object $target
  160. * @return Zend_EventManager_Event
  161. */
  162. public function setTarget($target)
  163. {
  164. $this->target = $target;
  165. return $this;
  166. }
  167. /**
  168. * Set an individual parameter to a value
  169. *
  170. * @param string|int $name
  171. * @param mixed $value
  172. * @return Zend_EventManager_Event
  173. */
  174. public function setParam($name, $value)
  175. {
  176. if (is_array($this->params) || $this->params instanceof ArrayAccess) {
  177. // Arrays or objects implementing array access
  178. $this->params[$name] = $value;
  179. } else {
  180. // Objects
  181. $this->params->{$name} = $value;
  182. }
  183. return $this;
  184. }
  185. /**
  186. * Stop further event propagation
  187. *
  188. * @param bool $flag
  189. * @return void
  190. */
  191. public function stopPropagation($flag = true)
  192. {
  193. $this->stopPropagation = (bool) $flag;
  194. }
  195. /**
  196. * Is propagation stopped?
  197. *
  198. * @return bool
  199. */
  200. public function propagationIsStopped()
  201. {
  202. return $this->stopPropagation;
  203. }
  204. }