|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NFePHP\Common\Exception; |
|
4
|
|
|
|
|
5
|
|
|
use IteratorAggregate; |
|
6
|
|
|
use Countable; |
|
7
|
|
|
|
|
8
|
|
|
class ExceptionCollection extends \Exception implements ExceptionInterface, IteratorAggregate, Countable |
|
9
|
|
|
{ |
|
10
|
|
|
protected $exceptions = array(); |
|
11
|
|
|
private $shortMessage; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct($message = '', $code = 0, \Exception $previous = null) |
|
14
|
|
|
{ |
|
15
|
|
|
parent::__construct($message, $code, $previous); |
|
16
|
|
|
$this->shortMessage = $message; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Set all of the exceptions |
|
21
|
|
|
* @param array $exceptions Array of exceptions |
|
22
|
|
|
* @return ExceptionCollection; |
|
|
|
|
|
|
23
|
|
|
*/ |
|
24
|
|
|
public function setExceptions(array $exceptions) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->exceptions = array(); |
|
27
|
|
|
foreach ($exceptions as $exception) { |
|
28
|
|
|
$this->add($exception); |
|
29
|
|
|
} |
|
30
|
|
|
return $this; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Add exceptions to the collection |
|
35
|
|
|
* @param ExceptionCollection|\Exception $e Exception to add |
|
|
|
|
|
|
36
|
|
|
* @return ExceptionCollection; |
|
|
|
|
|
|
37
|
|
|
*/ |
|
38
|
|
|
public function add(\Exception $exception) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->exceptions[] = $exception; |
|
41
|
|
|
$this->message = $this->__toString(); |
|
42
|
|
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get the total number of request exceptions |
|
47
|
|
|
* @return int |
|
48
|
|
|
*/ |
|
49
|
|
|
public function count() |
|
50
|
|
|
{ |
|
51
|
|
|
return count($this->exceptions); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Allows array-like iteration over the request exceptions |
|
56
|
|
|
* @return \ArrayIterator |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getIterator() |
|
59
|
|
|
{ |
|
60
|
|
|
return new \ArrayIterator($this->exceptions); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get the first exception in the collection |
|
65
|
|
|
* @return \Exception |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getFirst() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->exceptions ? $this->exceptions[0] : null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function __toString() |
|
73
|
|
|
{ |
|
74
|
|
|
$messages = array_map(function (\Exception $exception) { |
|
75
|
|
|
return $exception->getMessage(); |
|
76
|
|
|
}, $this->exceptions); |
|
77
|
|
|
return implode(PHP_EOL, $messages); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.