| 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\Cursor; |
| 18: | use Nexus\Mcp\Core\Schema\RequestMetaObject; |
| 19: | use Nexus\Mcp\Core\Schema\RequestParams; |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | final readonly class PaginatedRequestParams extends RequestParams |
| 27: | { |
| 28: | public function __construct(public ?Cursor $cursor = null, RequestMetaObject $meta = new RequestMetaObject()) |
| 29: | { |
| 30: | parent::__construct($meta); |
| 31: | } |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | #[\Override] |
| 37: | public static function fromArray(array $data): static |
| 38: | { |
| 39: | $cursor = null; |
| 40: | |
| 41: | if (\array_key_exists('cursor', $data)) { |
| 42: | $raw = $data['cursor']; |
| 43: | Assert::that($raw)->isString('"params.cursor" must be a string, {type} given.'); |
| 44: | $cursor = new Cursor($raw); |
| 45: | } |
| 46: | |
| 47: | $meta = new RequestMetaObject(); |
| 48: | |
| 49: | if (\array_key_exists('_meta', $data)) { |
| 50: | Assert::that($data['_meta']) |
| 51: | ->isArray('"params._meta" must be an object, {type} given.') |
| 52: | ->isMap('"params._meta" must be a string-keyed object.') |
| 53: | ; |
| 54: | $meta = RequestMetaObject::fromArray($data['_meta']); |
| 55: | } |
| 56: | |
| 57: | return new self($cursor, $meta); |
| 58: | } |
| 59: | |
| 60: | #[\Override] |
| 61: | public function toArray(): array |
| 62: | { |
| 63: | $data = parent::toArray(); |
| 64: | |
| 65: | if (null !== $this->cursor) { |
| 66: | $data['cursor'] = $this->cursor->cursor; |
| 67: | } |
| 68: | |
| 69: | return $data; |
| 70: | } |
| 71: | |
| 72: | #[\Override] |
| 73: | public function jsonSerialize(): array |
| 74: | { |
| 75: | return $this->toArray(); |
| 76: | } |
| 77: | } |
| 78: | |