1: <?php declare( strict_types = 1 );
2:
3: namespace Waves\Model;
4:
5: use Exception;
6: use Waves\Common\ExceptionCode;
7: use Waves\Common\Base58String;
8:
9: class AssetId
10: {
11: const BYTE_LENGTH = 32;
12: const WAVES_STRING = "WAVES";
13:
14: private Base58String $assetId;
15:
16: private function __construct(){}
17:
18: static function WAVES(): AssetId
19: {
20: return new AssetId;
21: }
22:
23: static function fromString( string $encoded ): AssetId
24: {
25: if( strtoupper( $encoded ) === AssetId::WAVES_STRING )
26: return AssetId::WAVES();
27:
28: $assetId = new AssetId;
29: $assetId->assetId = Base58String::fromString( $encoded );
30: return $assetId;
31: }
32:
33: static function fromBytes( string $bytes ): AssetId
34: {
35: if( $bytes === '' )
36: return AssetId::WAVES();
37:
38: if( strlen( $bytes ) !== AssetId::BYTE_LENGTH )
39: throw new Exception( __FUNCTION__ . ' bad asset length: ' . strlen( $bytes ), ExceptionCode::BAD_ASSET );
40: $assetId = new AssetId;
41: $assetId->assetId = Base58String::fromBytes( $bytes );
42: return $assetId;
43: }
44:
45: function isWaves(): bool
46: {
47: return !isset( $this->assetId );
48: }
49:
50: function bytes(): string
51: {
52: if( $this->isWaves() )
53: return '';
54: $bytes = $this->assetId->bytes();
55: if( strlen( $bytes ) !== AssetId::BYTE_LENGTH )
56: throw new Exception( __FUNCTION__ . ' bad asset length: ' . strlen( $bytes ), ExceptionCode::BAD_ASSET );
57: return $bytes;
58: }
59:
60: function encoded(): string
61: {
62: return $this->isWaves() ? AssetId::WAVES_STRING : $this->assetId->encoded();
63: }
64:
65: function toString(): string
66: {
67: return $this->encoded();
68: }
69:
70: /**
71: * @return string|null
72: */
73: function toJsonValue()
74: {
75: return $this->isWaves() ? null : $this->encoded();
76: }
77: }
78: