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/2026-07-28/schema#resourcetemplatereference
29: */
30: final readonly class ResourceTemplateReference implements Arrayable
31: {
32: public const string TYPE = 'ref/resource';
33:
34: /**
35: * @param non-empty-string $uri
36: */
37: public function __construct(public string $uri)
38: {
39: Rfc6570UriTemplateValidator::validate($uri, 'resource template reference "uri"');
40: }
41:
42: #[\Override]
43: public static function fromArray(array $data): static
44: {
45: Assert::that($data)->hasOffset('type', 'resource template reference is missing the required "type" key.');
46: $type = $data['type'];
47: Assert::that($type)->isIdentical(self::TYPE, 'resource template reference "type" must be {other}, {value} given.');
48:
49: Assert::that($data)->hasOffset('uri', 'resource template reference is missing the required "uri" key.');
50: $uri = $data['uri'];
51: Assert::that($uri)->isNonEmptyString('resource template reference "uri" must be a non-empty string, {type} given.');
52:
53: return new self(uri: $uri);
54: }
55:
56: #[\Override]
57: public function toArray(): array
58: {
59: return [
60: 'type' => self::TYPE,
61: 'uri' => $this->uri,
62: ];
63: }
64:
65: #[\Override]
66: public function jsonSerialize(): array
67: {
68: return $this->toArray();
69: }
70: }
71: