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/2026-07-28/schema#booleanschema
29: */
30: final readonly class BooleanSchema implements PrimitiveSchemaDefinition
31: {
32: public const string TYPE = 'boolean';
33:
34: /**
35: * @param null|non-empty-string $title
36: * @param null|non-empty-string $description
37: */
38: public function __construct(
39: public ?string $title = null,
40: public ?string $description = null,
41: public ?bool $default = null,
42: ) {
43: Assert::that($title)->nullOr()->isNonEmptyString('boolean schema "title" must be a non-empty string or null.');
44: Assert::that($description)->nullOr()->isNonEmptyString('boolean schema "description" must be a non-empty string or null.');
45: }
46:
47: #[\Override]
48: public static function fromArray(array $data): static
49: {
50: Assert::that($data)->hasOffset('type', 'boolean schema is missing the required "type" key.');
51: $type = $data['type'];
52: Assert::that($type)->isIdentical(self::TYPE, 'boolean schema "type" must be {other}, {value} given.');
53:
54: $title = $data['title'] ?? null;
55: Assert::that($title)->nullOr()->isNonEmptyString('boolean schema "title" must be a non-empty string or null, {type} given.');
56:
57: $description = $data['description'] ?? null;
58: Assert::that($description)->nullOr()->isNonEmptyString('boolean schema "description" must be a non-empty string or null, {type} given.');
59:
60: $default = $data['default'] ?? null;
61: Assert::that($default)->nullOr()->isBool('boolean schema "default" must be a bool or null, {type} given.');
62:
63: return new self(title: $title, description: $description, default: $default);
64: }
65:
66: #[\Override]
67: public function toArray(): array
68: {
69: $data = ['type' => self::TYPE];
70:
71: if (null !== $this->title) {
72: $data['title'] = $this->title;
73: }
74:
75: if (null !== $this->description) {
76: $data['description'] = $this->description;
77: }
78:
79: if (null !== $this->default) {
80: $data['default'] = $this->default;
81: }
82:
83: return $data;
84: }
85:
86: #[\Override]
87: public function jsonSerialize(): array
88: {
89: return $this->toArray();
90: }
91: }
92: