Group.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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\Exception;
  36. /**
  37. * Class \Hoa\Exception\Group.
  38. *
  39. * This is an exception that contains a group of exceptions.
  40. *
  41. * @copyright Copyright © 2007-2017 Hoa community
  42. * @license New BSD License
  43. */
  44. class Group extends Exception implements \ArrayAccess, \IteratorAggregate, \Countable
  45. {
  46. /**
  47. * All exceptions (stored in a stack for transactions).
  48. *
  49. * @var \SplStack
  50. */
  51. protected $_group = null;
  52. /**
  53. * Create an exception.
  54. *
  55. * @param string $message Formatted message.
  56. * @param int $code Code (the ID).
  57. * @param array $arguments Arguments to format message.
  58. * @param \Exception $previous Previous exception in chaining.
  59. */
  60. public function __construct(
  61. $message,
  62. $code = 0,
  63. $arguments = [],
  64. \Exception $previous = null
  65. ) {
  66. parent::__construct($message, $code, $arguments, $previous);
  67. $this->_group = new \SplStack();
  68. $this->beginTransaction();
  69. return;
  70. }
  71. /**
  72. * Raise an exception as a string.
  73. *
  74. * @param bool $previous Whether raise previous exception if exists.
  75. * @return string
  76. */
  77. public function raise($previous = false)
  78. {
  79. $out = parent::raise($previous);
  80. if (0 >= count($this)) {
  81. return $out;
  82. }
  83. $out .= "\n\n" . 'Contains the following exceptions:';
  84. foreach ($this as $exception) {
  85. $out .=
  86. "\n\n" . ' • ' .
  87. str_replace(
  88. "\n",
  89. "\n" . ' ',
  90. $exception->raise($previous)
  91. );
  92. }
  93. return $out;
  94. }
  95. /**
  96. * Begin a transaction.
  97. *
  98. * @return \Hoa\Exception\Group
  99. */
  100. public function beginTransaction()
  101. {
  102. $this->_group->push(new \ArrayObject());
  103. return $this;
  104. }
  105. /**
  106. * Rollback a transaction.
  107. *
  108. * @return \Hoa\Exception\Group
  109. */
  110. public function rollbackTransaction()
  111. {
  112. if (1 >= count($this->_group)) {
  113. return $this;
  114. }
  115. $this->_group->pop();
  116. return $this;
  117. }
  118. /**
  119. * Commit a transaction.
  120. *
  121. * @return \Hoa\Exception\Group
  122. */
  123. public function commitTransaction()
  124. {
  125. if (false === $this->hasUncommittedExceptions()) {
  126. $this->_group->pop();
  127. return $this;
  128. }
  129. foreach ($this->_group->pop() as $index => $exception) {
  130. $this[$index] = $exception;
  131. }
  132. return $this;
  133. }
  134. /**
  135. * Check if there is uncommitted exceptions.
  136. *
  137. * @return bool
  138. */
  139. public function hasUncommittedExceptions()
  140. {
  141. return
  142. 1 < count($this->_group) &&
  143. 0 < count($this->_group->top());
  144. }
  145. /**
  146. * Check if an index in the group exists.
  147. *
  148. * @param mixed $index Index.
  149. * @return bool
  150. */
  151. public function offsetExists($index)
  152. {
  153. foreach ($this->_group as $group) {
  154. if (isset($group[$index])) {
  155. return true;
  156. }
  157. }
  158. return false;
  159. }
  160. /**
  161. * Get an exception from the group.
  162. *
  163. * @param mixed $index Index.
  164. * @return Exception
  165. */
  166. public function offsetGet($index)
  167. {
  168. foreach ($this->_group as $group) {
  169. if (isset($group[$index])) {
  170. return $group[$index];
  171. }
  172. }
  173. return null;
  174. }
  175. /**
  176. * Set an exception in the group.
  177. *
  178. * @param mixed $index Index.
  179. * @param Exception $exception Exception.
  180. * @return void
  181. */
  182. public function offsetSet($index, $exception)
  183. {
  184. if (!($exception instanceof \Exception)) {
  185. return null;
  186. }
  187. $group = $this->_group->top();
  188. if (null === $index ||
  189. true === is_int($index)) {
  190. $group[] = $exception;
  191. } else {
  192. $group[$index] = $exception;
  193. }
  194. return;
  195. }
  196. /**
  197. * Remove an exception in the group.
  198. *
  199. * @param mixed $index Index.
  200. * @return void
  201. */
  202. public function offsetUnset($index)
  203. {
  204. foreach ($this->_group as $group) {
  205. if (isset($group[$index])) {
  206. unset($group[$index]);
  207. }
  208. }
  209. return;
  210. }
  211. /**
  212. * Get committed exceptions in the group.
  213. *
  214. * @return \ArrayObject
  215. */
  216. public function getExceptions()
  217. {
  218. return $this->_group->bottom();
  219. }
  220. /**
  221. * Get an iterator over all exceptions (committed or not).
  222. *
  223. * @return \ArrayIterator
  224. */
  225. public function getIterator()
  226. {
  227. return $this->getExceptions()->getIterator();
  228. }
  229. /**
  230. * Count the number of committed exceptions.
  231. *
  232. * @return int
  233. */
  234. public function count()
  235. {
  236. return count($this->getExceptions());
  237. }
  238. /**
  239. * Count the stack size, i.e. the number of opened transactions.
  240. *
  241. * @return int
  242. */
  243. public function getStackSize()
  244. {
  245. return count($this->_group);
  246. }
  247. }