| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | namespace Nexus\Mcp\Core\Schema\Prompt; |
| 15: | |
| 16: | use Nexus\Assert\Assert; |
| 17: | use Nexus\Mcp\Core\Schema\Arrayable; |
| 18: | use Nexus\Mcp\Core\Schema\BaseMetadata; |
| 19: | use Nexus\Mcp\Core\Validation\IdentifierNameValidator; |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | final readonly class PromptReference extends BaseMetadata implements Arrayable |
| 33: | { |
| 34: | public const string TYPE = 'ref/prompt'; |
| 35: | |
| 36: | public function __construct(string $name, ?string $title = null) |
| 37: | { |
| 38: | parent::__construct($name, $title); |
| 39: | |
| 40: | IdentifierNameValidator::validate($name, 'prompt reference "name"'); |
| 41: | } |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | #[\Override] |
| 47: | public static function fromArray(array $data): static |
| 48: | { |
| 49: | Assert::that($data)->hasOffset('type', 'prompt reference missing the required "type" key.'); |
| 50: | $type = $data['type']; |
| 51: | Assert::that($type)->isIdentical(self::TYPE, 'prompt reference "type" must be {other}, {value} given.'); |
| 52: | |
| 53: | Assert::that($data)->hasOffset('name', 'prompt reference missing the required "name" key.'); |
| 54: | $name = $data['name']; |
| 55: | Assert::that($name)->isString('prompt reference "name" must be a string, {type} given.'); |
| 56: | |
| 57: | $title = $data['title'] ?? null; |
| 58: | Assert::that($title)->nullOr()->isString('prompt reference "title" must be a string or null, {type} given.'); |
| 59: | |
| 60: | return new self($name, $title); |
| 61: | } |
| 62: | |
| 63: | #[\Override] |
| 64: | public function toArray(): array |
| 65: | { |
| 66: | $data = [ |
| 67: | 'name' => $this->name, |
| 68: | 'type' => self::TYPE, |
| 69: | ]; |
| 70: | |
| 71: | if (null !== $this->title) { |
| 72: | $data['title'] = $this->title; |
| 73: | } |
| 74: | |
| 75: | return $data; |
| 76: | } |
| 77: | |
| 78: | #[\Override] |
| 79: | public function jsonSerialize(): array |
| 80: | { |
| 81: | return $this->toArray(); |
| 82: | } |
| 83: | } |
| 84: | |