| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | namespace Nexus\Mcp\Schema\Elicitation\Builder; |
| 15: | |
| 16: | use Nexus\Mcp\Schema\Elicitation\EnumSchema; |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | final class EnumSchemaBuilder extends PrimitiveSchemaDefinitionBuilder |
| 31: | { |
| 32: | |
| 33: | |
| 34: | |
| 35: | private ?array $enumNames = null; |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | protected function __construct( |
| 42: | string $name, |
| 43: | private array $enum, |
| 44: | ) { |
| 45: | parent::__construct($name); |
| 46: | } |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | public function enumNames(array $enumNames): self |
| 52: | { |
| 53: | $this->enumNames = $enumNames; |
| 54: | |
| 55: | return $this; |
| 56: | } |
| 57: | |
| 58: | #[\Override] |
| 59: | public function build(): EnumSchema |
| 60: | { |
| 61: | if (isset($this->enumNames) && \count($this->enum) !== \count($this->enumNames)) { |
| 62: | throw new \LogicException('The number of enum names must match the number of enum values.'); |
| 63: | } |
| 64: | |
| 65: | if (isset($this->default) && ! \in_array($this->default, $this->enum, true)) { |
| 66: | throw new \LogicException('The default value must be one of the enum values.'); |
| 67: | } |
| 68: | |
| 69: | return new EnumSchema( |
| 70: | $this->enum, |
| 71: | $this->enumNames, |
| 72: | $this->description, |
| 73: | $this->title, |
| 74: | $this->default, |
| 75: | ); |
| 76: | } |
| 77: | } |
| 78: | |