1
0

GenericEvent.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCP\EventDispatcher;
  8. use ArrayAccess;
  9. use ArrayIterator;
  10. use InvalidArgumentException;
  11. use IteratorAggregate;
  12. use Traversable;
  13. use function array_key_exists;
  14. /**
  15. * Class GenericEvent
  16. *
  17. * convenience reimplementation of \Symfony\Component\GenericEvent against
  18. * \OCP\EventDispatcher\Event
  19. *
  20. * @since 18.0.0
  21. * @deprecated 22.0.0 use \OCP\EventDispatcher\Event
  22. */
  23. class GenericEvent extends Event implements ArrayAccess, IteratorAggregate {
  24. /** @deprecated 22.0.0 */
  25. protected $subject;
  26. /** @deprecated 22.0.0 */
  27. protected $arguments;
  28. /**
  29. * Encapsulate an event with $subject and $args.
  30. *
  31. * @since 18.0.0
  32. * @deprecated 22.0.0
  33. */
  34. public function __construct($subject = null, array $arguments = []) {
  35. parent::__construct();
  36. $this->subject = $subject;
  37. $this->arguments = $arguments;
  38. }
  39. /**
  40. * Getter for subject property.
  41. *
  42. * @since 18.0.0
  43. * @deprecated 22.0.0
  44. */
  45. public function getSubject() {
  46. return $this->subject;
  47. }
  48. /**
  49. * Get argument by key.
  50. *
  51. * @throws InvalidArgumentException if key is not found
  52. * @since 18.0.0
  53. * @deprecated 22.0.0
  54. */
  55. public function getArgument(string $key) {
  56. if ($this->hasArgument($key)) {
  57. return $this->arguments[$key];
  58. }
  59. throw new InvalidArgumentException(sprintf('Argument "%s" not found.', $key));
  60. }
  61. /**
  62. * Add argument to event.
  63. *
  64. * @since 18.0.0
  65. * @deprecated 22.0.0
  66. */
  67. public function setArgument($key, $value): GenericEvent {
  68. $this->arguments[$key] = $value;
  69. return $this;
  70. }
  71. /**
  72. * Getter for all arguments.
  73. *
  74. * @since 18.0.0
  75. * @deprecated 22.0.0
  76. */
  77. public function getArguments(): array {
  78. return $this->arguments;
  79. }
  80. /**
  81. * Set args property.
  82. *
  83. * @since 18.0.0
  84. * @deprecated 22.0.0
  85. */
  86. public function setArguments(array $args = []): GenericEvent {
  87. $this->arguments = $args;
  88. return $this;
  89. }
  90. /**
  91. * Has argument.
  92. *
  93. * @since 18.0.0
  94. * @deprecated 22.0.0
  95. */
  96. public function hasArgument($key): bool {
  97. return array_key_exists($key, $this->arguments);
  98. }
  99. /**
  100. * Retrieve an external iterator
  101. *
  102. * @link https://php.net/manual/en/iteratoraggregate.getiterator.php
  103. * @since 18.0.0
  104. * @deprecated 22.0.0
  105. */
  106. public function getIterator(): Traversable {
  107. return new ArrayIterator($this->arguments);
  108. }
  109. /**
  110. * Whether a offset exists
  111. *
  112. * @link https://php.net/manual/en/arrayaccess.offsetexists.php
  113. * @since 18.0.0
  114. * @deprecated 22.0.0
  115. */
  116. public function offsetExists($offset): bool {
  117. return $this->hasArgument($offset);
  118. }
  119. /**
  120. * Offset to retrieve
  121. *
  122. * @link https://php.net/manual/en/arrayaccess.offsetget.php
  123. * @since 18.0.0
  124. * @deprecated 22.0.0
  125. * @return mixed
  126. */
  127. #[\ReturnTypeWillChange]
  128. public function offsetGet($offset) {
  129. return $this->arguments[$offset];
  130. }
  131. /**
  132. * Offset to set
  133. *
  134. * @link https://php.net/manual/en/arrayaccess.offsetset.php
  135. * @since 18.0.0
  136. * @deprecated 22.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. * @deprecated 22.0.0
  147. */
  148. public function offsetUnset($offset): void {
  149. if ($this->hasArgument($offset)) {
  150. unset($this->arguments[$offset]);
  151. }
  152. }
  153. }