| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | namespace Nexus\Mcp\Core\Schema; |
| 15: | |
| 16: | use Nexus\Assert\Assert; |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | final readonly class RequestMetaObject implements Arrayable |
| 27: | { |
| 28: | |
| 29: | |
| 30: | |
| 31: | public function __construct(public ?ProgressToken $progressToken = null, public array $extras = []) |
| 32: | { |
| 33: | } |
| 34: | |
| 35: | #[\Override] |
| 36: | public static function fromArray(array $data): static |
| 37: | { |
| 38: | $progressToken = null; |
| 39: | |
| 40: | if (\array_key_exists('progressToken', $data)) { |
| 41: | $raw = $data['progressToken']; |
| 42: | Assert::that($raw)->isArrayKey('"_meta.progressToken" must be an int or string, {type} given.'); |
| 43: | |
| 44: | $progressToken = new ProgressToken($raw); |
| 45: | unset($data['progressToken']); |
| 46: | } |
| 47: | |
| 48: | return new self($progressToken, $data); |
| 49: | } |
| 50: | |
| 51: | #[\Override] |
| 52: | public function toArray(): array |
| 53: | { |
| 54: | $out = $this->extras; |
| 55: | |
| 56: | if (null !== $this->progressToken) { |
| 57: | $out['progressToken'] = $this->progressToken->token; |
| 58: | } |
| 59: | |
| 60: | return $out; |
| 61: | } |
| 62: | |
| 63: | #[\Override] |
| 64: | public function jsonSerialize(): array|\stdClass |
| 65: | { |
| 66: | $data = $this->toArray(); |
| 67: | |
| 68: | return [] === $data ? new \stdClass() : $data; |
| 69: | } |
| 70: | } |
| 71: | |