| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace WP\McpSchema\Client\Elicitation\DTO; |
| 6 |
|
| 7 |
use WP\McpSchema\Client\Elicitation\Union\SingleSelectEnumSchemaInterface; |
| 8 |
use WP\McpSchema\Common\AbstractDataTransferObject; |
| 9 |
use WP\McpSchema\Common\Traits\ValidatesRequiredFields; |
| 10 |
|
| 11 |
/** |
| 12 |
* Schema for single-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 TitledSingleSelectEnumSchema extends AbstractDataTransferObject implements SingleSelectEnumSchemaInterface |
| 21 |
{ |
| 22 |
use ValidatesRequiredFields; |
| 23 |
|
| 24 |
public const TYPE = 'string'; |
| 25 |
|
| 26 |
public const DISCRIMINATOR_FIELD = 'type'; |
| 27 |
public const DISCRIMINATOR_VALUE = 'string'; |
| 28 |
|
| 29 |
/** |
| 30 |
* @since 2025-11-25 |
| 31 |
* |
| 32 |
* @var 'string' |
| 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 |
* Array of enum options with values and display labels. |
| 56 |
* |
| 57 |
* @since 2025-11-25 |
| 58 |
* |
| 59 |
* @var array<object> |
| 60 |
*/ |
| 61 |
protected array $oneOf; |
| 62 |
|
| 63 |
/** |
| 64 |
* Optional default value. |
| 65 |
* |
| 66 |
* @since 2025-11-25 |
| 67 |
* |
| 68 |
* @var string|null |
| 69 |
*/ |
| 70 |
protected ?string $default; |
| 71 |
|
| 72 |
/** |
| 73 |
* @param array<object> $oneOf @since 2025-11-25 |
| 74 |
* @param string|null $title @since 2025-11-25 |
| 75 |
* @param string|null $description @since 2025-11-25 |
| 76 |
* @param string|null $default @since 2025-11-25 |
| 77 |
*/ |
| 78 |
public function __construct( |
| 79 |
array $oneOf, |
| 80 |
?string $title = null, |
| 81 |
?string $description = null, |
| 82 |
?string $default = null |
| 83 |
) { |
| 84 |
$this->type = self::TYPE; |
| 85 |
$this->oneOf = $oneOf; |
| 86 |
$this->title = $title; |
| 87 |
$this->description = $description; |
| 88 |
$this->default = $default; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Creates an instance from an array. |
| 93 |
* |
| 94 |
* @param array{ |
| 95 |
* type: 'string', |
| 96 |
* title?: string|null, |
| 97 |
* description?: string|null, |
| 98 |
* oneOf: array<object>, |
| 99 |
* default?: string|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, ['oneOf']); |
| 107 |
|
| 108 |
/** @var array<object> $oneOf */ |
| 109 |
$oneOf = self::asArray($data['oneOf']); |
| 110 |
|
| 111 |
return new self( |
| 112 |
$oneOf, |
| 113 |
self::asStringOrNull($data['title'] ?? null), |
| 114 |
self::asStringOrNull($data['description'] ?? null), |
| 115 |
self::asStringOrNull($data['default'] ?? null) |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Converts the instance to an array. |
| 121 |
* |
| 122 |
* @return array<string, mixed> |
| 123 |
*/ |
| 124 |
public function toArray(): array |
| 125 |
{ |
| 126 |
$result = []; |
| 127 |
|
| 128 |
$result['type'] = $this->type; |
| 129 |
if ($this->title !== null) { |
| 130 |
$result['title'] = $this->title; |
| 131 |
} |
| 132 |
if ($this->description !== null) { |
| 133 |
$result['description'] = $this->description; |
| 134 |
} |
| 135 |
$result['oneOf'] = $this->oneOf; |
| 136 |
if ($this->default !== null) { |
| 137 |
$result['default'] = $this->default; |
| 138 |
} |
| 139 |
|
| 140 |
return $result; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* @return 'string' |
| 145 |
*/ |
| 146 |
public function getType(): string |
| 147 |
{ |
| 148 |
return $this->type; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* @return string|null |
| 153 |
*/ |
| 154 |
public function getTitle(): ?string |
| 155 |
{ |
| 156 |
return $this->title; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* @return string|null |
| 161 |
*/ |
| 162 |
public function getDescription(): ?string |
| 163 |
{ |
| 164 |
return $this->description; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* @return array<object> |
| 169 |
*/ |
| 170 |
public function getOneOf(): array |
| 171 |
{ |
| 172 |
return $this->oneOf; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* @return string|null |
| 177 |
*/ |
| 178 |
public function getDefault(): ?string |
| 179 |
{ |
| 180 |
return $this->default; |
| 181 |
} |
| 182 |
} |
| 183 |
|