| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace WP\McpSchema\Common\Core\DTO; |
| 6 |
|
| 7 |
use WP\McpSchema\Common\AbstractDataTransferObject; |
| 8 |
use WP\McpSchema\Common\Traits\ValidatesRequiredFields; |
| 9 |
|
| 10 |
/** |
| 11 |
* An optionally-sized icon that can be displayed in a user interface. |
| 12 |
* |
| 13 |
* @since 2025-11-25 |
| 14 |
* |
| 15 |
* @mcp-domain Common |
| 16 |
* @mcp-subdomain Core |
| 17 |
* @mcp-version 2025-11-25 |
| 18 |
*/ |
| 19 |
class Icon extends AbstractDataTransferObject |
| 20 |
{ |
| 21 |
use ValidatesRequiredFields; |
| 22 |
|
| 23 |
/** |
| 24 |
* A standard URI pointing to an icon resource. May be an HTTP/HTTPS URL or a |
| 25 |
* `data:` URI with Base64-encoded image data. |
| 26 |
* |
| 27 |
* Consumers SHOULD takes steps to ensure URLs serving icons are from the |
| 28 |
* same domain as the client/server or a trusted domain. |
| 29 |
* |
| 30 |
* Consumers SHOULD take appropriate precautions when consuming SVGs as they can contain |
| 31 |
* executable JavaScript. |
| 32 |
* |
| 33 |
* @since 2025-11-25 |
| 34 |
* |
| 35 |
* @var string |
| 36 |
*/ |
| 37 |
protected string $src; |
| 38 |
|
| 39 |
/** |
| 40 |
* Optional MIME type override if the source MIME type is missing or generic. |
| 41 |
* For example: `"image/png"`, `"image/jpeg"`, or `"image/svg+xml"`. |
| 42 |
* |
| 43 |
* @since 2025-11-25 |
| 44 |
* |
| 45 |
* @var string|null |
| 46 |
*/ |
| 47 |
protected ?string $mimeType; |
| 48 |
|
| 49 |
/** |
| 50 |
* Optional array of strings that specify sizes at which the icon can be used. |
| 51 |
* Each string should be in WxH format (e.g., `"48x48"`, `"96x96"`) or `"any"` for scalable formats like SVG. |
| 52 |
* |
| 53 |
* If not provided, the client should assume that the icon can be used at any size. |
| 54 |
* |
| 55 |
* @since 2025-11-25 |
| 56 |
* |
| 57 |
* @var array<string>|null |
| 58 |
*/ |
| 59 |
protected ?array $sizes; |
| 60 |
|
| 61 |
/** |
| 62 |
* Optional specifier for the theme this icon is designed for. `light` indicates |
| 63 |
* the icon is designed to be used with a light background, and `dark` indicates |
| 64 |
* the icon is designed to be used with a dark background. |
| 65 |
* |
| 66 |
* If not provided, the client should assume the icon can be used with any theme. |
| 67 |
* |
| 68 |
* @since 2025-11-25 |
| 69 |
* |
| 70 |
* @var 'light'|'dark'|null |
| 71 |
*/ |
| 72 |
protected ?string $theme; |
| 73 |
|
| 74 |
/** |
| 75 |
* @param string $src @since 2025-11-25 |
| 76 |
* @param string|null $mimeType @since 2025-11-25 |
| 77 |
* @param array<string>|null $sizes @since 2025-11-25 |
| 78 |
* @param 'light'|'dark'|null $theme @since 2025-11-25 |
| 79 |
*/ |
| 80 |
public function __construct( |
| 81 |
string $src, |
| 82 |
?string $mimeType = null, |
| 83 |
?array $sizes = null, |
| 84 |
?string $theme = null |
| 85 |
) { |
| 86 |
$this->src = $src; |
| 87 |
$this->mimeType = $mimeType; |
| 88 |
$this->sizes = $sizes; |
| 89 |
$this->theme = $theme; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Creates an instance from an array. |
| 94 |
* |
| 95 |
* @param array{ |
| 96 |
* src: string, |
| 97 |
* mimeType?: string|null, |
| 98 |
* sizes?: array<string>|null, |
| 99 |
* theme?: 'light'|'dark'|null |
| 100 |
* } $data |
| 101 |
* @phpstan-param array<string, mixed> $data |
| 102 |
* @return self |
| 103 |
*/ |
| 104 |
public static function fromArray(array $data): self |
| 105 |
{ |
| 106 |
self::assertRequired($data, ['src']); |
| 107 |
|
| 108 |
/** @var 'light'|'dark'|null $theme */ |
| 109 |
$theme = isset($data['theme']) |
| 110 |
? self::asStringOrNull($data['theme']) |
| 111 |
: null; |
| 112 |
|
| 113 |
return new self( |
| 114 |
self::asString($data['src']), |
| 115 |
self::asStringOrNull($data['mimeType'] ?? null), |
| 116 |
self::asStringArrayOrNull($data['sizes'] ?? null), |
| 117 |
$theme |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Converts the instance to an array. |
| 123 |
* |
| 124 |
* @return array<string, mixed> |
| 125 |
*/ |
| 126 |
public function toArray(): array |
| 127 |
{ |
| 128 |
$result = []; |
| 129 |
|
| 130 |
$result['src'] = $this->src; |
| 131 |
if ($this->mimeType !== null) { |
| 132 |
$result['mimeType'] = $this->mimeType; |
| 133 |
} |
| 134 |
if ($this->sizes !== null) { |
| 135 |
$result['sizes'] = $this->sizes; |
| 136 |
} |
| 137 |
if ($this->theme !== null) { |
| 138 |
$result['theme'] = $this->theme; |
| 139 |
} |
| 140 |
|
| 141 |
return $result; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* @return string |
| 146 |
*/ |
| 147 |
public function getSrc(): string |
| 148 |
{ |
| 149 |
return $this->src; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* @return string|null |
| 154 |
*/ |
| 155 |
public function getMimeType(): ?string |
| 156 |
{ |
| 157 |
return $this->mimeType; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* @return array<string>|null |
| 162 |
*/ |
| 163 |
public function getSizes(): ?array |
| 164 |
{ |
| 165 |
return $this->sizes; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* @return 'light'|'dark'|null |
| 170 |
*/ |
| 171 |
public function getTheme(): ?string |
| 172 |
{ |
| 173 |
return $this->theme; |
| 174 |
} |
| 175 |
} |
| 176 |
|