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\Arrayable;
18: use Nexus\Mcp\Core\Validation\Rfc6570UriTemplateValidator;
19:
20: /**
21: * A reference to a resource or resource template definition.
22: *
23: * @implements Arrayable<array{
24: * type: 'ref/resource',
25: * uri: non-empty-string,
26: * }>
27: *
28: * @see https://modelcontextprotocol.io/specification/2025-11-25/schema#resourcetemplatereference
29: */
30: final readonly class ResourceTemplateReference implements Arrayable
31: {
32: public const string TYPE = 'ref/resource';
33:
34: /**
35: * @var non-empty-string
36: */
37: public string $uri;
38:
39: public function __construct(string $uri)
40: {
41: Rfc6570UriTemplateValidator::validate($uri, 'resource template reference "uri"');
42:
43: $this->uri = $uri;
44: }
45:
46: /**
47: * @param array<string, mixed> $data
48: */
49: #[\Override]
50: public static function fromArray(array $data): static
51: {
52: Assert::that($data)->hasOffset('type', 'resource template reference missing the required "type" key.');
53: $type = $data['type'];
54: Assert::that($type)->isIdentical(self::TYPE, 'resource template reference "type" must be {other}, {value} given.');
55:
56: Assert::that($data)->hasOffset('uri', 'resource template reference missing the required "uri" key.');
57: $uri = $data['uri'];
58: Assert::that($uri)->isString('resource template reference "uri" must be a string, {type} given.');
59:
60: return new self($uri);
61: }
62:
63: #[\Override]
64: public function toArray(): array
65: {
66: return [
67: 'type' => self::TYPE,
68: 'uri' => $this->uri,
69: ];
70: }
71:
72: #[\Override]
73: public function jsonSerialize(): array
74: {
75: return $this->toArray();
76: }
77: }
78: