Event.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * @file
  4. * TeamSpeak 3 PHP Framework
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * @package TeamSpeak3
  20. * @author Sven 'ScP' Paulsen
  21. * @copyright Copyright (c) Planet TeamSpeak. All rights reserved.
  22. */
  23. /**
  24. * @class TeamSpeak3_Adapter_ServerQuery_Event
  25. * @brief Provides methods to analyze and format a ServerQuery event.
  26. */
  27. class TeamSpeak3_Adapter_ServerQuery_Event implements ArrayAccess
  28. {
  29. /**
  30. * Stores the event type.
  31. *
  32. * @var TeamSpeak3_Helper_String
  33. */
  34. protected $type = null;
  35. /**
  36. * Stores the event data.
  37. *
  38. * @var array
  39. */
  40. protected $data = null;
  41. /**
  42. * Stores the event data as an unparsed string.
  43. *
  44. * @var TeamSpeak3_Helper_String
  45. */
  46. protected $mesg = null;
  47. /**
  48. * Creates a new TeamSpeak3_Adapter_ServerQuery_Event object.
  49. *
  50. * @param TeamSpeak3_Helper_String $evt
  51. * @param TeamSpeak3_Node_Host $con
  52. * @throws TeamSpeak3_Adapter_Exception
  53. * @return TeamSpeak3_Adapter_ServerQuery_Event
  54. */
  55. public function __construct(TeamSpeak3_Helper_String $evt, TeamSpeak3_Node_Host $con = null)
  56. {
  57. if(!$evt->startsWith(TeamSpeak3::EVENT))
  58. {
  59. throw new TeamSpeak3_Adapter_Exception("invalid notification event format");
  60. }
  61. list($type, $data) = $evt->split(TeamSpeak3::SEPARATOR_CELL, 2);
  62. if(empty($data))
  63. {
  64. throw new TeamSpeak3_Adapter_Exception("invalid notification event data");
  65. }
  66. $fake = new TeamSpeak3_Helper_String(TeamSpeak3::ERROR . TeamSpeak3::SEPARATOR_CELL . "id" . TeamSpeak3::SEPARATOR_PAIR . 0 . TeamSpeak3::SEPARATOR_CELL . "msg" . TeamSpeak3::SEPARATOR_PAIR . "ok");
  67. $repl = new TeamSpeak3_Adapter_ServerQuery_Reply(array($data, $fake), $type);
  68. $this->type = $type->substr(strlen(TeamSpeak3::EVENT));
  69. $this->data = $repl->toList();
  70. $this->mesg = $data;
  71. TeamSpeak3_Helper_Signal::getInstance()->emit("notifyEvent", $this, $con);
  72. TeamSpeak3_Helper_Signal::getInstance()->emit("notify" . ucfirst($this->type), $this, $con);
  73. }
  74. /**
  75. * Returns the event type string.
  76. *
  77. * @return TeamSpeak3_Helper_String
  78. */
  79. public function getType()
  80. {
  81. return $this->type;
  82. }
  83. /**
  84. * Returns the event data array.
  85. *
  86. * @return array
  87. */
  88. public function getData()
  89. {
  90. return $this->data;
  91. }
  92. /**
  93. * Returns the event data as an unparsed string.
  94. *
  95. * @return TeamSpeak3_Helper_String
  96. */
  97. public function getMessage()
  98. {
  99. return $this->mesg;
  100. }
  101. /**
  102. * @ignore
  103. */
  104. public function offsetExists($offset)
  105. {
  106. return array_key_exists($offset, $this->data) ? TRUE : FALSE;
  107. }
  108. /**
  109. * @ignore
  110. */
  111. public function offsetGet($offset)
  112. {
  113. if(!$this->offsetExists($offset))
  114. {
  115. throw new TeamSpeak3_Adapter_ServerQuery_Exception("invalid parameter", 0x602);
  116. }
  117. return $this->data[$offset];
  118. }
  119. /**
  120. * @ignore
  121. */
  122. public function offsetSet($offset, $value)
  123. {
  124. throw new TeamSpeak3_Node_Exception("event '" . $this->getType() . "' is read only");
  125. }
  126. /**
  127. * @ignore
  128. */
  129. public function offsetUnset($offset)
  130. {
  131. unset($this->data[$offset]);
  132. }
  133. /**
  134. * @ignore
  135. */
  136. public function __get($offset)
  137. {
  138. return $this->offsetGet($offset);
  139. }
  140. /**
  141. * @ignore
  142. */
  143. public function __set($offset, $value)
  144. {
  145. $this->offsetSet($offset, $value);
  146. }
  147. }