| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | namespace Nexus\Mcp\Schema; |
| 15: | |
| 16: | use Nexus\Mcp\Schema\Resource\ResourceValidator; |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | final readonly class Root implements \JsonSerializable, Arrayable |
| 28: | { |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | public function __construct( |
| 36: | public string $uri, |
| 37: | public ?string $name = null, |
| 38: | public ?array $meta = null, |
| 39: | ) { |
| 40: | if (! ResourceValidator::isValidUri($this->uri) || ! str_starts_with($this->uri, 'file://')) { |
| 41: | throw new \InvalidArgumentException('The root URI must be a valid file URI.'); |
| 42: | } |
| 43: | } |
| 44: | |
| 45: | #[\Override] |
| 46: | public function toArray(): array |
| 47: | { |
| 48: | return array_filter([ |
| 49: | '_meta' => $this->meta, |
| 50: | 'uri' => $this->uri, |
| 51: | 'name' => $this->name, |
| 52: | ], static fn(mixed $value): bool => null !== $value); |
| 53: | } |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | #[\Override] |
| 59: | public function jsonSerialize(): array |
| 60: | { |
| 61: | return $this->toArray(); |
| 62: | } |
| 63: | } |
| 64: | |