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