GenericEvent.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. public function offsetGet($key)
  107. {
  108. return $this->getArgument($key);
  109. }
  110. /**
  111. * ArrayAccess for argument setter.
  112. *
  113. * @param string $key Array key to set
  114. * @param mixed $value Value
  115. */
  116. public function offsetSet($key, $value)
  117. {
  118. $this->setArgument($key, $value);
  119. }
  120. /**
  121. * ArrayAccess for unset argument.
  122. *
  123. * @param string $key Array key
  124. */
  125. public function offsetUnset($key)
  126. {
  127. if ($this->hasArgument($key)) {
  128. unset($this->arguments[$key]);
  129. }
  130. }
  131. /**
  132. * ArrayAccess has argument.
  133. *
  134. * @param string $key Array key
  135. *
  136. * @return bool
  137. */
  138. public function offsetExists($key)
  139. {
  140. return $this->hasArgument($key);
  141. }
  142. /**
  143. * IteratorAggregate for iterating over the object like an array.
  144. *
  145. * @return \ArrayIterator
  146. */
  147. public function getIterator()
  148. {
  149. return new \ArrayIterator($this->arguments);
  150. }
  151. }