1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus MCP SDK package.
7: *
8: * (c) 2026 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\Core\Schema\Elicitation;
15:
16: use Nexus\Assert\Assert;
17:
18: /**
19: * Schema for a boolean elicitation field.
20: *
21: * @implements PrimitiveSchemaDefinition<array{
22: * type: 'boolean',
23: * title?: non-empty-string,
24: * description?: non-empty-string,
25: * default?: bool,
26: * }>
27: *
28: * @see https://modelcontextprotocol.io/specification/draft/schema#booleanschema
29: */
30: final readonly class BooleanSchema implements PrimitiveSchemaDefinition
31: {
32: public const string TYPE = 'boolean';
33:
34: /**
35: * @var null|non-empty-string
36: */
37: public ?string $title;
38:
39: /**
40: * @var null|non-empty-string
41: */
42: public ?string $description;
43:
44: public function __construct(
45: ?string $title = null,
46: ?string $description = null,
47: public ?bool $default = null,
48: ) {
49: Assert::that($title)->nullOr()->isNonEmptyString('boolean schema "title" must be a non-empty string or null.');
50: Assert::that($description)->nullOr()->isNonEmptyString('boolean schema "description" must be a non-empty string or null.');
51:
52: $this->title = $title;
53: $this->description = $description;
54: }
55:
56: #[\Override]
57: public static function fromArray(array $data): static
58: {
59: Assert::that($data)->hasOffset('type', 'boolean schema is missing the required "type" key.');
60: $type = $data['type'];
61: Assert::that($type)->isIdentical(self::TYPE, 'boolean schema "type" must be {other}, {value} given.');
62:
63: $title = $data['title'] ?? null;
64: Assert::that($title)->nullOr()->isString('boolean schema "title" must be a string or null, {type} given.');
65:
66: $description = $data['description'] ?? null;
67: Assert::that($description)->nullOr()->isString('boolean schema "description" must be a string or null, {type} given.');
68:
69: $default = $data['default'] ?? null;
70: Assert::that($default)->nullOr()->isBool('boolean schema "default" must be a bool or null, {type} given.');
71:
72: return new self(title: $title, description: $description, default: $default);
73: }
74:
75: #[\Override]
76: public function toArray(): array
77: {
78: $data = ['type' => self::TYPE];
79:
80: if (null !== $this->title) {
81: $data['title'] = $this->title;
82: }
83:
84: if (null !== $this->description) {
85: $data['description'] = $this->description;
86: }
87:
88: if (null !== $this->default) {
89: $data['default'] = $this->default;
90: }
91:
92: return $data;
93: }
94:
95: #[\Override]
96: public function jsonSerialize(): array
97: {
98: return $this->toArray();
99: }
100: }
101: