| 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: | |
| 27: | |
| 28: | |
| 29: | final readonly class Root implements Arrayable |
| 30: | { |
| 31: | public function __construct( |
| 32: | public string $uri, |
| 33: | public ?string $name = null, |
| 34: | public MetaObject $meta = new MetaObject(), |
| 35: | ) { |
| 36: | Assert::that($uri)->startsWith('file://', 'root "uri" must start with {needle}, got {value}.'); |
| 37: | } |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | #[\Override] |
| 43: | public static function fromArray(array $data): static |
| 44: | { |
| 45: | Assert::that($data)->hasOffset('uri', 'root missing the required "uri" key.'); |
| 46: | $uri = $data['uri']; |
| 47: | Assert::that($uri)->isString('root "uri" must be a string, {type} given.'); |
| 48: | |
| 49: | $name = $data['name'] ?? null; |
| 50: | Assert::that($name)->nullOr()->isString('root "name" must be a string or null, {type} given.'); |
| 51: | |
| 52: | $meta = new MetaObject(); |
| 53: | |
| 54: | if (\array_key_exists('_meta', $data)) { |
| 55: | Assert::that($data['_meta']) |
| 56: | ->isArray('root "_meta" must be an object, {type} given.') |
| 57: | ->isMap('root "_meta" must be a string-keyed object.') |
| 58: | ; |
| 59: | $meta = MetaObject::fromArray($data['_meta']); |
| 60: | } |
| 61: | |
| 62: | return new self($uri, $name, $meta); |
| 63: | } |
| 64: | |
| 65: | #[\Override] |
| 66: | public function toArray(): array |
| 67: | { |
| 68: | $data = ['uri' => $this->uri]; |
| 69: | |
| 70: | if (null !== $this->name) { |
| 71: | $data['name'] = $this->name; |
| 72: | } |
| 73: | |
| 74: | $meta = $this->meta->toArray(); |
| 75: | |
| 76: | if ([] !== $meta) { |
| 77: | $data['_meta'] = $meta; |
| 78: | } |
| 79: | |
| 80: | return $data; |
| 81: | } |
| 82: | |
| 83: | #[\Override] |
| 84: | public function jsonSerialize(): array |
| 85: | { |
| 86: | return $this->toArray(); |
| 87: | } |
| 88: | } |
| 89: | |