You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
3.6 KiB

3 years ago
  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. * @since 18.0.0
  38. */
  39. class GenericEvent extends Event implements ArrayAccess, IteratorAggregate {
  40. protected $subject;
  41. protected $arguments;
  42. /**
  43. * Encapsulate an event with $subject and $args.
  44. *
  45. * @since 18.0.0
  46. */
  47. public function __construct($subject = null, array $arguments = []) {
  48. $this->subject = $subject;
  49. $this->arguments = $arguments;
  50. }
  51. /**
  52. * Getter for subject property.
  53. *
  54. * @since 18.0.0
  55. */
  56. public function getSubject() {
  57. return $this->subject;
  58. }
  59. /**
  60. * Get argument by key.
  61. *
  62. * @throws InvalidArgumentException if key is not found
  63. * @since 18.0.0
  64. */
  65. public function getArgument(string $key) {
  66. if ($this->hasArgument($key)) {
  67. return $this->arguments[$key];
  68. }
  69. throw new InvalidArgumentException(sprintf('Argument "%s" not found.', $key));
  70. }
  71. /**
  72. * Add argument to event.
  73. *
  74. * @since 18.0.0
  75. */
  76. public function setArgument($key, $value): GenericEvent {
  77. $this->arguments[$key] = $value;
  78. return $this;
  79. }
  80. /**
  81. * Getter for all arguments.
  82. *
  83. * @since 18.0.0
  84. */
  85. public function getArguments(): array {
  86. return $this->arguments;
  87. }
  88. /**
  89. * Set args property.
  90. *
  91. * @since 18.0.0
  92. */
  93. public function setArguments(array $args = []): GenericEvent {
  94. $this->arguments = $args;
  95. return $this;
  96. }
  97. /**
  98. * Has argument.
  99. *
  100. * @since 18.0.0
  101. */
  102. public function hasArgument($key): bool {
  103. return array_key_exists($key, $this->arguments);
  104. }
  105. /**
  106. * Retrieve an external iterator
  107. *
  108. * @link https://php.net/manual/en/iteratoraggregate.getiterator.php
  109. * @since 18.0.0
  110. */
  111. public function getIterator(): Traversable {
  112. return new ArrayIterator($this->arguments);
  113. }
  114. /**
  115. * Whether a offset exists
  116. *
  117. * @link https://php.net/manual/en/arrayaccess.offsetexists.php
  118. * @since 18.0.0
  119. */
  120. public function offsetExists($offset): bool {
  121. return $this->hasArgument($offset);
  122. }
  123. /**
  124. * Offset to retrieve
  125. *
  126. * @link https://php.net/manual/en/arrayaccess.offsetget.php
  127. * @since 18.0.0
  128. */
  129. public function offsetGet($offset) {
  130. return $this->arguments[$offset];
  131. }
  132. /**
  133. * Offset to set
  134. *
  135. * @link https://php.net/manual/en/arrayaccess.offsetset.php
  136. * @since 18.0.0
  137. */
  138. public function offsetSet($offset, $value): void {
  139. $this->setArgument($offset, $value);
  140. }
  141. /**
  142. * Offset to unset
  143. *
  144. * @link https://php.net/manual/en/arrayaccess.offsetunset.php
  145. * @since 18.0.0
  146. */
  147. public function offsetUnset($offset): void {
  148. if ($this->hasArgument($offset)) {
  149. unset($this->arguments[$offset]);
  150. }
  151. }
  152. }