| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace WP\McpSchema\Client\Roots\DTO; |
| 6 |
|
| 7 |
use WP\McpSchema\Common\AbstractDataTransferObject; |
| 8 |
use WP\McpSchema\Common\Traits\ValidatesRequiredFields; |
| 9 |
|
| 10 |
/** |
| 11 |
* Represents a root directory or file that the server can operate on. |
| 12 |
* |
| 13 |
* @since 2024-11-05 |
| 14 |
* @last-updated 2025-06-18 (added properties: _meta) |
| 15 |
* |
| 16 |
* @mcp-domain Client |
| 17 |
* @mcp-subdomain Roots |
| 18 |
* @mcp-version 2025-11-25 |
| 19 |
*/ |
| 20 |
class Root extends AbstractDataTransferObject |
| 21 |
{ |
| 22 |
use ValidatesRequiredFields; |
| 23 |
|
| 24 |
/** |
| 25 |
* The URI identifying the root. This *must* start with file:// for now. |
| 26 |
* This restriction may be relaxed in future versions of the protocol to allow |
| 27 |
* other URI schemes. |
| 28 |
* |
| 29 |
* @since 2024-11-05 |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
protected string $uri; |
| 34 |
|
| 35 |
/** |
| 36 |
* An optional name for the root. This can be used to provide a human-readable |
| 37 |
* identifier for the root, which may be useful for display purposes or for |
| 38 |
* referencing the root in other parts of the application. |
| 39 |
* |
| 40 |
* @since 2024-11-05 |
| 41 |
* |
| 42 |
* @var string|null |
| 43 |
*/ |
| 44 |
protected ?string $name; |
| 45 |
|
| 46 |
/** |
| 47 |
* See [General fields: `_meta`](/specification/2025-11-25/basic/index#meta) for notes on `_meta` usage. |
| 48 |
* |
| 49 |
* @since 2025-06-18 |
| 50 |
* |
| 51 |
* @var array<string, mixed>|null |
| 52 |
*/ |
| 53 |
protected ?array $_meta; |
| 54 |
|
| 55 |
/** |
| 56 |
* @param string $uri @since 2024-11-05 |
| 57 |
* @param string|null $name @since 2024-11-05 |
| 58 |
* @param array<string, mixed>|null $_meta @since 2025-06-18 |
| 59 |
*/ |
| 60 |
public function __construct( |
| 61 |
string $uri, |
| 62 |
?string $name = null, |
| 63 |
?array $_meta = null |
| 64 |
) { |
| 65 |
$this->uri = $uri; |
| 66 |
$this->name = $name; |
| 67 |
$this->_meta = $_meta; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Creates an instance from an array. |
| 72 |
* |
| 73 |
* @param array{ |
| 74 |
* uri: string, |
| 75 |
* name?: string|null, |
| 76 |
* _meta?: array<string, mixed>|null |
| 77 |
* } $data |
| 78 |
* @phpstan-param array<string, mixed> $data |
| 79 |
* @return self |
| 80 |
*/ |
| 81 |
public static function fromArray(array $data): self |
| 82 |
{ |
| 83 |
self::assertRequired($data, ['uri']); |
| 84 |
|
| 85 |
return new self( |
| 86 |
self::asString($data['uri']), |
| 87 |
self::asStringOrNull($data['name'] ?? null), |
| 88 |
self::asArrayOrNull($data['_meta'] ?? null) |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Converts the instance to an array. |
| 94 |
* |
| 95 |
* @return array<string, mixed> |
| 96 |
*/ |
| 97 |
public function toArray(): array |
| 98 |
{ |
| 99 |
$result = []; |
| 100 |
|
| 101 |
$result['uri'] = $this->uri; |
| 102 |
if ($this->name !== null) { |
| 103 |
$result['name'] = $this->name; |
| 104 |
} |
| 105 |
if ($this->_meta !== null) { |
| 106 |
$result['_meta'] = $this->_meta; |
| 107 |
} |
| 108 |
|
| 109 |
return $result; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* @return string |
| 114 |
*/ |
| 115 |
public function getUri(): string |
| 116 |
{ |
| 117 |
return $this->uri; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* @return string|null |
| 122 |
*/ |
| 123 |
public function getName(): ?string |
| 124 |
{ |
| 125 |
return $this->name; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* @return array<string, mixed>|null |
| 130 |
*/ |
| 131 |
public function get_meta(): ?array |
| 132 |
{ |
| 133 |
return $this->_meta; |
| 134 |
} |
| 135 |
} |
| 136 |
|