1: <?php
2:
3: namespace Guzzle\Common\Exception;
4:
5: /**
6: * Collection of exceptions
7: */
8: class ExceptionCollection extends \Exception implements GuzzleException, \IteratorAggregate, \Countable
9: {
10: /**
11: * @var array Array of Exceptions
12: */
13: protected $exceptions = array();
14:
15: /**
16: * Set all of the exceptions
17: *
18: * @param array $exceptions Array of exceptions
19: *
20: * @return self
21: */
22: public function setExceptions(array $exceptions)
23: {
24: $this->exceptions = $exceptions;
25:
26: return $this;
27: }
28:
29: /**
30: * Add exceptions to the collection
31: *
32: * @param ExceptionCollection|\Exception $e Exception to add
33: *
34: * @return ExceptionCollection;
35: */
36: public function add($e)
37: {
38: if ($this->message) {
39: $this->message .= "\n";
40: }
41:
42: if ($e instanceof self) {
43: foreach ($e as $exception) {
44: $this->exceptions[] = $exception;
45: $this->message .= $e->getMessage() . "\n";
46: }
47: } elseif ($e instanceof \Exception) {
48: $this->exceptions[] = $e;
49: $this->message .= $e->getMessage();
50: }
51:
52: $this->message = rtrim($this->message);
53:
54: return $this;
55: }
56:
57: /**
58: * Get the total number of request exceptions
59: *
60: * @return int
61: */
62: public function count()
63: {
64: return count($this->exceptions);
65: }
66:
67: /**
68: * Allows array-like iteration over the request exceptions
69: *
70: * @return \ArrayIterator
71: */
72: public function getIterator()
73: {
74: return new \ArrayIterator($this->exceptions);
75: }
76:
77: /**
78: * Get the first exception in the collection
79: *
80: * @return \Exception
81: */
82: public function getFirst()
83: {
84: return $this->exceptions ? $this->exceptions[0] : null;
85: }
86: }
87: