1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus MCP SDK package.
7: *
8: * (c) 2026 John Paul E. Balandan, CPA <paulbalandan@gmail.com>
9: *
10: * For the full copyright and license information, please view
11: * the LICENSE file that was distributed with this source code.
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: * Text-encoded resource contents. The `text` payload is set only when the
21: * resource can actually be represented as text (not binary data).
22: *
23: * @see https://modelcontextprotocol.io/specification/2025-11-25/schema#textresourcecontents
24: */
25: final readonly class TextResourceContents extends ResourceContents
26: {
27: public function __construct(
28: string $uri,
29: public string $text,
30: ?string $mimeType = null,
31: MetaObject $meta = new MetaObject(),
32: ) {
33: parent::__construct($uri, $mimeType, $meta);
34: }
35:
36: /**
37: * @param array<string, mixed> $data
38: */
39: #[\Override]
40: public static function fromArray(array $data): static
41: {
42: Assert::that($data)->hasOffset('uri', 'text resource contents missing the required "uri" key.');
43: $uri = $data['uri'];
44: Assert::that($uri)->isString('text resource contents "uri" must be a string, {type} given.');
45:
46: Assert::that($data)->hasOffset('text', 'text resource contents missing the required "text" key.');
47: $text = $data['text'];
48: Assert::that($text)->isString('text resource contents "text" must be a string, {type} given.');
49:
50: $mimeType = $data['mimeType'] ?? null;
51: Assert::that($mimeType)->nullOr()->isString('text 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('text resource contents "_meta" must be an object, {type} given.')
58: ->isMap('text resource contents "_meta" must be a string-keyed object.')
59: ;
60: $meta = MetaObject::fromArray($data['_meta']);
61: }
62:
63: return new self($uri, $text, $mimeType, $meta);
64: }
65:
66: #[\Override]
67: public function toArray(): array
68: {
69: return [...parent::toArray(), 'text' => $this->text];
70: }
71: }
72: