| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | namespace Nexus\Mcp\Schema\Resource; |
| 15: | |
| 16: | use Nexus\Mcp\Schema\Annotations; |
| 17: | use Nexus\Mcp\Schema\Arrayable; |
| 18: | use Nexus\Mcp\Schema\BaseMetadata; |
| 19: | use Nexus\Mcp\Schema\Icon; |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | final readonly class Resource extends BaseMetadata |
| 37: | { |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | public function __construct( |
| 50: | public string $uri, |
| 51: | string $name, |
| 52: | ?string $title = null, |
| 53: | public ?string $description = null, |
| 54: | public ?string $mimeType = null, |
| 55: | public ?int $size = null, |
| 56: | public ?Annotations $annotations = null, |
| 57: | public ?array $icons = null, |
| 58: | public ?array $meta = null, |
| 59: | ) { |
| 60: | ResourceValidator::assertValidUri($this->uri); |
| 61: | |
| 62: | parent::__construct($name, $title); |
| 63: | } |
| 64: | |
| 65: | #[\Override] |
| 66: | public function toArray(): array |
| 67: | { |
| 68: | return array_filter([ |
| 69: | '_meta' => $this->meta, |
| 70: | 'uri' => $this->uri, |
| 71: | 'name' => $this->name, |
| 72: | 'title' => $this->title, |
| 73: | 'description' => $this->description, |
| 74: | 'mimeType' => $this->mimeType, |
| 75: | 'size' => $this->size, |
| 76: | 'annotations' => $this->annotations?->toArray(), |
| 77: | 'icons' => null === $this->icons ? null : array_map( |
| 78: | static fn(Icon $icon): array => $icon->toArray(), |
| 79: | $this->icons, |
| 80: | ), |
| 81: | ], static fn(mixed $value): bool => null !== $value); |
| 82: | } |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | #[\Override] |
| 88: | public function jsonSerialize(): array |
| 89: | { |
| 90: | return $this->toArray(); |
| 91: | } |
| 92: | } |
| 93: | |