Repository navigation
Expand file tree
/
Copy pathIcon.php
More file actions
135 lines (120 loc) · 4.4 KB
/
Copy pathIcon.php
File metadata and controls
135 lines (120 loc) · 4.4 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?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;
use Mcp\Schema\Enum\IconTheme;
use Mcp\Schema\Exception\InvalidArgumentException;
/**
* A url pointing to an icon URL or a base64-encoded data URI.
*
* @phpstan-type IconData array{
* src: string,
* mimeType?: string,
* sizes?: string[],
* theme?: string,
* }
*
* @author Christopher Hertel <mail@christopher-hertel.de>
*/
class Icon implements \JsonSerializable
{
/**
* @param string $src a standard URI pointing to an icon resource
* @param ?string $mimeType optional override if the server's MIME type is missing or generic
* @param ?string[] $sizes optional array of strings that specify sizes at which the icon can be used.
* Each string should be in WxH format (e.g., `"48x48"`, `"96x96"`) or `"any"` for
* scalable formats like SVG.
* @param ?IconTheme $theme Optional background this icon is designed for. When omitted, the icon is
* assumed to work against any background.
*/
public function __construct(
public readonly string $src,
public readonly ?string $mimeType = null,
public readonly ?array $sizes = null,
public readonly ?IconTheme $theme = null,
) {
if (empty($src)) {
throw new InvalidArgumentException('Icon "src" must be a non-empty string.');
}
if (!preg_match('#^(https?://|data:)#', $src)) {
throw new InvalidArgumentException('Icon "src" must be a valid URL or data URI.');
}
if (null !== $sizes) {
foreach ($sizes as $size) {
if (!\is_string($size)) {
throw new InvalidArgumentException('Each size in "sizes" must be a string.');
}
if (!preg_match('/^(any|\d+x\d+)$/', $size)) {
throw new InvalidArgumentException(\sprintf('Invalid size format "%s" in "sizes". Expected "WxH" or "any".', $size));
}
}
}
}
/**
* @param IconData|array<mixed> $data
*/
public static function fromArray(array $data): self
{
if (empty($data['src']) || !\is_string($data['src'])) {
throw new InvalidArgumentException('Invalid or missing "src" in Icon data.');
}
if (isset($data['mimeType']) && !\is_string($data['mimeType'])) {
throw new InvalidArgumentException('Invalid "mimeType" in Icon data.');
}
if (isset($data['sizes']) && !\is_array($data['sizes'])) {
throw new InvalidArgumentException('Invalid "sizes" in Icon data.');
}
$theme = null;
if (isset($data['theme'])) {
if (!\is_string($data['theme']) || null === $theme = IconTheme::tryFrom($data['theme'])) {
throw new InvalidArgumentException('Invalid "theme" in Icon data.');
}
}
return new self($data['src'], $data['mimeType'] ?? null, $data['sizes'] ?? null, $theme);
}
/**
* Hydrates an "icons" list, rejecting entries that are not objects.
*
* @param array<mixed> $icons
* @param string $context the surrounding schema type, used for the error message
*
* @return self[]
*/
public static function listFromArray(array $icons, string $context): array
{
return array_map(
static function (mixed $icon) use ($context): self {
if (!\is_array($icon)) {
throw new InvalidArgumentException(\sprintf('Each entry in "icons" of %s data must be an array.', $context));
}
return self::fromArray($icon);
},
$icons,
);
}
/**
* @return IconData
*/
public function jsonSerialize(): array
{
$data = [
'src' => $this->src,
];
if (null !== $this->mimeType) {
$data['mimeType'] = $this->mimeType;
}
if (null !== $this->sizes) {
$data['sizes'] = $this->sizes;
}
if (null !== $this->theme) {
$data['theme'] = $this->theme->value;
}
return $data;
}
}