-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathStringSchemaDefinition.php
More file actions
119 lines (102 loc) · 3.64 KB
/
Copy pathStringSchemaDefinition.php
File metadata and controls
119 lines (102 loc) · 3.64 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
119
<?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 fields in elicitation requests.
*
* Supports optional format validation (date, date-time, email, uri) and length constraints.
*
* @author Johannes Wachter <johannes@sulu.io>
*/
final class StringSchemaDefinition extends AbstractSchemaDefinition
{
private const VALID_FORMATS = ['date', 'date-time', 'email', 'uri'];
/**
* @param string $title Human-readable title for the field
* @param string|null $description Optional description/help text
* @param string|null $default Optional default value
* @param string|null $format Optional format constraint (date, date-time, email, uri)
* @param int|null $minLength Optional minimum string length
* @param int|null $maxLength Optional maximum string length
*/
public function __construct(
string $title,
?string $description = null,
public readonly ?string $default = null,
public readonly ?string $format = null,
public readonly ?int $minLength = null,
public readonly ?int $maxLength = null,
) {
parent::__construct($title, $description);
if (null !== $format && !\in_array($format, self::VALID_FORMATS, true)) {
throw new InvalidArgumentException(\sprintf('Invalid format "%s". Valid formats are: %s.', $format, implode(', ', self::VALID_FORMATS)));
}
if (null !== $minLength && $minLength < 0) {
throw new InvalidArgumentException('minLength must be non-negative.');
}
if (null !== $maxLength && $maxLength < 0) {
throw new InvalidArgumentException('maxLength must be non-negative.');
}
if (null !== $minLength && null !== $maxLength && $minLength > $maxLength) {
throw new InvalidArgumentException('minLength cannot be greater than maxLength.');
}
}
/**
* @param array{
* title: string,
* description?: string,
* default?: string,
* format?: string,
* minLength?: int,
* maxLength?: int,
* } $data
*/
public static function fromArray(array $data): self
{
self::validateTitle($data, 'string');
return new self(
title: $data['title'],
description: $data['description'] ?? null,
default: $data['default'] ?? null,
format: $data['format'] ?? null,
minLength: isset($data['minLength']) ? (int) $data['minLength'] : null,
maxLength: isset($data['maxLength']) ? (int) $data['maxLength'] : null,
);
}
/**
* @return array{
* type: string,
* title: string,
* description?: string,
* default?: string,
* format?: string,
* minLength?: int,
* maxLength?: int,
* }
*/
public function jsonSerialize(): array
{
$data = $this->buildBaseJson('string');
if (null !== $this->default) {
$data['default'] = $this->default;
}
if (null !== $this->format) {
$data['format'] = $this->format;
}
if (null !== $this->minLength) {
$data['minLength'] = $this->minLength;
}
if (null !== $this->maxLength) {
$data['maxLength'] = $this->maxLength;
}
return $data;
}
}