| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | namespace Nexus\Mcp\Core\Schema\Elicitation; |
| 15: | |
| 16: | use Nexus\Assert\Assert; |
| 17: | use Nexus\Mcp\Core\Schema\Arrayable; |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | final readonly class TitledSingleSelectEnumSchema implements Arrayable, SingleSelectEnumSchema |
| 33: | { |
| 34: | public const string TYPE = 'string'; |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | public array $oneOf; |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | public ?string $title; |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | public ?string $description; |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | public function __construct( |
| 55: | array $oneOf, |
| 56: | ?string $title = null, |
| 57: | ?string $description = null, |
| 58: | public ?string $default = null, |
| 59: | ) { |
| 60: | Assert::that($oneOf) |
| 61: | ->isList('titled single select enum schema "oneOf" must be a list, non-list array given.') |
| 62: | ->values()->isInstanceOf(EnumOption::class) |
| 63: | ; |
| 64: | Assert::that($title)->nullOr()->isNonEmptyString('titled single select enum schema "title" must be a non-empty string or null.'); |
| 65: | Assert::that($description)->nullOr()->isNonEmptyString('titled single select enum schema "description" must be a non-empty string or null.'); |
| 66: | |
| 67: | $this->oneOf = $oneOf; |
| 68: | $this->title = $title; |
| 69: | $this->description = $description; |
| 70: | } |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | #[\Override] |
| 76: | public static function fromArray(array $data): static |
| 77: | { |
| 78: | Assert::that($data)->hasOffset('type', 'titled single select enum schema missing the required "type" key.'); |
| 79: | $type = $data['type']; |
| 80: | Assert::that($type)->isIdentical(self::TYPE, 'titled single select enum schema "type" must be {other}, {value} given.'); |
| 81: | |
| 82: | Assert::that($data)->hasOffset('oneOf', 'titled single select enum schema missing the required "oneOf" key.'); |
| 83: | Assert::that($data['oneOf']) |
| 84: | ->isList('titled single select enum schema "oneOf" must be a list, non-list array given.') |
| 85: | ->values() |
| 86: | ->isArray('each titled single select enum schema "oneOf" must be an object, {type} given.') |
| 87: | ->isMap('each titled single select enum schema "oneOf" must be a string-keyed object.') |
| 88: | ; |
| 89: | $oneOf = array_map(EnumOption::fromArray(...), $data['oneOf']); |
| 90: | |
| 91: | $title = $data['title'] ?? null; |
| 92: | Assert::that($title)->nullOr()->isString('titled single select enum schema "title" must be a string or null, {type} given.'); |
| 93: | |
| 94: | $description = $data['description'] ?? null; |
| 95: | Assert::that($description)->nullOr()->isString('titled single select enum schema "description" must be a string or null, {type} given.'); |
| 96: | |
| 97: | $default = $data['default'] ?? null; |
| 98: | Assert::that($default)->nullOr()->isString('titled single select enum schema "default" must be a string or null, {type} given.'); |
| 99: | |
| 100: | return new self($oneOf, $title, $description, $default); |
| 101: | } |
| 102: | |
| 103: | #[\Override] |
| 104: | public function toArray(): array |
| 105: | { |
| 106: | $data = [ |
| 107: | 'type' => self::TYPE, |
| 108: | 'oneOf' => array_map(static fn(EnumOption $o): array => $o->toArray(), $this->oneOf), |
| 109: | ]; |
| 110: | |
| 111: | if (null !== $this->title) { |
| 112: | $data['title'] = $this->title; |
| 113: | } |
| 114: | |
| 115: | if (null !== $this->description) { |
| 116: | $data['description'] = $this->description; |
| 117: | } |
| 118: | |
| 119: | if (null !== $this->default) { |
| 120: | $data['default'] = $this->default; |
| 121: | } |
| 122: | |
| 123: | return $data; |
| 124: | } |
| 125: | |
| 126: | #[\Override] |
| 127: | public function jsonSerialize(): array |
| 128: | { |
| 129: | return $this->toArray(); |
| 130: | } |
| 131: | } |
| 132: | |