123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- <?php
- namespace Hoa\Exception;
- class Group extends Exception implements \ArrayAccess, \IteratorAggregate, \Countable
- {
-
- protected $_group = null;
-
- public function __construct(
- $message,
- $code = 0,
- $arguments = [],
- \Exception $previous = null
- ) {
- parent::__construct($message, $code, $arguments, $previous);
- $this->_group = new \SplStack();
- $this->beginTransaction();
- return;
- }
-
- public function raise($previous = false)
- {
- $out = parent::raise($previous);
- if (0 >= count($this)) {
- return $out;
- }
- $out .= "\n\n" . 'Contains the following exceptions:';
- foreach ($this as $exception) {
- $out .=
- "\n\n" . ' • ' .
- str_replace(
- "\n",
- "\n" . ' ',
- $exception->raise($previous)
- );
- }
- return $out;
- }
-
- public function beginTransaction()
- {
- $this->_group->push(new \ArrayObject());
- return $this;
- }
-
- public function rollbackTransaction()
- {
- if (1 >= count($this->_group)) {
- return $this;
- }
- $this->_group->pop();
- return $this;
- }
-
- public function commitTransaction()
- {
- if (false === $this->hasUncommittedExceptions()) {
- $this->_group->pop();
- return $this;
- }
- foreach ($this->_group->pop() as $index => $exception) {
- $this[$index] = $exception;
- }
- return $this;
- }
-
- public function hasUncommittedExceptions()
- {
- return
- 1 < count($this->_group) &&
- 0 < count($this->_group->top());
- }
-
- public function offsetExists($index)
- {
- foreach ($this->_group as $group) {
- if (isset($group[$index])) {
- return true;
- }
- }
- return false;
- }
-
- public function offsetGet($index)
- {
- foreach ($this->_group as $group) {
- if (isset($group[$index])) {
- return $group[$index];
- }
- }
- return null;
- }
-
- public function offsetSet($index, $exception)
- {
- if (!($exception instanceof \Exception)) {
- return null;
- }
- $group = $this->_group->top();
- if (null === $index ||
- true === is_int($index)) {
- $group[] = $exception;
- } else {
- $group[$index] = $exception;
- }
- return;
- }
-
- public function offsetUnset($index)
- {
- foreach ($this->_group as $group) {
- if (isset($group[$index])) {
- unset($group[$index]);
- }
- }
- return;
- }
-
- public function getExceptions()
- {
- return $this->_group->bottom();
- }
-
- public function getIterator()
- {
- return $this->getExceptions()->getIterator();
- }
-
- public function count()
- {
- return count($this->getExceptions());
- }
-
- public function getStackSize()
- {
- return count($this->_group);
- }
- }
|