Event.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Copyright 2014 Fabian Grutschus. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  19. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. * The views and conclusions contained in the software and documentation are those
  27. * of the authors and should not be interpreted as representing official policies,
  28. * either expressed or implied, of the copyright holders.
  29. *
  30. * @author Fabian Grutschus <f.grutschus@lubyte.de>
  31. * @copyright 2014 Fabian Grutschus. All rights reserved.
  32. * @license BSD
  33. * @link http://github.com/fabiang/xmpp
  34. */
  35. namespace Fabiang\Xmpp\Event;
  36. use Fabiang\Xmpp\Exception\OutOfRangeException;
  37. use Fabiang\Xmpp\Exception\InvalidArgumentException;
  38. /**
  39. * Generic event.
  40. *
  41. * @package Xmpp\Event
  42. */
  43. class Event implements EventInterface
  44. {
  45. /**
  46. * Event name.
  47. *
  48. * @var string
  49. */
  50. protected $name;
  51. /**
  52. * Target object.
  53. *
  54. * @var object
  55. */
  56. protected $target;
  57. /**
  58. * Event parameters.
  59. *
  60. * @var array
  61. */
  62. protected $parameters = [];
  63. /**
  64. * Event stack.
  65. *
  66. * @var array
  67. */
  68. protected $eventStack = [];
  69. /**
  70. * {@inheritDoc}
  71. */
  72. public function getName()
  73. {
  74. return $this->name;
  75. }
  76. /**
  77. * {@inheritDoc}
  78. */
  79. public function getTarget()
  80. {
  81. return $this->target;
  82. }
  83. /**
  84. * {@inheritDoc}
  85. */
  86. public function getParameters()
  87. {
  88. return $this->parameters;
  89. }
  90. /**
  91. * {@inheritDoc}
  92. */
  93. public function setName($name)
  94. {
  95. $this->name = (string) $name;
  96. return $this;
  97. }
  98. /**
  99. * {@inheritDoc}
  100. */
  101. public function setTarget($target)
  102. {
  103. $this->target = $target;
  104. return $this;
  105. }
  106. /**
  107. * {@inheritDoc}
  108. */
  109. public function setParameters(array $parameters)
  110. {
  111. $this->parameters = array_values($parameters);
  112. return $this;
  113. }
  114. /**
  115. * {@inheritDoc}
  116. */
  117. public function getEventStack()
  118. {
  119. return $this->eventStack;
  120. }
  121. /**
  122. * {@inheritDoc}
  123. */
  124. public function setEventStack(array $eventStack)
  125. {
  126. $this->eventStack = $eventStack;
  127. return $this;
  128. }
  129. /**
  130. * {@inheritDoc}
  131. */
  132. public function getParameter($index)
  133. {
  134. $parameters = $this->getParameters();
  135. if (!is_int($index)) {
  136. throw new InvalidArgumentException(
  137. 'Argument #1 of "' . __CLASS__ . '::' . __METHOD__ . '" must be an integer'
  138. );
  139. }
  140. if (!array_key_exists($index, $parameters)) {
  141. throw new OutOfRangeException("The offset $index is out of range.");
  142. }
  143. return $parameters[$index];
  144. }
  145. }