| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | namespace Nexus\Mcp\Core\Schema\RequestParams; |
| 15: | |
| 16: | use Nexus\Assert\Assert; |
| 17: | use Nexus\Mcp\Core\Schema\Arrayable; |
| 18: | use Nexus\Mcp\Core\Schema\Elicitation\ElicitResult; |
| 19: | use Nexus\Mcp\Core\Schema\RequestMetaObject; |
| 20: | use Nexus\Mcp\Core\Schema\Result\InputResponse; |
| 21: | use Nexus\Mcp\Core\Validation\IdentifierNameValidator; |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | final readonly class GetPromptRequestParams extends InputResponseRequestParams |
| 37: | { |
| 38: | |
| 39: | |
| 40: | |
| 41: | public string $name; |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | public ?array $arguments; |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | public function __construct( |
| 53: | string $name, |
| 54: | RequestMetaObject $meta, |
| 55: | ?array $arguments = null, |
| 56: | ?array $inputResponses = null, |
| 57: | ?string $requestState = null, |
| 58: | ) { |
| 59: | IdentifierNameValidator::validate($name, '"params.name"'); |
| 60: | |
| 61: | if (null !== $arguments) { |
| 62: | Assert::that($arguments) |
| 63: | ->isMap('"params.arguments" must be a string-keyed map.') |
| 64: | ->values()->isString('"params.arguments" values must all be strings, {type} given.') |
| 65: | ; |
| 66: | } |
| 67: | |
| 68: | $this->name = $name; |
| 69: | $this->arguments = $arguments; |
| 70: | |
| 71: | parent::__construct(inputResponses: $inputResponses, requestState: $requestState, meta: $meta); |
| 72: | } |
| 73: | |
| 74: | #[\Override] |
| 75: | public static function fromArray(array $data): static |
| 76: | { |
| 77: | Assert::that($data)->hasOffset('name', '"params" is missing the required "name" key.'); |
| 78: | $name = $data['name']; |
| 79: | Assert::that($name)->isString('"params.name" must be a string, {type} given.'); |
| 80: | |
| 81: | $arguments = null; |
| 82: | |
| 83: | if (\array_key_exists('arguments', $data)) { |
| 84: | Assert::that($data['arguments']) |
| 85: | ->isArray('"params.arguments" must be an object, {type} given.') |
| 86: | ->isMap('"params.arguments" must be a string-keyed object.') |
| 87: | ->values()->isString('"params.arguments" value must be a string, {type} given.') |
| 88: | ; |
| 89: | $arguments = $data['arguments']; |
| 90: | } |
| 91: | |
| 92: | $inputResponses = null; |
| 93: | |
| 94: | if (\array_key_exists('inputResponses', $data)) { |
| 95: | Assert::that($data['inputResponses']) |
| 96: | ->isArray('"params.inputResponses" must be an object, {type} given.') |
| 97: | ->isMap('"params.inputResponses" must be a string-keyed object.') |
| 98: | ->values() |
| 99: | ->isArray('each "params.inputResponses" entry must be an object, {type} given.') |
| 100: | ->isMap('each "params.inputResponses" entry must be a string-keyed object.') |
| 101: | ; |
| 102: | $inputResponses = array_map(ElicitResult::fromArray(...), $data['inputResponses']); |
| 103: | } |
| 104: | |
| 105: | $requestState = null; |
| 106: | |
| 107: | if (\array_key_exists('requestState', $data)) { |
| 108: | Assert::that($data['requestState'])->isString('"params.requestState" must be a string, {type} given.'); |
| 109: | $requestState = $data['requestState']; |
| 110: | } |
| 111: | |
| 112: | Assert::that($data)->hasOffset('_meta', '"params" is missing the required "_meta" key.'); |
| 113: | Assert::that($data['_meta']) |
| 114: | ->isArray('"params._meta" must be an object, {type} given.') |
| 115: | ->isMap('"params._meta" must be a string-keyed object.') |
| 116: | ; |
| 117: | $meta = RequestMetaObject::fromArray($data['_meta']); |
| 118: | |
| 119: | return new self( |
| 120: | name: $name, |
| 121: | meta: $meta, |
| 122: | arguments: $arguments, |
| 123: | inputResponses: $inputResponses, |
| 124: | requestState: $requestState, |
| 125: | ); |
| 126: | } |
| 127: | |
| 128: | #[\Override] |
| 129: | public function toArray(): array |
| 130: | { |
| 131: | $data = [ |
| 132: | '_meta' => $this->meta->toArray(), |
| 133: | 'name' => $this->name, |
| 134: | ]; |
| 135: | |
| 136: | if (null !== $this->arguments && [] !== $this->arguments) { |
| 137: | $data['arguments'] = $this->arguments; |
| 138: | } |
| 139: | |
| 140: | if (null !== $this->inputResponses) { |
| 141: | $data['inputResponses'] = array_map( |
| 142: | static fn(InputResponse $response): array => $response->toArray(), |
| 143: | $this->inputResponses, |
| 144: | ); |
| 145: | } |
| 146: | |
| 147: | if (null !== $this->requestState) { |
| 148: | $data['requestState'] = $this->requestState; |
| 149: | } |
| 150: | |
| 151: | return $data; |
| 152: | } |
| 153: | } |
| 154: | |