PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
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
29use function is_array;
30use function is_int;
31use function is_string;
32use function json_decode;
33
34abstract class LegacyToStringIdMap{
35
40 private array $legacyToString = [];
41
42 public function __construct(string $file){
43 $stringToLegacyId = json_decode(Filesystem::fileGetContents($file), true);
44 if(!is_array($stringToLegacyId)){
45 throw new AssumptionFailedError("Invalid format of ID map");
46 }
47 foreach(Utils::promoteKeys($stringToLegacyId) as $stringId => $legacyId){
48 if(!is_string($stringId) || !is_int($legacyId)){
49 throw new AssumptionFailedError("ID map should have string keys and int values");
50 }
51 $this->legacyToString[$legacyId] = $stringId;
52 }
53 }
54
55 public function legacyToString(int $legacy) : ?string{
56 return $this->legacyToString[$legacy] ?? null;
57 }
58
63 public function getLegacyToStringMap() : array{
64 return $this->legacyToString;
65 }
66
67 public function add(string $string, int $legacy) : void{
68 if(isset($this->legacyToString[$legacy])){
69 if($this->legacyToString[$legacy] === $string){
70 return;
71 }
72 throw new \InvalidArgumentException("Legacy ID $legacy is already mapped to string " . $this->legacyToString[$legacy]);
73 }
74 $this->legacyToString[$legacy] = $string;
75 }
76}