1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus MCP SDK package.
7: *
8: * (c) 2025 John Paul E. Balandan, CPA <paulbalandan@gmail.com>
9: *
10: * For the full copyright and license information, please view
11: * the LICENSE file that was distributed with this source code.
12: */
13:
14: namespace Nexus\Mcp\Schema\Elicitation\Builder;
15:
16: use Nexus\Mcp\Schema\Elicitation\EnumSchema;
17:
18: /**
19: * Fluent builder for {@see EnumSchema}.
20: *
21: * @extends PrimitiveSchemaDefinitionBuilder<non-empty-string, array{
22: * type: 'string',
23: * enum: list<non-empty-string>,
24: * enumNames?: list<non-empty-string>,
25: * description?: non-empty-string,
26: * title?: non-empty-string,
27: * default?: non-empty-string,
28: * }>
29: */
30: final class EnumSchemaBuilder extends PrimitiveSchemaDefinitionBuilder
31: {
32: /**
33: * @var null|list<non-empty-string>
34: */
35: private ?array $enumNames = null;
36:
37: /**
38: * @param non-empty-string $name
39: * @param list<non-empty-string> $enum
40: */
41: protected function __construct(
42: string $name,
43: private array $enum,
44: ) {
45: parent::__construct($name);
46: }
47:
48: /**
49: * @param list<non-empty-string> $enumNames
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: