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