GenericEvent.php 3.8 KB

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