-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathEnumSchemaDefinition.php
More file actions
118 lines (102 loc) · 3.41 KB
/
EnumSchemaDefinition.php
File metadata and controls
118 lines (102 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Schema\Elicitation;
use Mcp\Exception\InvalidArgumentException;
/**
* Schema definition for string enum fields in elicitation requests.
*
* Provides a list of allowed values with optional human-readable labels.
*
* @author Johannes Wachter <johannes@sulu.io>
*/
final class EnumSchemaDefinition extends AbstractSchemaDefinition
{
/**
* @param string $title Human-readable title for the field
* @param string[] $enum Array of allowed string values
* @param string|null $description Optional description/help text
* @param string|null $default Optional default value (must be in enum)
* @param string[]|null $enumNames Optional human-readable labels for each enum value
*/
public function __construct(
string $title,
public readonly array $enum,
?string $description = null,
public readonly ?string $default = null,
public readonly ?array $enumNames = null,
) {
parent::__construct($title, $description);
if ([] === $enum) {
throw new InvalidArgumentException('enum array must not be empty.');
}
foreach ($enum as $value) {
if (!\is_string($value)) {
throw new InvalidArgumentException('All enum values must be strings.');
}
}
if (null !== $enumNames && \count($enumNames) !== \count($enum)) {
throw new InvalidArgumentException('enumNames length must match enum length.');
}
if (null !== $default && !\in_array($default, $enum, true)) {
throw new InvalidArgumentException(\sprintf('Default value "%s" is not in the enum array.', $default));
}
}
/**
* @param array{
* title: string,
* enum: string[],
* description?: string,
* default?: string,
* enumNames?: string[],
* } $data
*/
public static function fromArray(array $data): self
{
self::validateTitle($data, 'enum');
if (!isset($data['enum']) || !\is_array($data['enum'])) {
throw new InvalidArgumentException('Missing or invalid "enum" for enum schema definition.');
}
return new self(
title: $data['title'],
enum: $data['enum'],
description: $data['description'] ?? null,
default: $data['default'] ?? null,
enumNames: $data['enumNames'] ?? null,
);
}
/**
* @return array{
* type: string,
* title: string,
* enum: string[],
* description?: string,
* default?: string,
* enumNames?: string[],
* }
*/
public function jsonSerialize(): array
{
$data = [
'type' => 'string',
'title' => $this->title,
'enum' => $this->enum,
];
if (null !== $this->description) {
$data['description'] = $this->description;
}
if (null !== $this->default) {
$data['default'] = $this->default;
}
if (null !== $this->enumNames) {
$data['enumNames'] = $this->enumNames;
}
return $data;
}
}