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