| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | namespace Nexus\Mcp\Core\Schema\Result; |
| 15: | |
| 16: | use Nexus\Assert\Assert; |
| 17: | use Nexus\Mcp\Core\Schema\Enum\ResultType; |
| 18: | use Nexus\Mcp\Core\Schema\MetaObject; |
| 19: | use Nexus\Mcp\Core\Schema\MetaObject\GenericResultMetaObject; |
| 20: | use Nexus\Mcp\Core\Schema\MetaObject\ResultMetaObject; |
| 21: | use Nexus\Mcp\Core\Schema\Result; |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | final readonly class CompleteResult extends Result implements ServerResult |
| 35: | { |
| 36: | |
| 37: | |
| 38: | |
| 39: | public array $completion; |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | public function __construct(array $completion, ResultMetaObject $meta = new GenericResultMetaObject()) |
| 45: | { |
| 46: | Assert::that($completion['values']) |
| 47: | ->isList('"result.completion.values" must be a list, non-list array given.') |
| 48: | ->values()->isString('each "result.completion.values" must be a string, {type} given.') |
| 49: | ; |
| 50: | |
| 51: | $normalized = ['values' => $completion['values']]; |
| 52: | |
| 53: | if (\array_key_exists('total', $completion)) { |
| 54: | Assert::that($completion['total'])->isInt('"result.completion.total" must be an int, {type} given.'); |
| 55: | $normalized['total'] = $completion['total']; |
| 56: | } |
| 57: | |
| 58: | if (\array_key_exists('hasMore', $completion)) { |
| 59: | Assert::that($completion['hasMore'])->isBool('"result.completion.hasMore" must be a bool, {type} given.'); |
| 60: | $normalized['hasMore'] = $completion['hasMore']; |
| 61: | } |
| 62: | |
| 63: | $this->completion = $normalized; |
| 64: | |
| 65: | parent::__construct(meta: $meta); |
| 66: | } |
| 67: | |
| 68: | #[\Override] |
| 69: | public static function fromArray(array $data): static |
| 70: | { |
| 71: | Assert::that($data)->hasOffset('completion', '"result" is missing the required "completion" key.'); |
| 72: | Assert::that($data['completion']) |
| 73: | ->isArray('"result.completion" must be an object, {type} given.') |
| 74: | ->isMap('"result.completion" must be a string-keyed object.') |
| 75: | ; |
| 76: | |
| 77: | Assert::that($data['completion'])->hasOffset('values', '"result" is missing the required "completion.values" key.'); |
| 78: | Assert::that($data['completion']['values']) |
| 79: | ->isList('"result.completion.values" must be a list, {type} given.') |
| 80: | ->values()->isString('each "result.completion.values" must be a string, {type} given.') |
| 81: | ; |
| 82: | |
| 83: | $completion = ['values' => $data['completion']['values']]; |
| 84: | |
| 85: | if (\array_key_exists('total', $data['completion'])) { |
| 86: | Assert::that($data['completion']['total'])->isInt('"result.completion.total" must be an int, {type} given.'); |
| 87: | $completion['total'] = $data['completion']['total']; |
| 88: | } |
| 89: | |
| 90: | if (\array_key_exists('hasMore', $data['completion'])) { |
| 91: | Assert::that($data['completion']['hasMore'])->isBool('"result.completion.hasMore" must be a bool, {type} given.'); |
| 92: | $completion['hasMore'] = $data['completion']['hasMore']; |
| 93: | } |
| 94: | |
| 95: | $meta = new GenericResultMetaObject(); |
| 96: | |
| 97: | if (\array_key_exists('_meta', $data)) { |
| 98: | Assert::that($data['_meta']) |
| 99: | ->isArray('"result._meta" must be an object, {type} given.') |
| 100: | ->isMap('"result._meta" must be a string-keyed object.') |
| 101: | ; |
| 102: | $meta = GenericResultMetaObject::fromArray($data['_meta']); |
| 103: | } |
| 104: | |
| 105: | return new self(completion: $completion, meta: $meta); |
| 106: | } |
| 107: | |
| 108: | #[\Override] |
| 109: | public function toArray(): array |
| 110: | { |
| 111: | $data = []; |
| 112: | $meta = $this->meta->toArray(); |
| 113: | |
| 114: | if ([] !== $meta) { |
| 115: | $data['_meta'] = $meta; |
| 116: | } |
| 117: | |
| 118: | $data['resultType'] = self::getResultType(); |
| 119: | $data['completion'] = $this->completion; |
| 120: | |
| 121: | return $data; |
| 122: | } |
| 123: | |
| 124: | #[\Override] |
| 125: | public function rebuildWithMeta(ResultMetaObject $meta): static |
| 126: | { |
| 127: | return new self(completion: $this->completion, meta: $meta); |
| 128: | } |
| 129: | |
| 130: | #[\Override] |
| 131: | protected function getResultType(): string |
| 132: | { |
| 133: | return ResultType::Complete->value; |
| 134: | } |
| 135: | } |
| 136: | |