| 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\Arrayable; |
| 18: | use Nexus\Mcp\Core\Schema\Cursor; |
| 19: | use Nexus\Mcp\Core\Schema\Enum\CacheScope; |
| 20: | use Nexus\Mcp\Core\Schema\Enum\ResultType; |
| 21: | use Nexus\Mcp\Core\Schema\MetaObject; |
| 22: | use Nexus\Mcp\Core\Schema\MetaObject\GenericResultMetaObject; |
| 23: | use Nexus\Mcp\Core\Schema\MetaObject\ResultMetaObject; |
| 24: | use Nexus\Mcp\Core\Schema\Resource\ResourceTemplate; |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | final readonly class ListResourceTemplatesResult extends PaginatedResult implements ServerResult |
| 41: | { |
| 42: | |
| 43: | |
| 44: | |
| 45: | public function __construct( |
| 46: | public array $resourceTemplates, |
| 47: | int $ttlMs, |
| 48: | CacheScope $cacheScope, |
| 49: | ?Cursor $nextCursor = null, |
| 50: | ResultMetaObject $meta = new GenericResultMetaObject(), |
| 51: | ) { |
| 52: | Assert::that($resourceTemplates) |
| 53: | ->isList('"result.resourceTemplates" must be a list, non-list array given.') |
| 54: | ->values()->isInstanceOf(ResourceTemplate::class) |
| 55: | ; |
| 56: | |
| 57: | parent::__construct(ttlMs: $ttlMs, cacheScope: $cacheScope, nextCursor: $nextCursor, meta: $meta); |
| 58: | } |
| 59: | |
| 60: | #[\Override] |
| 61: | public static function fromArray(array $data): static |
| 62: | { |
| 63: | Assert::that($data)->hasOffset('resourceTemplates', '"result" is missing the required "resourceTemplates" key.'); |
| 64: | Assert::that($data['resourceTemplates']) |
| 65: | ->isList('"result.resourceTemplates" must be a list, {type} given.') |
| 66: | ->values() |
| 67: | ->isArray('each "result.resourceTemplate" must be an object, {type} given.') |
| 68: | ->isMap('each "result.resourceTemplate" must be a string-keyed object.') |
| 69: | ; |
| 70: | $resourceTemplates = array_map(ResourceTemplate::fromArray(...), $data['resourceTemplates']); |
| 71: | |
| 72: | Assert::that($data)->hasOffset('ttlMs', '"result" is missing the required "ttlMs" key.'); |
| 73: | $ttlMs = $data['ttlMs']; |
| 74: | Assert::that($ttlMs)->isInt('"result.ttlMs" must be an integer, {type} given.'); |
| 75: | |
| 76: | Assert::that($data)->hasOffset('cacheScope', '"result" is missing the required "cacheScope" key.'); |
| 77: | Assert::that($data['cacheScope'])->isOneOf(array_column(CacheScope::cases(), 'value'), '"result.cacheScope" must be one of {choices}, {value} given.'); |
| 78: | $cacheScope = CacheScope::from($data['cacheScope']); |
| 79: | |
| 80: | $nextCursor = null; |
| 81: | |
| 82: | if (\array_key_exists('nextCursor', $data)) { |
| 83: | $raw = $data['nextCursor']; |
| 84: | Assert::that($raw)->isNonEmptyString('"result.nextCursor" must be a non-empty string, {type} given.'); |
| 85: | $nextCursor = new Cursor(cursor: $raw); |
| 86: | } |
| 87: | |
| 88: | $meta = new GenericResultMetaObject(); |
| 89: | |
| 90: | if (\array_key_exists('_meta', $data)) { |
| 91: | Assert::that($data['_meta']) |
| 92: | ->isArray('"result._meta" must be an object, {type} given.') |
| 93: | ->not()->isNonEmptyList('"result._meta" must be a string-keyed object.') |
| 94: | ; |
| 95: | $meta = GenericResultMetaObject::fromArray($data['_meta']); |
| 96: | } |
| 97: | |
| 98: | return new self( |
| 99: | resourceTemplates: $resourceTemplates, |
| 100: | ttlMs: $ttlMs, |
| 101: | cacheScope: $cacheScope, |
| 102: | nextCursor: $nextCursor, |
| 103: | meta: $meta, |
| 104: | ); |
| 105: | } |
| 106: | |
| 107: | #[\Override] |
| 108: | public function toArray(): array |
| 109: | { |
| 110: | $data = []; |
| 111: | $meta = $this->meta->toArray(); |
| 112: | |
| 113: | if ([] !== $meta) { |
| 114: | $data['_meta'] = $meta; |
| 115: | } |
| 116: | |
| 117: | $data['resultType'] = self::getResultType(); |
| 118: | $data['resourceTemplates'] = array_map( |
| 119: | static fn(ResourceTemplate $template): array => $template->toArray(), |
| 120: | $this->resourceTemplates, |
| 121: | ); |
| 122: | |
| 123: | if (null !== $this->nextCursor) { |
| 124: | $data['nextCursor'] = $this->nextCursor->cursor; |
| 125: | } |
| 126: | |
| 127: | $data['ttlMs'] = $this->ttlMs; |
| 128: | $data['cacheScope'] = $this->cacheScope->value; |
| 129: | |
| 130: | return $data; |
| 131: | } |
| 132: | |
| 133: | #[\Override] |
| 134: | public function rebuildWithMeta(ResultMetaObject $meta): static |
| 135: | { |
| 136: | return new self( |
| 137: | resourceTemplates: $this->resourceTemplates, |
| 138: | ttlMs: $this->ttlMs, |
| 139: | cacheScope: $this->cacheScope, |
| 140: | nextCursor: $this->nextCursor, |
| 141: | meta: $meta, |
| 142: | ); |
| 143: | } |
| 144: | |
| 145: | #[\Override] |
| 146: | protected function getResultType(): string |
| 147: | { |
| 148: | return ResultType::Complete->value; |
| 149: | } |
| 150: | } |
| 151: | |