| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace WP\McpSchema\Common\JsonRpc\DTO; |
| 6 |
|
| 7 |
use WP\McpSchema\Common\AbstractDataTransferObject; |
| 8 |
use WP\McpSchema\Common\Traits\ValidatesRequiredFields; |
| 9 |
|
| 10 |
/** |
| 11 |
* @since 2025-11-25 |
| 12 |
* |
| 13 |
* @mcp-domain Common |
| 14 |
* @mcp-subdomain JsonRpc |
| 15 |
* @mcp-version 2025-11-25 |
| 16 |
*/ |
| 17 |
class Error extends AbstractDataTransferObject |
| 18 |
{ |
| 19 |
use ValidatesRequiredFields; |
| 20 |
|
| 21 |
/** |
| 22 |
* The error type that occurred. |
| 23 |
* |
| 24 |
* @since 2025-11-25 |
| 25 |
* |
| 26 |
* @var int |
| 27 |
*/ |
| 28 |
protected int $code; |
| 29 |
|
| 30 |
/** |
| 31 |
* A short description of the error. The message SHOULD be limited to a concise single sentence. |
| 32 |
* |
| 33 |
* @since 2025-11-25 |
| 34 |
* |
| 35 |
* @var string |
| 36 |
*/ |
| 37 |
protected string $message; |
| 38 |
|
| 39 |
/** |
| 40 |
* Additional information about the error. The value of this member is defined by the sender (e.g. detailed error information, nested errors etc.). |
| 41 |
* |
| 42 |
* @since 2025-11-25 |
| 43 |
* |
| 44 |
* @var mixed|null |
| 45 |
*/ |
| 46 |
protected $data; |
| 47 |
|
| 48 |
/** |
| 49 |
* @param int $code @since 2025-11-25 |
| 50 |
* @param string $message @since 2025-11-25 |
| 51 |
* @param mixed|null $data @since 2025-11-25 |
| 52 |
*/ |
| 53 |
public function __construct( |
| 54 |
int $code, |
| 55 |
string $message, |
| 56 |
$data = null |
| 57 |
) { |
| 58 |
$this->code = $code; |
| 59 |
$this->message = $message; |
| 60 |
$this->data = $data; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Creates an instance from an array. |
| 65 |
* |
| 66 |
* @param array{ |
| 67 |
* code: int, |
| 68 |
* message: string, |
| 69 |
* data?: mixed|null |
| 70 |
* } $data |
| 71 |
* @phpstan-param array<string, mixed> $data |
| 72 |
* @return self |
| 73 |
*/ |
| 74 |
public static function fromArray(array $data): self |
| 75 |
{ |
| 76 |
self::assertRequired($data, ['code', 'message']); |
| 77 |
|
| 78 |
return new self( |
| 79 |
self::asInt($data['code']), |
| 80 |
self::asString($data['message']), |
| 81 |
$data['data'] ?? null |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Converts the instance to an array. |
| 87 |
* |
| 88 |
* @return array<string, mixed> |
| 89 |
*/ |
| 90 |
public function toArray(): array |
| 91 |
{ |
| 92 |
$result = []; |
| 93 |
|
| 94 |
$result['code'] = $this->code; |
| 95 |
$result['message'] = $this->message; |
| 96 |
if ($this->data !== null) { |
| 97 |
$result['data'] = $this->data; |
| 98 |
} |
| 99 |
|
| 100 |
return $result; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* @return int |
| 105 |
*/ |
| 106 |
public function getCode(): int |
| 107 |
{ |
| 108 |
return $this->code; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* @return string |
| 113 |
*/ |
| 114 |
public function getMessage(): string |
| 115 |
{ |
| 116 |
return $this->message; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* @return mixed|null |
| 121 |
*/ |
| 122 |
public function getData() |
| 123 |
{ |
| 124 |
return $this->data; |
| 125 |
} |
| 126 |
} |
| 127 |
|