PocketMine-MP 5.32.2 git-1ebd7d3960d713d56f77f610fe0c15cdee201069
Loading...
Searching...
No Matches
EnumFromRawStateMap.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\block\convert\property;
25
26use function spl_object_id;
27
38 private array $enumToValue = [];
39
44 private array $valueToEnum = [];
45
51 public function __construct(
52 string $class,
53 \Closure $mapper,
54 ?\Closure $aliasMapper = null
55 ){
56 foreach($class::cases() as $case){
57 $int = $mapper($case);
58 $this->valueToEnum[$int] = $case;
59 $this->enumToValue[spl_object_id($case)] = $int;
60
61 if($aliasMapper !== null){
62 $aliases = $aliasMapper($case);
63 foreach($aliases as $alias){
64 $this->valueToEnum[$alias] = $case;
65 }
66 }
67 }
68 }
69
80 public static function string(string $class, \Closure $mapper, ?\Closure $aliasMapper = null) : self{ return new self($class, $mapper, $aliasMapper); }
81
92 public static function int(string $class, \Closure $mapper, ?\Closure $aliasMapper = null) : self{ return new self($class, $mapper, $aliasMapper); }
93
94 public function getRawToValueMap() : array{
95 return $this->valueToEnum;
96 }
97
98 public function valueToRaw(mixed $value) : int|string{
99 return $this->enumToValue[spl_object_id($value)];
100 }
101
102 public function rawToValue(int|string $raw) : ?\UnitEnum{
103 return $this->valueToEnum[$raw] ?? null;
104 }
105
106 public function printableValue(mixed $value) : string{
107 return $value::class . "::" . $value->name;
108 }
109}
__construct(string $class, \Closure $mapper, ?\Closure $aliasMapper=null)
static string(string $class, \Closure $mapper, ?\Closure $aliasMapper=null)
static int(string $class, \Closure $mapper, ?\Closure $aliasMapper=null)