| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | namespace Nexus\Mcp\Extension\Apps\Schema; |
| 15: | |
| 16: | use Nexus\Mcp\Core\Schema\Arrayable; |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | final readonly class UiResourcePermissions implements Arrayable |
| 31: | { |
| 32: | public function __construct( |
| 33: | public bool $camera = false, |
| 34: | public bool $microphone = false, |
| 35: | public bool $geolocation = false, |
| 36: | public bool $clipboardWrite = false, |
| 37: | ) { |
| 38: | } |
| 39: | |
| 40: | #[\Override] |
| 41: | public static function fromArray(array $data): static |
| 42: | { |
| 43: | return new self( |
| 44: | camera: self::parseRequested($data, 'camera'), |
| 45: | microphone: self::parseRequested($data, 'microphone'), |
| 46: | geolocation: self::parseRequested($data, 'geolocation'), |
| 47: | clipboardWrite: self::parseRequested($data, 'clipboardWrite'), |
| 48: | ); |
| 49: | } |
| 50: | |
| 51: | #[\Override] |
| 52: | public function toArray(): array |
| 53: | { |
| 54: | $data = []; |
| 55: | |
| 56: | if ($this->camera) { |
| 57: | $data['camera'] = new \stdClass(); |
| 58: | } |
| 59: | |
| 60: | if ($this->microphone) { |
| 61: | $data['microphone'] = new \stdClass(); |
| 62: | } |
| 63: | |
| 64: | if ($this->geolocation) { |
| 65: | $data['geolocation'] = new \stdClass(); |
| 66: | } |
| 67: | |
| 68: | if ($this->clipboardWrite) { |
| 69: | $data['clipboardWrite'] = new \stdClass(); |
| 70: | } |
| 71: | |
| 72: | return $data; |
| 73: | } |
| 74: | |
| 75: | #[\Override] |
| 76: | public function jsonSerialize(): array|\stdClass |
| 77: | { |
| 78: | $data = $this->toArray(); |
| 79: | |
| 80: | return [] === $data ? new \stdClass() : $data; |
| 81: | } |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | private static function parseRequested(array $data, string $slot): bool |
| 90: | { |
| 91: | if (! \array_key_exists($slot, $data)) { |
| 92: | return false; |
| 93: | } |
| 94: | |
| 95: | $value = $data[$slot]; |
| 96: | |
| 97: | if (! \is_array($value) && ! $value instanceof \stdClass) { |
| 98: | throw new \InvalidArgumentException(\sprintf( |
| 99: | '"_meta.ui.permissions.%s" must be an object, %s given.', |
| 100: | $slot, |
| 101: | get_debug_type($value), |
| 102: | )); |
| 103: | } |
| 104: | |
| 105: | return true; |
| 106: | } |
| 107: | } |
| 108: | |