1: <?php declare( strict_types = 1 );
2:
3: namespace Waves\Common;
4:
5: use Exception;
6: use Waves\Common\ExceptionCode;
7: use Waves\Model\ArgMeta;
8: use Waves\Account\Address;
9: use Waves\Account\PublicKey;
10: use Waves\Model\AssetId;
11: use Waves\Model\Id;
12: use Waves\Model\ChainId;
13: use Waves\Model\ApplicationStatus;
14: use Waves\Model\Status;
15: use Waves\Transactions\Recipient;
16:
17: class Value
18: {
19: /**
20: * @var mixed
21: */
22: private $value;
23:
24: /**
25: * @param mixed $value
26: */
27: private function __construct( $value )
28: {
29: $this->value = $value;
30: }
31:
32: /**
33: * Value function constructor
34: *
35: * @param mixed $value
36: * @return Value
37: */
38: static function as( $value ): Value
39: {
40: return new Value( $value );
41: }
42:
43: /**
44: * Gets an boolean value
45: *
46: * @return bool
47: */
48: function asBoolean(): bool
49: {
50: if( !is_bool( $this->value ) )
51: throw new Exception( __FUNCTION__ . ' failed to detect boolean at `' . json_encode( $this->value ) . '`', ExceptionCode::BOOL_EXPECTED );
52: return $this->value;
53: }
54:
55: /**
56: * Gets an integer value
57: *
58: * @return int
59: */
60: function asInt(): int
61: {
62: if( !is_int( $this->value ) )
63: {
64: if( is_string( $this->value ) )
65: {
66: $intval = intval( $this->value );
67: if( strval( $intval ) === $this->value )
68: return $intval;
69: }
70: throw new Exception( __FUNCTION__ . ' failed to detect integer at `' . json_encode( $this->value ) . '`', ExceptionCode::INT_EXPECTED );
71: }
72: return $this->value;
73: }
74:
75: /**
76: * Gets a string value
77: *
78: * @return string
79: */
80: function asString(): string
81: {
82: if( !is_string( $this->value ) )
83: throw new Exception( __FUNCTION__ . ' failed to detect string at `' . json_encode( $this->value ) . '`', ExceptionCode::STRING_EXPECTED );
84: return $this->value;
85: }
86:
87: function asBase64String(): Base64String
88: {
89: if( !is_string( $this->value ) )
90: throw new Exception( __FUNCTION__ . ' failed to detect string at `' . json_encode( $this->value ) . '`', ExceptionCode::STRING_EXPECTED );
91: return Base64String::fromString( $this->value );
92: }
93:
94: function asChainId(): ChainId
95: {
96: if( is_int( $this->value ) )
97: return ChainId::fromInt( $this->value );
98: return ChainId::fromString( $this->asString() );
99: }
100:
101: /**
102: * Gets a base64 decoded string value
103: *
104: * @return string
105: */
106: function asBase64Decoded(): string
107: {
108: if( !is_string( $this->value ) )
109: throw new Exception( __FUNCTION__ . ' failed to detect string at `' . json_encode( $this->value ) . '`', ExceptionCode::STRING_EXPECTED );
110: if( substr( $this->value, 0, 7 ) !== 'base64:' )
111: throw new Exception( __FUNCTION__ . ' failed to detect base64 `' . $this->value . '`', ExceptionCode::BASE64_DECODE );
112: $decoded = base64_decode( substr( $this->value, 7 ) );
113: if( !is_string( $decoded ) )
114: throw new Exception( __FUNCTION__ . ' failed to decode base64 `' . substr( $this->value, 7 ) . '`', ExceptionCode::BASE64_DECODE );
115: return $decoded;
116: }
117:
118: function asBase58String(): Base58String
119: {
120: return Base58String::fromString( $this->asString() );
121: }
122:
123: /**
124: * Gets a Json value
125: *
126: * @return Json
127: */
128: function asJson(): Json
129: {
130: if( !is_array( $this->value ) )
131: throw new Exception( __FUNCTION__ . ' failed to detect Json at `' . json_encode( $this->value ) . '`', ExceptionCode::ARRAY_EXPECTED );
132: return Json::as( $this->value );
133: }
134:
135: /**
136: * Gets an array value
137: *
138: * @return array<mixed, mixed>
139: */
140: function asArray(): array
141: {
142: if( !is_array( $this->value ) )
143: throw new Exception( __FUNCTION__ . ' failed to detect array at `' . json_encode( $this->value ) . '`', ExceptionCode::ARRAY_EXPECTED );
144: return $this->value;
145: }
146:
147: /**
148: * Gets an array of integers value
149: *
150: * @return array<int, int>
151: */
152: function asArrayInt(): array
153: {
154: if( !is_array( $this->value ) )
155: throw new Exception( __FUNCTION__ . ' failed to detect array at `' . json_encode( $this->value ) . '`', ExceptionCode::ARRAY_EXPECTED );
156: $ints = [];
157: foreach( $this->value as $value )
158: $ints[] = Value::as( $value )->asInt();
159: return $ints;
160: }
161:
162: /**
163: * @return array<int, string>
164: */
165: function asArrayString(): array
166: {
167: if( !is_array( $this->value ) )
168: throw new Exception( __FUNCTION__ . ' failed to detect array at `' . json_encode( $this->value ) . '`', ExceptionCode::ARRAY_EXPECTED );
169: $strings = [];
170: foreach( $this->value as $value )
171: $strings[] = Value::as( $value )->asString();
172: return $strings;
173: }
174:
175: /**
176: * Gets an array of string to integer map
177: *
178: * @return array<string, int>
179: */
180: function asMapStringInt(): array
181: {
182: if( !is_array( $this->value ) )
183: throw new Exception( __FUNCTION__ . ' failed to detect array at `' . json_encode( $this->value ) . '`', ExceptionCode::ARRAY_EXPECTED );
184: $ints = [];
185: foreach( $this->value as $key => $value )
186: $ints[Value::as( $key )->asString()] = Value::as( $value )->asInt();
187: return $ints;
188: }
189:
190: function asArgMeta(): ArgMeta
191: {
192: return new ArgMeta( $this->asJson() );
193: }
194:
195: /**
196: * Gets an Address value
197: *
198: * @return Address
199: */
200: function asAddress(): Address
201: {
202: return Address::fromString( $this->asString() );
203: }
204:
205: /**
206: * @return Recipient
207: */
208: function asRecipient(): Recipient
209: {
210: return Recipient::fromAddressOrAlias( $this->asString() );
211: }
212:
213: function asPublicKey(): PublicKey
214: {
215: return PublicKey::fromString( $this->asString() );
216: }
217:
218: /**
219: * Gets an AssetId value
220: *
221: * @return AssetId
222: */
223: function asAssetId(): AssetId
224: {
225: return isset( $this->value ) ? AssetId::fromString( $this->asString() ) : AssetId::WAVES();
226: }
227:
228: /**
229: * Gets an Id value
230: *
231: * @return Id
232: */
233: function asId(): Id
234: {
235: return Id::fromString( $this->asString() );
236: }
237:
238: function asApplicationStatus(): int
239: {
240: switch( $this->asString() )
241: {
242: case ApplicationStatus::SUCCEEDED_S: return ApplicationStatus::SUCCEEDED;
243: case ApplicationStatus::SCRIPT_EXECUTION_FAILED_S: return ApplicationStatus::SCRIPT_EXECUTION_FAILED;
244: default: return ApplicationStatus::UNKNOWN;
245: }
246: }
247:
248: function asStatus(): int
249: {
250: switch( $this->asString() )
251: {
252: case Status::CONFIRMED_S: return Status::CONFIRMED;
253: case Status::UNCONFIRMED_S: return Status::UNCONFIRMED;
254: case Status::NOT_FOUND_S: return Status::NOT_FOUND;
255: default: return Status::UNKNOWN;
256: }
257: }
258: }
259: