GenericEvent.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019 Arthur Schiwon <blizzz@arthur-schiwon.de>
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCP\EventDispatcher;
  25. use ArrayAccess;
  26. use ArrayIterator;
  27. use InvalidArgumentException;
  28. use IteratorAggregate;
  29. use Traversable;
  30. use function array_key_exists;
  31. /**
  32. * Class GenericEvent
  33. *
  34. * convenience reimplementation of \Symfony\Component\GenericEvent against
  35. * \OCP\EventDispatcher\Event
  36. *
  37. * @package OCP\EventDispatcher
  38. * @since 18.0.0
  39. */
  40. class GenericEvent extends Event implements ArrayAccess, IteratorAggregate {
  41. protected $subject;
  42. protected $arguments;
  43. /**
  44. * Encapsulate an event with $subject and $args.
  45. *
  46. * @since 18.0.0
  47. */
  48. public function __construct($subject = null, array $arguments = []) {
  49. $this->subject = $subject;
  50. $this->arguments = $arguments;
  51. }
  52. /**
  53. * Getter for subject property.
  54. *
  55. * @since 18.0.0
  56. */
  57. public function getSubject() {
  58. return $this->subject;
  59. }
  60. /**
  61. * Get argument by key.
  62. *
  63. * @throws InvalidArgumentException if key is not found
  64. * @since 18.0.0
  65. */
  66. public function getArgument(string $key) {
  67. if ($this->hasArgument($key)) {
  68. return $this->arguments[$key];
  69. }
  70. throw new InvalidArgumentException(sprintf('Argument "%s" not found.', $key));
  71. }
  72. /**
  73. * Add argument to event.
  74. *
  75. * @since 18.0.0
  76. */
  77. public function setArgument($key, $value): GenericEvent {
  78. $this->arguments[$key] = $value;
  79. return $this;
  80. }
  81. /**
  82. * Getter for all arguments.
  83. *
  84. * @since 18.0.0
  85. */
  86. public function getArguments(): array {
  87. return $this->arguments;
  88. }
  89. /**
  90. * Set args property.
  91. *
  92. * @since 18.0.0
  93. */
  94. public function setArguments(array $args = []): GenericEvent {
  95. $this->arguments = $args;
  96. return $this;
  97. }
  98. /**
  99. * Has argument.
  100. *
  101. * @since 18.0.0
  102. */
  103. public function hasArgument($key): bool {
  104. return array_key_exists($key, $this->arguments);
  105. }
  106. /**
  107. * Retrieve an external iterator
  108. *
  109. * @link https://php.net/manual/en/iteratoraggregate.getiterator.php
  110. * @since 18.0.0
  111. */
  112. public function getIterator(): Traversable {
  113. return new ArrayIterator($this->arguments);
  114. }
  115. /**
  116. * Whether a offset exists
  117. *
  118. * @link https://php.net/manual/en/arrayaccess.offsetexists.php
  119. * @since 18.0.0
  120. */
  121. public function offsetExists($offset): bool {
  122. return $this->hasArgument($offset);
  123. }
  124. /**
  125. * Offset to retrieve
  126. *
  127. * @link https://php.net/manual/en/arrayaccess.offsetget.php
  128. * @since 18.0.0
  129. */
  130. public function offsetGet($offset) {
  131. return $this->arguments[$offset];
  132. }
  133. /**
  134. * Offset to set
  135. *
  136. * @link https://php.net/manual/en/arrayaccess.offsetset.php
  137. * @since 18.0.0
  138. */
  139. public function offsetSet($offset, $value): void {
  140. $this->setArgument($offset, $value);
  141. }
  142. /**
  143. * Offset to unset
  144. *
  145. * @link https://php.net/manual/en/arrayaccess.offsetunset.php
  146. * @since 18.0.0
  147. */
  148. public function offsetUnset($offset): void {
  149. if ($this->hasArgument($offset)) {
  150. unset($this->arguments[$offset]);
  151. }
  152. }
  153. }