Event.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace Symfony\Components\EventDispatcher;
  3. /*
  4. * This file is part of the symfony package.
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Event.
  12. *
  13. * @package symfony
  14. * @subpackage event_dispatcher
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. */
  17. class Event implements \ArrayAccess
  18. {
  19. protected $value = null;
  20. protected $processed = false;
  21. protected $subject;
  22. protected $name;
  23. protected $parameters;
  24. /**
  25. * Constructs a new Event.
  26. *
  27. * @param mixed $subject The subject
  28. * @param string $name The event name
  29. * @param array $parameters An array of parameters
  30. */
  31. public function __construct($subject, $name, $parameters = array())
  32. {
  33. $this->subject = $subject;
  34. $this->name = $name;
  35. $this->parameters = $parameters;
  36. }
  37. /**
  38. * Returns the subject.
  39. *
  40. * @return mixed The subject
  41. */
  42. public function getSubject()
  43. {
  44. return $this->subject;
  45. }
  46. /**
  47. * Returns the event name.
  48. *
  49. * @return string The event name
  50. */
  51. public function getName()
  52. {
  53. return $this->name;
  54. }
  55. /**
  56. * Sets the return value for this event.
  57. *
  58. * @param mixed $value The return value
  59. */
  60. public function setReturnValue($value)
  61. {
  62. $this->value = $value;
  63. }
  64. /**
  65. * Returns the return value.
  66. *
  67. * @return mixed The return value
  68. */
  69. public function getReturnValue()
  70. {
  71. return $this->value;
  72. }
  73. /**
  74. * Sets the processed flag.
  75. *
  76. * @param Boolean $processed The processed flag value
  77. */
  78. public function setProcessed($processed)
  79. {
  80. $this->processed = (boolean) $processed;
  81. }
  82. /**
  83. * Returns whether the event has been processed by a listener or not.
  84. *
  85. * @return Boolean true if the event has been processed, false otherwise
  86. */
  87. public function isProcessed()
  88. {
  89. return $this->processed;
  90. }
  91. /**
  92. * Returns the event parameters.
  93. *
  94. * @return array The event parameters
  95. */
  96. public function getParameters()
  97. {
  98. return $this->parameters;
  99. }
  100. /**
  101. * Returns true if the parameter exists.
  102. *
  103. * @param string $name The parameter name
  104. *
  105. * @return Boolean true if the parameter exists, false otherwise
  106. */
  107. public function hasParameter($name)
  108. {
  109. return array_key_exists($name, $this->parameters);
  110. }
  111. /**
  112. * Returns a parameter value.
  113. *
  114. * @param string $name The parameter name
  115. *
  116. * @return mixed The parameter value
  117. */
  118. public function getParameter($name)
  119. {
  120. if (!array_key_exists($name, $this->parameters))
  121. {
  122. throw new \InvalidArgumentException(sprintf('The event "%s" has no "%s" parameter.', $this->name, $name));
  123. }
  124. return $this->parameters[$name];
  125. }
  126. /**
  127. * Sets a parameter.
  128. *
  129. * @param string $name The parameter name
  130. * @param mixed $value The parameter value
  131. */
  132. public function setParameter($name, $value)
  133. {
  134. $this->parameters[$name] = $value;
  135. }
  136. /**
  137. * Returns true if the parameter exists (implements the ArrayAccess interface).
  138. *
  139. * @param string $name The parameter name
  140. *
  141. * @return Boolean true if the parameter exists, false otherwise
  142. */
  143. public function offsetExists($name)
  144. {
  145. return array_key_exists($name, $this->parameters);
  146. }
  147. /**
  148. * Returns a parameter value (implements the ArrayAccess interface).
  149. *
  150. * @param string $name The parameter name
  151. *
  152. * @return mixed The parameter value
  153. */
  154. public function offsetGet($name)
  155. {
  156. if (!array_key_exists($name, $this->parameters))
  157. {
  158. throw new \InvalidArgumentException(sprintf('The event "%s" has no "%s" parameter.', $this->name, $name));
  159. }
  160. return $this->parameters[$name];
  161. }
  162. /**
  163. * Sets a parameter (implements the ArrayAccess interface).
  164. *
  165. * @param string $name The parameter name
  166. * @param mixed $value The parameter value
  167. */
  168. public function offsetSet($name, $value)
  169. {
  170. $this->parameters[$name] = $value;
  171. }
  172. /**
  173. * Removes a parameter (implements the ArrayAccess interface).
  174. *
  175. * @param string $name The parameter name
  176. */
  177. public function offsetUnset($name)
  178. {
  179. unset($this->parameters[$name]);
  180. }
  181. }