| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | namespace Nexus\Mcp\Core\Schema; |
| 15: | |
| 16: | use Nexus\Assert\Assert; |
| 17: | use Nexus\Mcp\Core\Schema\Enum\ProtocolErrorCode; |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | abstract readonly class Error implements Arrayable |
| 29: | { |
| 30: | public int $code; |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | public string $message; |
| 36: | |
| 37: | public function __construct( |
| 38: | int|ProtocolErrorCode $code, |
| 39: | string $message, |
| 40: | public mixed $data = null, |
| 41: | ) { |
| 42: | Assert::that($message)->isNonEmptyString('error "message" must be a non-empty string.'); |
| 43: | |
| 44: | $this->code = $code instanceof ProtocolErrorCode ? $code->value : $code; |
| 45: | $this->message = $message; |
| 46: | } |
| 47: | |
| 48: | #[\Override] |
| 49: | public function toArray(): array |
| 50: | { |
| 51: | $result = [ |
| 52: | 'code' => $this->code, |
| 53: | 'message' => $this->message, |
| 54: | ]; |
| 55: | |
| 56: | $data = $this->data ?? []; |
| 57: | |
| 58: | if ([] !== $data) { |
| 59: | $result['data'] = $data; |
| 60: | } |
| 61: | |
| 62: | return $result; |
| 63: | } |
| 64: | |
| 65: | #[\Override] |
| 66: | public function jsonSerialize(): array |
| 67: | { |
| 68: | return $this->toArray(); |
| 69: | } |
| 70: | } |
| 71: | |