ameliabooking
/
vendor
/
wordpress
/
php-mcp-schema
/
src
/
Common
/
Lifecycle
/
DTO
/
Implementation.php
ameliabooking
/
vendor
/
wordpress
/
php-mcp-schema
/
src
/
Common
/
Lifecycle
/
DTO
Last commit date
Implementation.php
1 month ago
Implementation.php
187 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace WP\McpSchema\Common\Lifecycle\DTO; |
| 6 | |
| 7 | use WP\McpSchema\Common\Core\DTO\Icon; |
| 8 | use WP\McpSchema\Common\Protocol\DTO\BaseMetadata; |
| 9 | use WP\McpSchema\Common\Traits\ValidatesRequiredFields; |
| 10 | |
| 11 | /** |
| 12 | * Describes the MCP implementation. |
| 13 | * |
| 14 | * @since 2024-11-05 |
| 15 | * @last-updated 2025-11-25 (added properties: description, icons, websiteUrl) |
| 16 | * |
| 17 | * @mcp-domain Common |
| 18 | * @mcp-subdomain Lifecycle |
| 19 | * @mcp-version 2025-11-25 |
| 20 | */ |
| 21 | class Implementation extends BaseMetadata |
| 22 | { |
| 23 | use ValidatesRequiredFields; |
| 24 | |
| 25 | /** |
| 26 | * @since 2024-11-05 |
| 27 | * |
| 28 | * @var string |
| 29 | */ |
| 30 | protected string $version; |
| 31 | |
| 32 | /** |
| 33 | * An optional human-readable description of what this implementation does. |
| 34 | * |
| 35 | * This can be used by clients or servers to provide context about their purpose |
| 36 | * and capabilities. For example, a server might describe the types of resources |
| 37 | * or tools it provides, while a client might describe its intended use case. |
| 38 | * |
| 39 | * @since 2025-11-25 |
| 40 | * |
| 41 | * @var string|null |
| 42 | */ |
| 43 | protected ?string $description; |
| 44 | |
| 45 | /** |
| 46 | * An optional URL of the website for this implementation. |
| 47 | * |
| 48 | * @since 2025-11-25 |
| 49 | * |
| 50 | * @var string|null |
| 51 | */ |
| 52 | protected ?string $websiteUrl; |
| 53 | |
| 54 | /** |
| 55 | * Optional set of sized icons that the client can display in a user interface. |
| 56 | * |
| 57 | * Clients that support rendering icons MUST support at least the following MIME types: |
| 58 | * - `image/png` - PNG images (safe, universal compatibility) |
| 59 | * - `image/jpeg` (and `image/jpg`) - JPEG images (safe, universal compatibility) |
| 60 | * |
| 61 | * Clients that support rendering icons SHOULD also support: |
| 62 | * - `image/svg+xml` - SVG images (scalable but requires security precautions) |
| 63 | * - `image/webp` - WebP images (modern, efficient format) |
| 64 | * |
| 65 | * @since 2025-11-25 |
| 66 | * |
| 67 | * @var array<\WP\McpSchema\Common\Core\DTO\Icon>|null |
| 68 | */ |
| 69 | protected ?array $icons; |
| 70 | |
| 71 | /** |
| 72 | * @param string $name @since 2024-11-05 |
| 73 | * @param string $version @since 2024-11-05 |
| 74 | * @param string|null $title @since 2025-06-18 |
| 75 | * @param string|null $description @since 2025-11-25 |
| 76 | * @param string|null $websiteUrl @since 2025-11-25 |
| 77 | * @param array<\WP\McpSchema\Common\Core\DTO\Icon>|null $icons @since 2025-11-25 |
| 78 | */ |
| 79 | public function __construct( |
| 80 | string $name, |
| 81 | string $version, |
| 82 | ?string $title = null, |
| 83 | ?string $description = null, |
| 84 | ?string $websiteUrl = null, |
| 85 | ?array $icons = null |
| 86 | ) { |
| 87 | parent::__construct($name, $title); |
| 88 | $this->version = $version; |
| 89 | $this->description = $description; |
| 90 | $this->websiteUrl = $websiteUrl; |
| 91 | $this->icons = $icons; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Creates an instance from an array. |
| 96 | * |
| 97 | * @param array{ |
| 98 | * name: string, |
| 99 | * title?: string|null, |
| 100 | * version: string, |
| 101 | * description?: string|null, |
| 102 | * websiteUrl?: string|null, |
| 103 | * icons?: array<array<string, mixed>|\WP\McpSchema\Common\Core\DTO\Icon>|null |
| 104 | * } $data |
| 105 | * @phpstan-param array<string, mixed> $data |
| 106 | * @return self |
| 107 | */ |
| 108 | public static function fromArray(array $data): self |
| 109 | { |
| 110 | self::assertRequired($data, ['name', 'version']); |
| 111 | |
| 112 | /** @var array<\WP\McpSchema\Common\Core\DTO\Icon>|null $icons */ |
| 113 | $icons = isset($data['icons']) |
| 114 | ? array_map( |
| 115 | static fn($item) => is_array($item) |
| 116 | ? Icon::fromArray($item) |
| 117 | : $item, |
| 118 | self::asArray($data['icons']) |
| 119 | ) |
| 120 | : null; |
| 121 | |
| 122 | return new self( |
| 123 | self::asString($data['name']), |
| 124 | self::asString($data['version']), |
| 125 | self::asStringOrNull($data['title'] ?? null), |
| 126 | self::asStringOrNull($data['description'] ?? null), |
| 127 | self::asStringOrNull($data['websiteUrl'] ?? null), |
| 128 | $icons |
| 129 | ); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Converts the instance to an array. |
| 134 | * |
| 135 | * @return array<string, mixed> |
| 136 | */ |
| 137 | public function toArray(): array |
| 138 | { |
| 139 | $result = parent::toArray(); |
| 140 | |
| 141 | $result['version'] = $this->version; |
| 142 | if ($this->description !== null) { |
| 143 | $result['description'] = $this->description; |
| 144 | } |
| 145 | if ($this->websiteUrl !== null) { |
| 146 | $result['websiteUrl'] = $this->websiteUrl; |
| 147 | } |
| 148 | if ($this->icons !== null) { |
| 149 | $result['icons'] = array_map(static fn($item) => $item->toArray(), $this->icons); |
| 150 | } |
| 151 | |
| 152 | return $result; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * @return string |
| 157 | */ |
| 158 | public function getVersion(): string |
| 159 | { |
| 160 | return $this->version; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * @return string|null |
| 165 | */ |
| 166 | public function getDescription(): ?string |
| 167 | { |
| 168 | return $this->description; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @return string|null |
| 173 | */ |
| 174 | public function getWebsiteUrl(): ?string |
| 175 | { |
| 176 | return $this->websiteUrl; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * @return array<\WP\McpSchema\Common\Core\DTO\Icon>|null |
| 181 | */ |
| 182 | public function getIcons(): ?array |
| 183 | { |
| 184 | return $this->icons; |
| 185 | } |
| 186 | } |
| 187 |