| 1: | <?php declare(strict_types=1); |
| 2: | |
| 3: | namespace Salient\Core\Exception; |
| 4: | |
| 5: | use Salient\Contract\Console\ConsoleInterface as Console; |
| 6: | use Salient\Contract\Core\Exception\MultipleErrorException; |
| 7: | use Salient\Utility\Arr; |
| 8: | use Salient\Utility\Format; |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | trait MultipleErrorExceptionTrait |
| 16: | { |
| 17: | protected string $MessageOnly; |
| 18: | |
| 19: | protected array $Errors; |
| 20: | protected bool $HasUnreportedErrors = true; |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | public function __construct(string $message = '', string ...$errors) |
| 26: | { |
| 27: | $message = rtrim($message, ':'); |
| 28: | $this->MessageOnly = $message; |
| 29: | $this->Errors = $errors; |
| 30: | |
| 31: | if (!$errors) { |
| 32: | $this->HasUnreportedErrors = false; |
| 33: | } elseif (count($errors) === 1) { |
| 34: | $message = Arr::implode(': ', [$message, $errors[0]], ''); |
| 35: | } else { |
| 36: | $message = Arr::implode(":\n", [$message, rtrim(Format::list($errors))], ''); |
| 37: | } |
| 38: | |
| 39: | parent::__construct($message); |
| 40: | } |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | public function getMessageOnly(): string |
| 46: | { |
| 47: | return $this->MessageOnly; |
| 48: | } |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | public function getErrors(): array |
| 54: | { |
| 55: | return $this->Errors; |
| 56: | } |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | public function reportErrors(Console $console): void |
| 62: | { |
| 63: | foreach ($this->Errors as $error) { |
| 64: | $console->message('__Error:__', $error, Console::LEVEL_ERROR, Console::TYPE_UNFORMATTED); |
| 65: | } |
| 66: | $this->HasUnreportedErrors = false; |
| 67: | } |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | public function hasUnreportedErrors(): bool |
| 73: | { |
| 74: | return $this->HasUnreportedErrors; |
| 75: | } |
| 76: | } |
| 77: | |