38 private array $idToEnum = [];
44 private array $enumToId = [];
46 public function __construct(){
47 foreach(GameMode::cases() as $case){
48 $this->
register(match($case){
49 GameMode::SURVIVAL => 0,
50 GameMode::CREATIVE => 1,
51 GameMode::ADVENTURE => 2,
52 GameMode::SPECTATOR => 3,
57 private function register(
int $id, GameMode $type) :
void{
58 $this->idToEnum[$id] = $type;
59 $this->enumToId[spl_object_id($type)] = $id;
62 public function fromId(
int $id) : ?GameMode{
63 return $this->idToEnum[$id] ??
null;
66 public function toId(GameMode $type) :
int{
67 $k = spl_object_id($type);
68 if(!array_key_exists($k, $this->enumToId)){
69 throw new \InvalidArgumentException(
"Game mode $type->name does not have a mapped ID");
71 return $this->enumToId[$k];