| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace WP\McpSchema\Server\Resources\DTO; |
| 6 |
|
| 7 |
use WP\McpSchema\Common\AbstractDataTransferObject; |
| 8 |
use WP\McpSchema\Common\Traits\ValidatesRequiredFields; |
| 9 |
|
| 10 |
/** |
| 11 |
* The contents of a specific resource or sub-resource. |
| 12 |
* |
| 13 |
* @since 2024-11-05 |
| 14 |
* @last-updated 2025-06-18 (added properties: _meta) |
| 15 |
* |
| 16 |
* @mcp-domain Server |
| 17 |
* @mcp-subdomain Resources |
| 18 |
* @mcp-version 2025-11-25 |
| 19 |
*/ |
| 20 |
class ResourceContents extends AbstractDataTransferObject |
| 21 |
{ |
| 22 |
use ValidatesRequiredFields; |
| 23 |
|
| 24 |
/** |
| 25 |
* The URI of this resource. |
| 26 |
* |
| 27 |
* @since 2024-11-05 |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
protected string $uri; |
| 32 |
|
| 33 |
/** |
| 34 |
* The MIME type of this resource, if known. |
| 35 |
* |
| 36 |
* @since 2024-11-05 |
| 37 |
* |
| 38 |
* @var string|null |
| 39 |
*/ |
| 40 |
protected ?string $mimeType; |
| 41 |
|
| 42 |
/** |
| 43 |
* See [General fields: `_meta`](/specification/2025-11-25/basic/index#meta) for notes on `_meta` usage. |
| 44 |
* |
| 45 |
* @since 2025-06-18 |
| 46 |
* |
| 47 |
* @var array<string, mixed>|null |
| 48 |
*/ |
| 49 |
protected ?array $_meta; |
| 50 |
|
| 51 |
/** |
| 52 |
* @param string $uri @since 2024-11-05 |
| 53 |
* @param string|null $mimeType @since 2024-11-05 |
| 54 |
* @param array<string, mixed>|null $_meta @since 2025-06-18 |
| 55 |
*/ |
| 56 |
public function __construct( |
| 57 |
string $uri, |
| 58 |
?string $mimeType = null, |
| 59 |
?array $_meta = null |
| 60 |
) { |
| 61 |
$this->uri = $uri; |
| 62 |
$this->mimeType = $mimeType; |
| 63 |
$this->_meta = $_meta; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Creates an instance from an array. |
| 68 |
* |
| 69 |
* @param array{ |
| 70 |
* uri: string, |
| 71 |
* mimeType?: string|null, |
| 72 |
* _meta?: array<string, mixed>|null |
| 73 |
* } $data |
| 74 |
* @phpstan-param array<string, mixed> $data |
| 75 |
* @return self |
| 76 |
*/ |
| 77 |
public static function fromArray(array $data): self |
| 78 |
{ |
| 79 |
self::assertRequired($data, ['uri']); |
| 80 |
|
| 81 |
return new self( |
| 82 |
self::asString($data['uri']), |
| 83 |
self::asStringOrNull($data['mimeType'] ?? null), |
| 84 |
self::asArrayOrNull($data['_meta'] ?? null) |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Converts the instance to an array. |
| 90 |
* |
| 91 |
* @return array<string, mixed> |
| 92 |
*/ |
| 93 |
public function toArray(): array |
| 94 |
{ |
| 95 |
$result = []; |
| 96 |
|
| 97 |
$result['uri'] = $this->uri; |
| 98 |
if ($this->mimeType !== null) { |
| 99 |
$result['mimeType'] = $this->mimeType; |
| 100 |
} |
| 101 |
if ($this->_meta !== null) { |
| 102 |
$result['_meta'] = $this->_meta; |
| 103 |
} |
| 104 |
|
| 105 |
return $result; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* @return string |
| 110 |
*/ |
| 111 |
public function getUri(): string |
| 112 |
{ |
| 113 |
return $this->uri; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* @return string|null |
| 118 |
*/ |
| 119 |
public function getMimeType(): ?string |
| 120 |
{ |
| 121 |
return $this->mimeType; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* @return array<string, mixed>|null |
| 126 |
*/ |
| 127 |
public function get_meta(): ?array |
| 128 |
{ |
| 129 |
return $this->_meta; |
| 130 |
} |
| 131 |
} |
| 132 |
|