Group.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Socket\Connection;
  36. use Hoa\Socket;
  37. /**
  38. * Class \Hoa\Socket\Connection\Group.
  39. *
  40. * Represent a group of connection handlers.
  41. * Add semantics around Hoa\Socket\Connection\Handler.
  42. *
  43. * @copyright Copyright © 2007-2017 Hoa community
  44. * @license New BSD License
  45. */
  46. class Group implements \ArrayAccess, \IteratorAggregate, \Countable
  47. {
  48. /**
  49. * All connections.
  50. *
  51. * @var array
  52. */
  53. protected $_connections = [];
  54. /**
  55. * Check if a connection offset exists.
  56. *
  57. * @param mixed $offset Offset.
  58. * @return bool
  59. */
  60. public function offsetExists($offset)
  61. {
  62. return true === array_key_exists($offset, $this->_connections);
  63. }
  64. /**
  65. * Get a specific connection.
  66. *
  67. * @param mixed $offset Offset.
  68. * @return \Hoa\Socket\Connection\Handler
  69. */
  70. public function offsetGet($offset)
  71. {
  72. if (false === $this->offsetExists($offset)) {
  73. return null;
  74. }
  75. return $this->_connections[$offset];
  76. }
  77. /**
  78. * Add a connection.
  79. *
  80. * @param mixed $offset Offset.
  81. * @param \Hoa\Socket\Connection\Handler $connection Connection
  82. * (handler).
  83. * @return void
  84. * @throws \Hoa\Socket\Exception
  85. */
  86. public function offsetSet($offset, $connection)
  87. {
  88. if (!($connection instanceof Handler)) {
  89. throw new Socket\Exception(
  90. '%s only accepts %s\Handler objects.',
  91. 0,
  92. [__CLASS__, __NAMESPACE__]
  93. );
  94. }
  95. if (null === $offset) {
  96. $this->_connections[] = $connection;
  97. } else {
  98. $this->_connections[$offset] = $connection;
  99. }
  100. if (1 < count($this)) {
  101. $this->getFirstConnection()->merge($connection);
  102. }
  103. return;
  104. }
  105. /**
  106. * Nothing (not allowed).
  107. *
  108. * @param mixed $offset Offset.
  109. * @return void
  110. * @throws \Hoa\Socket\Exception
  111. */
  112. public function offsetUnset($offset)
  113. {
  114. throw new Socket\Exception(
  115. 'This operation is not allowed: you cannot unset a connection ' .
  116. 'from a group.',
  117. 1
  118. );
  119. return;
  120. }
  121. /**
  122. * Get iterator of all declared connections.
  123. *
  124. * @return \ArrayIterator
  125. */
  126. public function getIterator()
  127. {
  128. return new \ArrayIterator($this->_connections);
  129. }
  130. /**
  131. * Count number of declared connections.
  132. *
  133. * @return int
  134. */
  135. public function count()
  136. {
  137. return count($this->_connections);
  138. }
  139. /**
  140. * Semantics alias of $this->offsetSet(null, $connection).
  141. *
  142. * @param \Hoa\Socket\Connection\Handler $connection Connection
  143. * (handler).
  144. * @return \Hoa\Socket\Connection\Group
  145. */
  146. public function merge(Handler $connection)
  147. {
  148. $this[] = $connection;
  149. return $this;
  150. }
  151. /**
  152. * Run the group of connections.
  153. *
  154. * @return void
  155. * @throws \Hoa\Socket\Exception
  156. */
  157. public function run()
  158. {
  159. if (0 === count($this)) {
  160. throw new Socket\Exception(
  161. 'Nothing to run. You should merge a connection.',
  162. 2
  163. );
  164. }
  165. return $this->getFirstConnection()->run();
  166. }
  167. /**
  168. * Get the first declared connection (where other connections have been
  169. * merged).
  170. *
  171. * @return \Hoa\Socket\Connection\Handler
  172. */
  173. public function getFirstConnection()
  174. {
  175. return $this[key($this->_connections)];
  176. }
  177. }