GenericEvent.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #[\ReturnTypeWillChange]
  113. public function offsetGet($key)
  114. {
  115. return $this->getArgument($key);
  116. }
  117. /**
  118. * ArrayAccess for argument setter.
  119. *
  120. * @param string $key Array key to set
  121. * @param mixed $value Value
  122. *
  123. * @return void
  124. */
  125. #[\ReturnTypeWillChange]
  126. public function offsetSet($key, $value)
  127. {
  128. $this->setArgument($key, $value);
  129. }
  130. /**
  131. * ArrayAccess for unset argument.
  132. *
  133. * @param string $key Array key
  134. *
  135. * @return void
  136. */
  137. #[\ReturnTypeWillChange]
  138. public function offsetUnset($key)
  139. {
  140. if ($this->hasArgument($key)) {
  141. unset($this->arguments[$key]);
  142. }
  143. }
  144. /**
  145. * ArrayAccess has argument.
  146. *
  147. * @param string $key Array key
  148. *
  149. * @return bool
  150. */
  151. #[\ReturnTypeWillChange]
  152. public function offsetExists($key)
  153. {
  154. return $this->hasArgument($key);
  155. }
  156. /**
  157. * IteratorAggregate for iterating over the object like an array.
  158. *
  159. * @return \ArrayIterator
  160. */
  161. #[\ReturnTypeWillChange]
  162. public function getIterator()
  163. {
  164. return new \ArrayIterator($this->arguments);
  165. }
  166. }