| 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: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | final readonly class TextResourceContents extends ResourceContents |
| 34: | { |
| 35: | public function __construct( |
| 36: | string $uri, |
| 37: | public string $text, |
| 38: | ?string $mimeType = null, |
| 39: | MetaObject $meta = new MetaObject(), |
| 40: | ) { |
| 41: | parent::__construct(uri: $uri, mimeType: $mimeType, meta: $meta); |
| 42: | } |
| 43: | |
| 44: | #[\Override] |
| 45: | public static function fromArray(array $data): static |
| 46: | { |
| 47: | Assert::that($data)->hasOffset('uri', 'text resource contents is missing the required "uri" key.'); |
| 48: | $uri = $data['uri']; |
| 49: | Assert::that($uri)->isString('text resource contents "uri" must be a string, {type} given.'); |
| 50: | |
| 51: | Assert::that($data)->hasOffset('text', 'text resource contents is missing the required "text" key.'); |
| 52: | $text = $data['text']; |
| 53: | Assert::that($text)->isString('text resource contents "text" must be a string, {type} given.'); |
| 54: | |
| 55: | $mimeType = $data['mimeType'] ?? null; |
| 56: | Assert::that($mimeType)->nullOr()->isString('text resource contents "mimeType" must be a string or null, {type} given.'); |
| 57: | |
| 58: | $meta = new MetaObject(); |
| 59: | |
| 60: | if (\array_key_exists('_meta', $data)) { |
| 61: | Assert::that($data['_meta']) |
| 62: | ->isArray('text resource contents "_meta" must be an object, {type} given.') |
| 63: | ->isMap('text resource contents "_meta" must be a string-keyed object.') |
| 64: | ; |
| 65: | $meta = MetaObject::fromArray($data['_meta']); |
| 66: | } |
| 67: | |
| 68: | return new self(uri: $uri, text: $text, mimeType: $mimeType, meta: $meta); |
| 69: | } |
| 70: | |
| 71: | #[\Override] |
| 72: | public function toArray(): array |
| 73: | { |
| 74: | $data = [ |
| 75: | 'uri' => $this->uri, |
| 76: | 'text' => $this->text, |
| 77: | ]; |
| 78: | |
| 79: | if (null !== $this->mimeType) { |
| 80: | $data['mimeType'] = $this->mimeType; |
| 81: | } |
| 82: | |
| 83: | $meta = $this->meta->toArray(); |
| 84: | |
| 85: | if ([] !== $meta) { |
| 86: | $data['_meta'] = $meta; |
| 87: | } |
| 88: | |
| 89: | return $data; |
| 90: | } |
| 91: | } |
| 92: | |