1: <?php declare(strict_types=1);
2:
3: namespace Salient\Core\Concern;
4:
5: use Salient\Contract\Core\Exception\ExceptionInterface;
6: use Throwable;
7:
8: /**
9: * @phpstan-require-implements ExceptionInterface
10: */
11: trait ExceptionTrait
12: {
13: protected ?int $ExitStatus;
14:
15: public function __construct(
16: string $message = '',
17: ?Throwable $previous = null,
18: ?int $exitStatus = null
19: ) {
20: $this->ExitStatus = $exitStatus;
21:
22: parent::__construct($message, 0, $previous);
23: }
24:
25: /**
26: * @param mixed ...$args
27: */
28: public static function withExitStatus(?int $exitStatus, ...$args): ExceptionInterface
29: {
30: // @phpstan-ignore-next-line
31: $instance = new static(...$args);
32: $instance->ExitStatus = $exitStatus;
33: return $instance;
34: }
35:
36: /**
37: * @inheritDoc
38: */
39: public function getExitStatus(): ?int
40: {
41: return $this->ExitStatus;
42: }
43:
44: /**
45: * @codeCoverageIgnore
46: */
47: public function getMetadata(): array
48: {
49: return [];
50: }
51:
52: /**
53: * @inheritDoc
54: */
55: public function __toString(): string
56: {
57: $detail = '';
58: foreach ($this->getMetadata() as $key => $value) {
59: $detail .= sprintf("\n\n%s:\n%s", $key, rtrim((string) $value, "\n"));
60: }
61:
62: return parent::__toString() . $detail;
63: }
64: }
65: