1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus MCP SDK package.
7: *
8: * (c) 2025 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\Schema\Content;
15:
16: /**
17: * The contents of a specific resource or sub-resource.
18: *
19: * @extends ResourceContents<array{
20: * _meta?: array<string, mixed>,
21: * text: non-empty-string,
22: * uri: non-empty-string,
23: * mimeType?: non-empty-string,
24: * }>
25: */
26: final readonly class TextResourceContents extends ResourceContents
27: {
28: /**
29: * @param non-empty-string $text The text of the item. This must only be set if the item
30: * can actually be represented as text (not binary data).
31: */
32: public function __construct(
33: string $uri,
34: public string $text,
35: ?string $mimeType = null,
36: ?array $meta = null,
37: ) {
38: if (preg_match('/[^\x09\x0A\x0D\x20-\x7E]/', $text) === 1) {
39: throw new \InvalidArgumentException('Text resource contents must have a valid non-binary string text.');
40: }
41:
42: parent::__construct($uri, $mimeType, $meta);
43: }
44:
45: #[\Override]
46: public function toArray(): array
47: {
48: return array_filter([
49: '_meta' => $this->meta,
50: 'uri' => $this->uri,
51: 'mimeType' => $this->mimeType,
52: 'text' => $this->text,
53: ], static fn(mixed $value): bool => null !== $value);
54: }
55: }
56: