GenericEvent.php 4.1 KB

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