*/ private array $reasons; /** * @param non-empty-array $reasons Array of exceptions. * @param string|null $message Exception message, defaults to message generated from passed exceptions. * * @psalm-assert non-empty-array $reasons */ public function __construct(array $reasons, ?string $message = null) { parent::__construct($message ?? $this->generateMessage($reasons)); $this->reasons = $reasons; } /** * @return non-empty-array */ public function getReasons(): array { return $this->reasons; } /** * @param non-empty-array $reasons */ private function generateMessage(array $reasons): string { $message = \sprintf( 'Multiple errors encountered (%d); use "%s::getReasons()" to retrieve the array of exceptions thrown:', \count($reasons), self::class ); foreach ($reasons as $reason) { $message .= \PHP_EOL . \PHP_EOL . \get_class($reason); if ($reason->getMessage() !== '') { $message .= ': ' . $reason->getMessage(); } } return $message; } }