| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | namespace Nexus\Mcp\Core\Schema\NotificationParams; |
| 15: | |
| 16: | use Nexus\Assert\Assert; |
| 17: | use Nexus\Mcp\Core\Schema\Arrayable; |
| 18: | use Nexus\Mcp\Core\Schema\MetaObject; |
| 19: | use Nexus\Mcp\Core\Schema\NotificationParams; |
| 20: | use Nexus\Mcp\Core\Schema\ParsesNumber; |
| 21: | use Nexus\Mcp\Core\Schema\ProgressToken; |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | final readonly class ProgressNotificationParams extends NotificationParams |
| 37: | { |
| 38: | use ParsesNumber; |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | public ?string $message; |
| 44: | |
| 45: | public function __construct( |
| 46: | public ProgressToken $progressToken, |
| 47: | public float $progress, |
| 48: | public ?float $total = null, |
| 49: | ?string $message = null, |
| 50: | MetaObject $meta = new MetaObject(), |
| 51: | ) { |
| 52: | Assert::that($message)->nullOr()->isNonEmptyString('"params.message" must be a non-empty string or null.'); |
| 53: | |
| 54: | $this->message = $message; |
| 55: | |
| 56: | parent::__construct(meta: $meta); |
| 57: | } |
| 58: | |
| 59: | #[\Override] |
| 60: | public static function fromArray(array $data): static |
| 61: | { |
| 62: | Assert::that($data)->hasOffset('progressToken', '"params" is missing the required "progressToken" key.'); |
| 63: | $progressToken = $data['progressToken']; |
| 64: | Assert::that($progressToken)->isArrayKey('"params.progressToken" must be an int or string, {type} given.'); |
| 65: | |
| 66: | Assert::that($data)->hasOffset('progress', '"params" is missing the required "progress" key.'); |
| 67: | $progress = self::parseNumber($data['progress'], '"params.progress" must be a number, {type} given.'); |
| 68: | |
| 69: | $total = $data['total'] ?? null; |
| 70: | |
| 71: | if (null !== $total) { |
| 72: | $total = self::parseNumber($total, '"params.total" must be a number or null, {type} given.'); |
| 73: | } |
| 74: | |
| 75: | $message = $data['message'] ?? null; |
| 76: | Assert::that($message)->nullOr()->isString('"params.message" must be a string or null, {type} given.'); |
| 77: | |
| 78: | $meta = new MetaObject(); |
| 79: | |
| 80: | if (\array_key_exists('_meta', $data)) { |
| 81: | Assert::that($data['_meta']) |
| 82: | ->isArray('"params._meta" must be an object, {type} given.') |
| 83: | ->isMap('"params._meta" must be a string-keyed object.') |
| 84: | ; |
| 85: | $meta = MetaObject::fromArray($data['_meta']); |
| 86: | } |
| 87: | |
| 88: | return new self( |
| 89: | progressToken: new ProgressToken(token: $progressToken), |
| 90: | progress: $progress, |
| 91: | total: $total, |
| 92: | message: $message, |
| 93: | meta: $meta, |
| 94: | ); |
| 95: | } |
| 96: | |
| 97: | #[\Override] |
| 98: | public function toArray(): array |
| 99: | { |
| 100: | $data = []; |
| 101: | $meta = $this->meta->toArray(); |
| 102: | |
| 103: | if ([] !== $meta) { |
| 104: | $data['_meta'] = $meta; |
| 105: | } |
| 106: | |
| 107: | $data['progressToken'] = $this->progressToken->token; |
| 108: | $data['progress'] = $this->progress; |
| 109: | |
| 110: | if (null !== $this->total) { |
| 111: | $data['total'] = $this->total; |
| 112: | } |
| 113: | |
| 114: | if (null !== $this->message) { |
| 115: | $data['message'] = $this->message; |
| 116: | } |
| 117: | |
| 118: | return $data; |
| 119: | } |
| 120: | |
| 121: | #[\Override] |
| 122: | public function jsonSerialize(): array |
| 123: | { |
| 124: | return $this->toArray(); |
| 125: | } |
| 126: | } |
| 127: | |