GenericEvent.php 3.7 KB

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