1: <?php declare( strict_types = 1 );
2:
3: namespace Waves\Model;
4:
5: use Exception;
6: use Waves\Common\ExceptionCode;
7:
8: class ChainId
9: {
10: const MAINNET = 'W';
11: const TESTNET = 'T';
12: const STAGENET = 'S';
13: const PRIVATE = 'R';
14:
15: private string $chainId;
16:
17: private function __construct(){}
18:
19: static function fromInt( int $int ): ChainId
20: {
21: if( $int < 0 || $int > 255 )
22: throw new Exception( __FUNCTION__ . ' bad chainId value: ' . $int, ExceptionCode::BAD_CHAINID );
23: $chainId = new ChainId;
24: $chainId->chainId = chr( $int );
25: return $chainId;
26: }
27:
28: static function fromString( string $string ): ChainId
29: {
30: if( strlen( $string ) !== 1 )
31: throw new Exception( __FUNCTION__ . ' bad chainId value: ' . strlen( $string ), ExceptionCode::BAD_CHAINID );
32: $chainId = new ChainId;
33: $chainId->chainId = $string;
34: return $chainId;
35: }
36:
37: static function MAINNET(): ChainId
38: {
39: static $chainId;
40: if( !isset( $chainId ) )
41: $chainId = ChainId::fromString( ChainId::MAINNET );
42: return $chainId;
43: }
44:
45: static function TESTNET(): ChainId
46: {
47: static $chainId;
48: if( !isset( $chainId ) )
49: $chainId = ChainId::fromString( ChainId::TESTNET );
50: return $chainId;
51: }
52:
53: static function STAGENET(): ChainId
54: {
55: static $chainId;
56: if( !isset( $chainId ) )
57: $chainId = ChainId::fromString( ChainId::STAGENET );
58: return $chainId;
59: }
60:
61: static function PRIVATE(): ChainId
62: {
63: static $chainId;
64: if( !isset( $chainId ) )
65: $chainId = ChainId::fromString( ChainId::PRIVATE );
66: return $chainId;
67: }
68:
69: function asInt(): int
70: {
71: return ord( $this->chainId );
72: }
73:
74: function asString(): string
75: {
76: return $this->chainId;
77: }
78: }
79: