GenericEvent.php 3.4 KB

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