-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathPrimitiveSchemaDefinition.php
More file actions
57 lines (51 loc) · 1.85 KB
/
PrimitiveSchemaDefinition.php
File metadata and controls
57 lines (51 loc) · 1.85 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
<?php
declare(strict_types=1);
/*
* 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;
/**
* Factory class for creating primitive schema definitions from array data.
*
* Dispatches to the correct schema definition class based on the "type" field.
*
* @author Johannes Wachter <johannes@sulu.io>
*/
final class PrimitiveSchemaDefinition
{
/**
* Create a schema definition from array data.
*
* @param array{
* type: string,
* title: string,
* description?: string,
* default?: mixed,
* enum?: string[],
* enumNames?: string[],
* format?: string,
* minLength?: int,
* maxLength?: int,
* minimum?: int|float,
* maximum?: int|float,
* } $data
*/
public static function fromArray(array $data): StringSchemaDefinition|NumberSchemaDefinition|BooleanSchemaDefinition|EnumSchemaDefinition
{
if (!isset($data['type']) || !\is_string($data['type'])) {
throw new InvalidArgumentException('Missing or invalid "type" for primitive schema definition.');
}
return match ($data['type']) {
'string' => isset($data['enum']) ? EnumSchemaDefinition::fromArray($data) : StringSchemaDefinition::fromArray($data),
'integer', 'number' => NumberSchemaDefinition::fromArray($data),
'boolean' => BooleanSchemaDefinition::fromArray($data),
default => throw new InvalidArgumentException(\sprintf('Unsupported primitive type "%s". Supported types are: string, integer, number, boolean.', $data['type'])),
};
}
}