GenericEvent.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. /**
  12. * Event encapsulation class.
  13. *
  14. * Encapsulates events thus decoupling the observer from the subject they encapsulate.
  15. *
  16. * @author Drak <drak@zikula.org>
  17. */
  18. class GenericEvent extends Event implements \ArrayAccess, \IteratorAggregate
  19. {
  20. protected $subject;
  21. protected $arguments;
  22. /**
  23. * Encapsulate an event with $subject and $args.
  24. *
  25. * @param mixed $subject The subject of the event, usually an object or a callable
  26. * @param array $arguments Arguments to store in the event
  27. */
  28. public function __construct($subject = null, array $arguments = array())
  29. {
  30. $this->subject = $subject;
  31. $this->arguments = $arguments;
  32. }
  33. /**
  34. * Getter for subject property.
  35. *
  36. * @return mixed $subject The observer subject
  37. */
  38. public function getSubject()
  39. {
  40. return $this->subject;
  41. }
  42. /**
  43. * Get argument by key.
  44. *
  45. * @param string $key Key
  46. *
  47. * @return mixed Contents of array key
  48. *
  49. * @throws \InvalidArgumentException if key is not found
  50. */
  51. public function getArgument($key)
  52. {
  53. if ($this->hasArgument($key)) {
  54. return $this->arguments[$key];
  55. }
  56. throw new \InvalidArgumentException(sprintf('Argument "%s" not found.', $key));
  57. }
  58. /**
  59. * Add argument to event.
  60. *
  61. * @param string $key Argument name
  62. * @param mixed $value Value
  63. *
  64. * @return $this
  65. */
  66. public function setArgument($key, $value)
  67. {
  68. $this->arguments[$key] = $value;
  69. return $this;
  70. }
  71. /**
  72. * Getter for all arguments.
  73. *
  74. * @return array
  75. */
  76. public function getArguments()
  77. {
  78. return $this->arguments;
  79. }
  80. /**
  81. * Set args property.
  82. *
  83. * @param array $args Arguments
  84. *
  85. * @return $this
  86. */
  87. public function setArguments(array $args = array())
  88. {
  89. $this->arguments = $args;
  90. return $this;
  91. }
  92. /**
  93. * Has argument.
  94. *
  95. * @param string $key Key of arguments array
  96. *
  97. * @return bool
  98. */
  99. public function hasArgument($key)
  100. {
  101. return array_key_exists($key, $this->arguments);
  102. }
  103. /**
  104. * ArrayAccess for argument getter.
  105. *
  106. * @param string $key Array key
  107. *
  108. * @return mixed
  109. *
  110. * @throws \InvalidArgumentException if key does not exist in $this->args
  111. */
  112. public function offsetGet($key)
  113. {
  114. return $this->getArgument($key);
  115. }
  116. /**
  117. * ArrayAccess for argument setter.
  118. *
  119. * @param string $key Array key to set
  120. * @param mixed $value Value
  121. */
  122. public function offsetSet($key, $value)
  123. {
  124. $this->setArgument($key, $value);
  125. }
  126. /**
  127. * ArrayAccess for unset argument.
  128. *
  129. * @param string $key Array key
  130. */
  131. public function offsetUnset($key)
  132. {
  133. if ($this->hasArgument($key)) {
  134. unset($this->arguments[$key]);
  135. }
  136. }
  137. /**
  138. * ArrayAccess has argument.
  139. *
  140. * @param string $key Array key
  141. *
  142. * @return bool
  143. */
  144. public function offsetExists($key)
  145. {
  146. return $this->hasArgument($key);
  147. }
  148. /**
  149. * IteratorAggregate for iterating over the object like an array.
  150. *
  151. * @return \ArrayIterator
  152. */
  153. public function getIterator()
  154. {
  155. return new \ArrayIterator($this->arguments);
  156. }
  157. }