| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace WP\McpSchema\Client\Elicitation\DTO; |
| 6 |
|
| 7 |
use WP\McpSchema\Client\Elicitation\Union\MultiSelectEnumSchemaInterface; |
| 8 |
use WP\McpSchema\Common\AbstractDataTransferObject; |
| 9 |
use WP\McpSchema\Common\Traits\ValidatesRequiredFields; |
| 10 |
|
| 11 |
/** |
| 12 |
* Schema for multiple-selection enumeration with display titles for each option. |
| 13 |
* |
| 14 |
* @since 2025-11-25 |
| 15 |
* |
| 16 |
* @mcp-domain Client |
| 17 |
* @mcp-subdomain Elicitation |
| 18 |
* @mcp-version 2025-11-25 |
| 19 |
*/ |
| 20 |
class TitledMultiSelectEnumSchema extends AbstractDataTransferObject implements MultiSelectEnumSchemaInterface |
| 21 |
{ |
| 22 |
use ValidatesRequiredFields; |
| 23 |
|
| 24 |
public const TYPE = 'array'; |
| 25 |
|
| 26 |
public const DISCRIMINATOR_FIELD = 'type'; |
| 27 |
public const DISCRIMINATOR_VALUE = 'array'; |
| 28 |
|
| 29 |
/** |
| 30 |
* @since 2025-11-25 |
| 31 |
* |
| 32 |
* @var 'array' |
| 33 |
*/ |
| 34 |
protected string $type; |
| 35 |
|
| 36 |
/** |
| 37 |
* Optional title for the enum field. |
| 38 |
* |
| 39 |
* @since 2025-11-25 |
| 40 |
* |
| 41 |
* @var string|null |
| 42 |
*/ |
| 43 |
protected ?string $title; |
| 44 |
|
| 45 |
/** |
| 46 |
* Optional description for the enum field. |
| 47 |
* |
| 48 |
* @since 2025-11-25 |
| 49 |
* |
| 50 |
* @var string|null |
| 51 |
*/ |
| 52 |
protected ?string $description; |
| 53 |
|
| 54 |
/** |
| 55 |
* Minimum number of items to select. |
| 56 |
* |
| 57 |
* @since 2025-11-25 |
| 58 |
* |
| 59 |
* @var int|null |
| 60 |
*/ |
| 61 |
protected ?int $minItems; |
| 62 |
|
| 63 |
/** |
| 64 |
* Maximum number of items to select. |
| 65 |
* |
| 66 |
* @since 2025-11-25 |
| 67 |
* |
| 68 |
* @var int|null |
| 69 |
*/ |
| 70 |
protected ?int $maxItems; |
| 71 |
|
| 72 |
/** |
| 73 |
* Schema for array items with enum options and display labels. |
| 74 |
* |
| 75 |
* @since 2025-11-25 |
| 76 |
* |
| 77 |
* @var \WP\McpSchema\Client\Elicitation\DTO\TitledMultiSelectEnumSchemaItems |
| 78 |
*/ |
| 79 |
protected TitledMultiSelectEnumSchemaItems $items; |
| 80 |
|
| 81 |
/** |
| 82 |
* Optional default value. |
| 83 |
* |
| 84 |
* @since 2025-11-25 |
| 85 |
* |
| 86 |
* @var array<string>|null |
| 87 |
*/ |
| 88 |
protected ?array $default; |
| 89 |
|
| 90 |
/** |
| 91 |
* @param \WP\McpSchema\Client\Elicitation\DTO\TitledMultiSelectEnumSchemaItems $items @since 2025-11-25 |
| 92 |
* @param string|null $title @since 2025-11-25 |
| 93 |
* @param string|null $description @since 2025-11-25 |
| 94 |
* @param int|null $minItems @since 2025-11-25 |
| 95 |
* @param int|null $maxItems @since 2025-11-25 |
| 96 |
* @param array<string>|null $default @since 2025-11-25 |
| 97 |
*/ |
| 98 |
public function __construct( |
| 99 |
TitledMultiSelectEnumSchemaItems $items, |
| 100 |
?string $title = null, |
| 101 |
?string $description = null, |
| 102 |
?int $minItems = null, |
| 103 |
?int $maxItems = null, |
| 104 |
?array $default = null |
| 105 |
) { |
| 106 |
$this->type = self::TYPE; |
| 107 |
$this->items = $items; |
| 108 |
$this->title = $title; |
| 109 |
$this->description = $description; |
| 110 |
$this->minItems = $minItems; |
| 111 |
$this->maxItems = $maxItems; |
| 112 |
$this->default = $default; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Creates an instance from an array. |
| 117 |
* |
| 118 |
* @param array{ |
| 119 |
* type: 'array', |
| 120 |
* title?: string|null, |
| 121 |
* description?: string|null, |
| 122 |
* minItems?: int|null, |
| 123 |
* maxItems?: int|null, |
| 124 |
* items: array<string, mixed>|\WP\McpSchema\Client\Elicitation\DTO\TitledMultiSelectEnumSchemaItems, |
| 125 |
* default?: array<string>|null |
| 126 |
* } $data |
| 127 |
* @phpstan-param array<string, mixed> $data |
| 128 |
* @return self |
| 129 |
*/ |
| 130 |
public static function fromArray(array $data): self |
| 131 |
{ |
| 132 |
self::assertRequired($data, ['items']); |
| 133 |
|
| 134 |
/** @var \WP\McpSchema\Client\Elicitation\DTO\TitledMultiSelectEnumSchemaItems $items */ |
| 135 |
$items = is_array($data['items']) |
| 136 |
? TitledMultiSelectEnumSchemaItems::fromArray(self::asArray($data['items'])) |
| 137 |
: $data['items']; |
| 138 |
|
| 139 |
return new self( |
| 140 |
$items, |
| 141 |
self::asStringOrNull($data['title'] ?? null), |
| 142 |
self::asStringOrNull($data['description'] ?? null), |
| 143 |
self::asIntOrNull($data['minItems'] ?? null), |
| 144 |
self::asIntOrNull($data['maxItems'] ?? null), |
| 145 |
self::asStringArrayOrNull($data['default'] ?? null) |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Converts the instance to an array. |
| 151 |
* |
| 152 |
* @return array<string, mixed> |
| 153 |
*/ |
| 154 |
public function toArray(): array |
| 155 |
{ |
| 156 |
$result = []; |
| 157 |
|
| 158 |
$result['type'] = $this->type; |
| 159 |
if ($this->title !== null) { |
| 160 |
$result['title'] = $this->title; |
| 161 |
} |
| 162 |
if ($this->description !== null) { |
| 163 |
$result['description'] = $this->description; |
| 164 |
} |
| 165 |
if ($this->minItems !== null) { |
| 166 |
$result['minItems'] = $this->minItems; |
| 167 |
} |
| 168 |
if ($this->maxItems !== null) { |
| 169 |
$result['maxItems'] = $this->maxItems; |
| 170 |
} |
| 171 |
$result['items'] = $this->items->toArray(); |
| 172 |
if ($this->default !== null) { |
| 173 |
$result['default'] = $this->default; |
| 174 |
} |
| 175 |
|
| 176 |
return $result; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* @return 'array' |
| 181 |
*/ |
| 182 |
public function getType(): string |
| 183 |
{ |
| 184 |
return $this->type; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* @return string|null |
| 189 |
*/ |
| 190 |
public function getTitle(): ?string |
| 191 |
{ |
| 192 |
return $this->title; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* @return string|null |
| 197 |
*/ |
| 198 |
public function getDescription(): ?string |
| 199 |
{ |
| 200 |
return $this->description; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* @return int|null |
| 205 |
*/ |
| 206 |
public function getMinItems(): ?int |
| 207 |
{ |
| 208 |
return $this->minItems; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* @return int|null |
| 213 |
*/ |
| 214 |
public function getMaxItems(): ?int |
| 215 |
{ |
| 216 |
return $this->maxItems; |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* @return \WP\McpSchema\Client\Elicitation\DTO\TitledMultiSelectEnumSchemaItems |
| 221 |
*/ |
| 222 |
public function getItems(): TitledMultiSelectEnumSchemaItems |
| 223 |
{ |
| 224 |
return $this->items; |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* @return array<string>|null |
| 229 |
*/ |
| 230 |
public function getDefault(): ?array |
| 231 |
{ |
| 232 |
return $this->default; |
| 233 |
} |
| 234 |
} |
| 235 |
|