| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | namespace Nexus\Mcp\Core\Schema\Resource; |
| 15: | |
| 16: | use Nexus\Assert\Assert; |
| 17: | use Nexus\Mcp\Core\Schema\Arrayable; |
| 18: | use Nexus\Mcp\Core\Schema\MetaObject; |
| 19: | use Nexus\Mcp\Core\Validation\Rfc3986UriValidator; |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | abstract readonly class ResourceContents implements Arrayable |
| 34: | { |
| 35: | |
| 36: | |
| 37: | |
| 38: | public string $uri; |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | public ?string $mimeType; |
| 44: | |
| 45: | public function __construct( |
| 46: | string $uri, |
| 47: | ?string $mimeType = null, |
| 48: | public MetaObject $meta = new MetaObject(), |
| 49: | ) { |
| 50: | Rfc3986UriValidator::validate($uri, 'resource contents "uri"'); |
| 51: | Assert::that($mimeType)->nullOr()->isNonEmptyString('resource contents "mimeType" must be a non-empty string or null.'); |
| 52: | |
| 53: | $this->uri = $uri; |
| 54: | $this->mimeType = $mimeType; |
| 55: | } |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | #[\Override] |
| 61: | abstract public static function fromArray(array $data): static; |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | #[\Override] |
| 68: | public function toArray(): array |
| 69: | { |
| 70: | $data = ['uri' => $this->uri]; |
| 71: | |
| 72: | if (null !== $this->mimeType) { |
| 73: | $data['mimeType'] = $this->mimeType; |
| 74: | } |
| 75: | |
| 76: | $meta = $this->meta->toArray(); |
| 77: | |
| 78: | if ([] !== $meta) { |
| 79: | $data['_meta'] = $meta; |
| 80: | } |
| 81: | |
| 82: | return $data; |
| 83: | } |
| 84: | |
| 85: | #[\Override] |
| 86: | public function jsonSerialize(): array |
| 87: | { |
| 88: | return $this->toArray(); |
| 89: | } |
| 90: | } |
| 91: | |