-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathNumberSchemaDefinition.php
More file actions
115 lines (99 loc) · 3.52 KB
/
Copy pathNumberSchemaDefinition.php
File metadata and controls
115 lines (99 loc) · 3.52 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
<?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 number/integer fields in elicitation requests.
*
* Supports minimum and maximum value constraints.
*
* @author Johannes Wachter <johannes@sulu.io>
*/
final class NumberSchemaDefinition extends AbstractSchemaDefinition
{
/**
* @param string $title Human-readable title for the field
* @param bool $integerOnly Whether to restrict to integer values only
* @param string|null $description Optional description/help text
* @param int|float|null $default Optional default value
* @param int|float|null $minimum Optional minimum value (inclusive)
* @param int|float|null $maximum Optional maximum value (inclusive)
*/
public function __construct(
string $title,
public readonly bool $integerOnly = false,
?string $description = null,
public readonly int|float|null $default = null,
public readonly int|float|null $minimum = null,
public readonly int|float|null $maximum = null,
) {
parent::__construct($title, $description);
if (null !== $minimum && null !== $maximum && $minimum > $maximum) {
throw new InvalidArgumentException('minimum cannot be greater than maximum.');
}
if (null !== $default && null !== $minimum && $default < $minimum) {
throw new InvalidArgumentException('default value cannot be less than minimum.');
}
if (null !== $default && null !== $maximum && $default > $maximum) {
throw new InvalidArgumentException('default value cannot be greater than maximum.');
}
if ($integerOnly && null !== $default && $default !== (int) $default) {
throw new InvalidArgumentException('default value must be an integer when integerOnly is true.');
}
}
/**
* @param array{
* type: string,
* title: string,
* description?: string,
* default?: int|float,
* minimum?: int|float,
* maximum?: int|float,
* } $data
*/
public static function fromArray(array $data): self
{
self::validateTitle($data, 'number');
$type = $data['type'] ?? 'number';
$integerOnly = 'integer' === $type;
return new self(
title: $data['title'],
integerOnly: $integerOnly,
description: $data['description'] ?? null,
default: $data['default'] ?? null,
minimum: $data['minimum'] ?? null,
maximum: $data['maximum'] ?? null,
);
}
/**
* @return array{
* type: string,
* title: string,
* description?: string,
* default?: int|float,
* minimum?: int|float,
* maximum?: int|float,
* }
*/
public function jsonSerialize(): array
{
$data = $this->buildBaseJson($this->integerOnly ? 'integer' : 'number');
if (null !== $this->default) {
$data['default'] = $this->default;
}
if (null !== $this->minimum) {
$data['minimum'] = $this->minimum;
}
if (null !== $this->maximum) {
$data['maximum'] = $this->maximum;
}
return $data;
}
}