No Description

GenericEvent.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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. namespace Symfony\Component\EventDispatcher;
  11. /**
  12. * Event encapsulation class.
  13. *
  14. * Encapsulates events thus decoupling the observer from the subject they encapsulate.
  15. *
  16. * @author Drak <drak@zikula.org>
  17. */
  18. class GenericEvent extends Event implements \ArrayAccess, \IteratorAggregate
  19. {
  20. /**
  21. * Event subject.
  22. *
  23. * @var mixed usually object or callable
  24. */
  25. protected $subject;
  26. /**
  27. * Array of arguments.
  28. *
  29. * @var array
  30. */
  31. protected $arguments;
  32. /**
  33. * Encapsulate an event with $subject and $args.
  34. *
  35. * @param mixed $subject The subject of the event, usually an object.
  36. * @param array $arguments Arguments to store in the event.
  37. */
  38. public function __construct($subject = null, array $arguments = array())
  39. {
  40. $this->subject = $subject;
  41. $this->arguments = $arguments;
  42. }
  43. /**
  44. * Getter for subject property.
  45. *
  46. * @return mixed $subject The observer subject.
  47. */
  48. public function getSubject()
  49. {
  50. return $this->subject;
  51. }
  52. /**
  53. * Get argument by key.
  54. *
  55. * @param string $key Key.
  56. *
  57. * @throws \InvalidArgumentException If key is not found.
  58. *
  59. * @return mixed Contents of array key.
  60. */
  61. public function getArgument($key)
  62. {
  63. if ($this->hasArgument($key)) {
  64. return $this->arguments[$key];
  65. }
  66. throw new \InvalidArgumentException(sprintf('%s not found in %s', $key, $this->getName()));
  67. }
  68. /**
  69. * Add argument to event.
  70. *
  71. * @param string $key Argument name.
  72. * @param mixed $value Value.
  73. *
  74. * @return GenericEvent
  75. */
  76. public function setArgument($key, $value)
  77. {
  78. $this->arguments[$key] = $value;
  79. return $this;
  80. }
  81. /**
  82. * Getter for all arguments.
  83. *
  84. * @return array
  85. */
  86. public function getArguments()
  87. {
  88. return $this->arguments;
  89. }
  90. /**
  91. * Set args property.
  92. *
  93. * @param array $args Arguments.
  94. *
  95. * @return GenericEvent
  96. */
  97. public function setArguments(array $args = array())
  98. {
  99. $this->arguments = $args;
  100. return $this;
  101. }
  102. /**
  103. * Has argument.
  104. *
  105. * @param string $key Key of arguments array.
  106. *
  107. * @return bool
  108. */
  109. public function hasArgument($key)
  110. {
  111. return array_key_exists($key, $this->arguments);
  112. }
  113. /**
  114. * ArrayAccess for argument getter.
  115. *
  116. * @param string $key Array key.
  117. *
  118. * @throws \InvalidArgumentException If key does not exist in $this->args.
  119. *
  120. * @return mixed
  121. */
  122. public function offsetGet($key)
  123. {
  124. return $this->getArgument($key);
  125. }
  126. /**
  127. * ArrayAccess for argument setter.
  128. *
  129. * @param string $key Array key to set.
  130. * @param mixed $value Value.
  131. */
  132. public function offsetSet($key, $value)
  133. {
  134. $this->setArgument($key, $value);
  135. }
  136. /**
  137. * ArrayAccess for unset argument.
  138. *
  139. * @param string $key Array key.
  140. */
  141. public function offsetUnset($key)
  142. {
  143. if ($this->hasArgument($key)) {
  144. unset($this->arguments[$key]);
  145. }
  146. }
  147. /**
  148. * ArrayAccess has argument.
  149. *
  150. * @param string $key Array key.
  151. *
  152. * @return bool
  153. */
  154. public function offsetExists($key)
  155. {
  156. return $this->hasArgument($key);
  157. }
  158. /**
  159. * IteratorAggregate for iterating over the object like an array.
  160. *
  161. * @return \ArrayIterator
  162. */
  163. public function getIterator()
  164. {
  165. return new \ArrayIterator($this->arguments);
  166. }
  167. }