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\Completion;
15:
16: use Nexus\Mcp\Schema\Arrayable;
17: use Nexus\Mcp\Schema\Resource\ResourceValidator;
18:
19: /**
20: * A reference to a resource or resource template definition.
21: *
22: * @implements Arrayable<array{type: 'ref/resource', uri: non-empty-string}>
23: */
24: final readonly class ResourceTemplateReference implements \JsonSerializable, Arrayable
25: {
26: /**
27: * @var non-empty-string
28: */
29: public string $uri;
30:
31: /**
32: * @var non-empty-string
33: */
34: public string $type;
35:
36: /**
37: * @param non-empty-string $uri The URI or URI template of the resource.
38: */
39: public function __construct(string $uri)
40: {
41: if (str_contains($uri, '{') || str_contains($uri, '}')) {
42: ResourceValidator::assertValidUriTemplate($uri);
43: } else {
44: ResourceValidator::assertValidUri($uri);
45: }
46:
47: $this->uri = $uri;
48: $this->type = 'ref/resource';
49: }
50:
51: #[\Override]
52: public function toArray(): array
53: {
54: return [
55: 'type' => $this->type,
56: 'uri' => $this->uri,
57: ];
58: }
59:
60: /**
61: * @return template-type<self, Arrayable, 'T'>
62: */
63: #[\Override]
64: public function jsonSerialize(): array
65: {
66: return $this->toArray();
67: }
68: }
69: