| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace WP\McpSchema\Common\Content\DTO; |
| 6 |
|
| 7 |
use WP\McpSchema\Common\AbstractDataTransferObject; |
| 8 |
use WP\McpSchema\Common\Protocol\DTO\Annotations; |
| 9 |
use WP\McpSchema\Common\Protocol\Union\ContentBlockInterface; |
| 10 |
use WP\McpSchema\Common\Protocol\Union\SamplingMessageContentBlockInterface; |
| 11 |
use WP\McpSchema\Common\Traits\ValidatesRequiredFields; |
| 12 |
|
| 13 |
/** |
| 14 |
* An image provided to or from an LLM. |
| 15 |
* |
| 16 |
* @since 2024-11-05 |
| 17 |
* @last-updated 2025-11-25 (modified property: annotations) |
| 18 |
* |
| 19 |
* @mcp-domain Common |
| 20 |
* @mcp-subdomain Content |
| 21 |
* @mcp-version 2025-11-25 |
| 22 |
*/ |
| 23 |
class ImageContent extends AbstractDataTransferObject implements SamplingMessageContentBlockInterface, ContentBlockInterface |
| 24 |
{ |
| 25 |
use ValidatesRequiredFields; |
| 26 |
|
| 27 |
public const TYPE = 'image'; |
| 28 |
|
| 29 |
public const DISCRIMINATOR_FIELD = 'type'; |
| 30 |
public const DISCRIMINATOR_VALUE = 'image'; |
| 31 |
|
| 32 |
/** |
| 33 |
* @since 2024-11-05 |
| 34 |
* |
| 35 |
* @var 'image' |
| 36 |
*/ |
| 37 |
protected string $type; |
| 38 |
|
| 39 |
/** |
| 40 |
* The base64-encoded image data. |
| 41 |
* |
| 42 |
* @since 2024-11-05 |
| 43 |
* |
| 44 |
* @var string |
| 45 |
*/ |
| 46 |
protected string $data; |
| 47 |
|
| 48 |
/** |
| 49 |
* The MIME type of the image. Different providers may support different image types. |
| 50 |
* |
| 51 |
* @since 2024-11-05 |
| 52 |
* |
| 53 |
* @var string |
| 54 |
*/ |
| 55 |
protected string $mimeType; |
| 56 |
|
| 57 |
/** |
| 58 |
* Optional annotations for the client. |
| 59 |
* |
| 60 |
* @since 2024-11-05 |
| 61 |
* |
| 62 |
* @var \WP\McpSchema\Common\Protocol\DTO\Annotations|null |
| 63 |
*/ |
| 64 |
protected ?Annotations $annotations; |
| 65 |
|
| 66 |
/** |
| 67 |
* See [General fields: `_meta`](/specification/2025-11-25/basic/index#meta) for notes on `_meta` usage. |
| 68 |
* |
| 69 |
* @since 2025-06-18 |
| 70 |
* |
| 71 |
* @var array<string, mixed>|null |
| 72 |
*/ |
| 73 |
protected ?array $_meta; |
| 74 |
|
| 75 |
/** |
| 76 |
* @param string $data @since 2024-11-05 |
| 77 |
* @param string $mimeType @since 2024-11-05 |
| 78 |
* @param \WP\McpSchema\Common\Protocol\DTO\Annotations|null $annotations @since 2024-11-05 |
| 79 |
* @param array<string, mixed>|null $_meta @since 2025-06-18 |
| 80 |
*/ |
| 81 |
public function __construct( |
| 82 |
string $data, |
| 83 |
string $mimeType, |
| 84 |
?Annotations $annotations = null, |
| 85 |
?array $_meta = null |
| 86 |
) { |
| 87 |
$this->type = self::TYPE; |
| 88 |
$this->data = $data; |
| 89 |
$this->mimeType = $mimeType; |
| 90 |
$this->annotations = $annotations; |
| 91 |
$this->_meta = $_meta; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Creates an instance from an array. |
| 96 |
* |
| 97 |
* @param array{ |
| 98 |
* type: 'image', |
| 99 |
* data: string, |
| 100 |
* mimeType: string, |
| 101 |
* annotations?: array<string, mixed>|\WP\McpSchema\Common\Protocol\DTO\Annotations|null, |
| 102 |
* _meta?: array<string, mixed>|null |
| 103 |
* } $data |
| 104 |
* @phpstan-param array<string, mixed> $data |
| 105 |
* @return self |
| 106 |
*/ |
| 107 |
public static function fromArray(array $data): self |
| 108 |
{ |
| 109 |
self::assertRequired($data, ['data', 'mimeType']); |
| 110 |
|
| 111 |
/** @var \WP\McpSchema\Common\Protocol\DTO\Annotations|null $annotations */ |
| 112 |
$annotations = isset($data['annotations']) |
| 113 |
? (is_array($data['annotations']) |
| 114 |
? Annotations::fromArray(self::asArray($data['annotations'])) |
| 115 |
: $data['annotations']) |
| 116 |
: null; |
| 117 |
|
| 118 |
return new self( |
| 119 |
self::asString($data['data']), |
| 120 |
self::asString($data['mimeType']), |
| 121 |
$annotations, |
| 122 |
self::asArrayOrNull($data['_meta'] ?? null) |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Converts the instance to an array. |
| 128 |
* |
| 129 |
* @return array<string, mixed> |
| 130 |
*/ |
| 131 |
public function toArray(): array |
| 132 |
{ |
| 133 |
$result = []; |
| 134 |
|
| 135 |
$result['type'] = $this->type; |
| 136 |
$result['data'] = $this->data; |
| 137 |
$result['mimeType'] = $this->mimeType; |
| 138 |
if ($this->annotations !== null) { |
| 139 |
$result['annotations'] = $this->annotations->toArray(); |
| 140 |
} |
| 141 |
if ($this->_meta !== null) { |
| 142 |
$result['_meta'] = $this->_meta; |
| 143 |
} |
| 144 |
|
| 145 |
return $result; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* @return 'image' |
| 150 |
*/ |
| 151 |
public function getType(): string |
| 152 |
{ |
| 153 |
return $this->type; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* @return string |
| 158 |
*/ |
| 159 |
public function getData(): string |
| 160 |
{ |
| 161 |
return $this->data; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* @return string |
| 166 |
*/ |
| 167 |
public function getMimeType(): string |
| 168 |
{ |
| 169 |
return $this->mimeType; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* @return \WP\McpSchema\Common\Protocol\DTO\Annotations|null |
| 174 |
*/ |
| 175 |
public function getAnnotations(): ?Annotations |
| 176 |
{ |
| 177 |
return $this->annotations; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* @return array<string, mixed>|null |
| 182 |
*/ |
| 183 |
public function get_meta(): ?array |
| 184 |
{ |
| 185 |
return $this->_meta; |
| 186 |
} |
| 187 |
} |
| 188 |
|