*/ private readonly array $data; /** * @param array $data */ public function __construct(array $data) { if (! array_key_exists(self::TYPE, $data)) { throw new InvalidArgumentException('Invalid key: the type is not defined'); } $this->data = $data; } /** * @param array $data */ public static function create(array $data): self { return new self($data); } /** * @param array $data */ public static function createFromData(array $data): self { if (! array_key_exists(self::TYPE, $data)) { throw new InvalidArgumentException('Invalid key: the type is not defined'); } return match ($data[self::TYPE]) { '1' => new OkpKey($data), '2' => new Ec2Key($data), '3' => new RsaKey($data), '4' => new SymmetricKey($data), default => self::create($data), }; } public function type(): int|string { return $this->data[self::TYPE]; } public function alg(): int { return (int) $this->get(self::ALG); } /** * @return array */ public function getData(): array { return $this->data; } public function has(int|string $key): bool { return array_key_exists($key, $this->data); } public function get(int|string $key): mixed { if (! array_key_exists($key, $this->data)) { throw new InvalidArgumentException(sprintf('The key has no data at index %d', $key)); } return $this->data[$key]; } }