PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
Loading...
Searching...
No Matches
LegacyToStringIdMap.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\data\bedrock;
25
28use function is_array;
29use function is_int;
30use function is_string;
31use function json_decode;
32
33abstract class LegacyToStringIdMap{
34
39 private array $legacyToString = [];
40
41 public function __construct(string $file){
42 $stringToLegacyId = json_decode(Filesystem::fileGetContents($file), true);
43 if(!is_array($stringToLegacyId)){
44 throw new AssumptionFailedError("Invalid format of ID map");
45 }
46 foreach($stringToLegacyId as $stringId => $legacyId){
47 if(!is_string($stringId) || !is_int($legacyId)){
48 throw new AssumptionFailedError("ID map should have string keys and int values");
49 }
50 $this->legacyToString[$legacyId] = $stringId;
51 }
52 }
53
54 public function legacyToString(int $legacy) : ?string{
55 return $this->legacyToString[$legacy] ?? null;
56 }
57
62 public function getLegacyToStringMap() : array{
63 return $this->legacyToString;
64 }
65
66 public function add(string $string, int $legacy) : void{
67 if(isset($this->legacyToString[$legacy])){
68 if($this->legacyToString[$legacy] === $string){
69 return;
70 }
71 throw new \InvalidArgumentException("Legacy ID $legacy is already mapped to string " . $this->legacyToString[$legacy]);
72 }
73 $this->legacyToString[$legacy] = $string;
74 }
75}