1: <?php declare( strict_types = 1 );
2:
3: namespace Waves\Transactions;
4:
5: use Exception;
6: use Waves\Account\Address;
7: use Waves\Common\ExceptionCode;
8: use Waves\Model\Alias;
9:
10: class Recipient
11: {
12: private Address $address;
13: private Alias $alias;
14:
15: private function __construct(){}
16:
17: static function fromAddress( Address $address ): Recipient
18: {
19: $recipient = new Recipient;
20: $recipient->address = $address;
21: return $recipient;
22: }
23:
24: static function fromAlias( Alias $alias ): Recipient
25: {
26: $recipient = new Recipient;
27: $recipient->alias = $alias;
28: return $recipient;
29: }
30:
31: static function fromAddressOrAlias( string $addressOrAlias ): Recipient
32: {
33: if( strlen( $addressOrAlias ) === Address::STRING_LENGTH )
34: return Recipient::fromAddress( Address::fromString( $addressOrAlias ) );
35: try
36: {
37: return Recipient::fromAlias( Alias::fromFullAlias( $addressOrAlias ) );
38: }
39: catch( Exception $e )
40: {
41: return Recipient::fromAlias( Alias::fromString( $addressOrAlias ) );
42: }
43: }
44:
45: function isAlias(): bool
46: {
47: return isset( $this->alias );
48: }
49:
50: function toString(): string
51: {
52: if( $this->isAlias() )
53: return $this->alias->toString();
54: return $this->address->toString();
55: }
56:
57: function address(): Address
58: {
59: return $this->address;
60: }
61:
62: function alias(): Alias
63: {
64: return $this->alias;
65: }
66:
67: function toProtobuf(): \Waves\Protobuf\Recipient
68: {
69: $pb_Recipient = new \Waves\Protobuf\Recipient;
70: if( $this->isAlias() )
71: $pb_Recipient->setAlias( $this->alias()->name() );
72: else
73: $pb_Recipient->setPublicKeyHash( $this->address()->publicKeyHash() );
74: return $pb_Recipient;
75: }
76: }
77: