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