Open Monograph Press  3.3.0
ExceptionCollection.php
1 <?php
2 
4 
8 class ExceptionCollection extends \Exception implements GuzzleException, \IteratorAggregate, \Countable
9 {
11  protected $exceptions = array();
12 
14  private $shortMessage;
15 
16  public function __construct($message = '', $code = 0, \Exception $previous = null)
17  {
18  parent::__construct($message, $code, $previous);
19  $this->shortMessage = $message;
20  }
21 
29  public function setExceptions(array $exceptions)
30  {
31  $this->exceptions = array();
32  foreach ($exceptions as $exception) {
33  $this->add($exception);
34  }
35 
36  return $this;
37  }
38 
46  public function add($e)
47  {
48  $this->exceptions[] = $e;
49  if ($this->message) {
50  $this->message .= "\n";
51  }
52 
53  $this->message .= $this->getExceptionMessage($e, 0);
54 
55  return $this;
56  }
57 
63  public function count()
64  {
65  return count($this->exceptions);
66  }
67 
73  public function getIterator()
74  {
75  return new \ArrayIterator($this->exceptions);
76  }
77 
83  public function getFirst()
84  {
85  return $this->exceptions ? $this->exceptions[0] : null;
86  }
87 
88  private function getExceptionMessage(\Exception $e, $depth = 0)
89  {
90  static $sp = ' ';
91  $prefix = $depth ? str_repeat($sp, $depth) : '';
92  $message = "{$prefix}(" . get_class($e) . ') ' . $e->getFile() . ' line ' . $e->getLine() . "\n";
93 
94  if ($e instanceof self) {
95  if ($e->shortMessage) {
96  $message .= "\n{$prefix}{$sp}" . str_replace("\n", "\n{$prefix}{$sp}", $e->shortMessage) . "\n";
97  }
98  foreach ($e as $ee) {
99  $message .= "\n" . $this->getExceptionMessage($ee, $depth + 1);
100  }
101  } else {
102  $message .= "\n{$prefix}{$sp}" . str_replace("\n", "\n{$prefix}{$sp}", $e->getMessage()) . "\n";
103  $message .= "\n{$prefix}{$sp}" . str_replace("\n", "\n{$prefix}{$sp}", $e->getTraceAsString()) . "\n";
104  }
105 
106  return str_replace(getcwd(), '.', $message);
107  }
108 }
Guzzle\Common\Exception\ExceptionCollection
Definition: ExceptionCollection.php:8
Guzzle\Common\Exception
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Exception/BadMethodCallException.php:3
Guzzle\Common\Exception\ExceptionCollection\__construct
__construct($message='', $code=0, \Exception $previous=null)
Definition: ExceptionCollection.php:22
Guzzle\Common\Exception\ExceptionCollection\getIterator
getIterator()
Definition: ExceptionCollection.php:79
Guzzle\Common\Exception\ExceptionCollection\$exceptions
$exceptions
Definition: ExceptionCollection.php:14
Guzzle\Common\Exception\ExceptionCollection\count
count()
Definition: ExceptionCollection.php:69
Guzzle\Common\Exception\ExceptionCollection\setExceptions
setExceptions(array $exceptions)
Definition: ExceptionCollection.php:35
Guzzle\Common\Exception\GuzzleException
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Exception/GuzzleException.php:8
Guzzle\Common\Exception\ExceptionCollection\add
add($e)
Definition: ExceptionCollection.php:52
Guzzle\Common\Exception\ExceptionCollection\getFirst
getFirst()
Definition: ExceptionCollection.php:89