PluginProbe
ZIP AI – AI Website Builder & AI Agent (Beta) / 0.0.8
ZIP AI – AI Website Builder & AI Agent (Beta) v0.0.8
0.0.10 0.0.9 trunk 0.0.4 0.0.5 0.0.6 0.0.7 0.0.8
zip-ai / lib / php-mcp-schema / src / Common / JsonRpc / DTO / JSONRPCErrorResponse.php

JSONRPCErrorResponse.php in ZIP AI – AI Website Builder & AI Agent (Beta) 0.0.8, at lib/php-mcp-schema/src/Common/JsonRpc/DTO/JSONRPCErrorResponse.php

137 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\JsonRpc\Union\JSONRPCResponseInterface;
9 use WP\McpSchema\Common\Traits\ValidatesRequiredFields;
10
11 /**
12 * A response to a request that indicates an error occurred.
13 *
14 * @since 2025-11-25
15 *
16 * @mcp-domain Common
17 * @mcp-subdomain JsonRpc
18 * @mcp-version 2025-11-25
19 */
20 class JSONRPCErrorResponse extends AbstractDataTransferObject implements JSONRPCResponseInterface
21 {
22 use ValidatesRequiredFields;
23
24 /**
25 * @since 2025-11-25
26 *
27 * @var '2.0'
28 */
29 protected string $jsonrpc;
30
31 /**
32 * @since 2025-11-25
33 *
34 * @var string|number|null
35 */
36 protected $id;
37
38 /**
39 * @since 2025-11-25
40 *
41 * @var \WP\McpSchema\Common\JsonRpc\DTO\Error
42 */
43 protected Error $error;
44
45 /**
46 * @param '2.0' $jsonrpc @since 2025-11-25
47 * @param \WP\McpSchema\Common\JsonRpc\DTO\Error $error @since 2025-11-25
48 * @param string|number|null $id @since 2025-11-25
49 */
50 public function __construct(
51 string $jsonrpc,
52 Error $error,
53 $id = null
54 ) {
55 $this->jsonrpc = $jsonrpc;
56 $this->error = $error;
57 $this->id = $id;
58 }
59
60 /**
61 * Creates an instance from an array.
62 *
63 * @param array{
64 * jsonrpc: '2.0',
65 * id?: string|number|null,
66 * error: array<string, mixed>|\WP\McpSchema\Common\JsonRpc\DTO\Error
67 * } $data
68 * @phpstan-param array<string, mixed> $data
69 * @return self
70 */
71 public static function fromArray(array $data): self
72 {
73 self::assertRequired($data, ['jsonrpc', 'error']);
74
75 /** @var '2.0' $jsonrpc */
76 $jsonrpc = self::asString($data['jsonrpc']);
77
78 /** @var \WP\McpSchema\Common\JsonRpc\DTO\Error $error */
79 $error = is_array($data['error'])
80 ? Error::fromArray(self::asArray($data['error']))
81 : $data['error'];
82
83 /** @var string|number|null $id */
84 $id = isset($data['id'])
85 ? self::asStringOrNumberOrNull($data['id'])
86 : null;
87
88 return new self(
89 $jsonrpc,
90 $error,
91 $id
92 );
93 }
94
95 /**
96 * Converts the instance to an array.
97 *
98 * @return array<string, mixed>
99 */
100 public function toArray(): array
101 {
102 $result = [];
103
104 $result['jsonrpc'] = $this->jsonrpc;
105 if ($this->id !== null) {
106 $result['id'] = $this->id;
107 }
108 $result['error'] = $this->error->toArray();
109
110 return $result;
111 }
112
113 /**
114 * @return '2.0'
115 */
116 public function getJsonrpc(): string
117 {
118 return $this->jsonrpc;
119 }
120
121 /**
122 * @return string|number|null
123 */
124 public function getId()
125 {
126 return $this->id;
127 }
128
129 /**
130 * @return \WP\McpSchema\Common\JsonRpc\DTO\Error
131 */
132 public function getError(): Error
133 {
134 return $this->error;
135 }
136 }
137