|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the ngutech/bitcoin-interop project. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace NGUtech\Bitcoin\ValueObject; |
|
10
|
|
|
|
|
11
|
|
|
use Daikon\Interop\Assertion; |
|
12
|
|
|
use Daikon\Interop\MakeEmptyInterface; |
|
13
|
|
|
use Daikon\ValueObject\ValueObjectInterface; |
|
14
|
|
|
|
|
15
|
|
|
final class Address implements MakeEmptyInterface, ValueObjectInterface |
|
16
|
|
|
{ |
|
17
|
|
|
private string $address; |
|
18
|
|
|
|
|
19
|
|
|
/** @param string|null $value */ |
|
20
|
5 |
|
public static function fromNative($value): self |
|
21
|
|
|
{ |
|
22
|
|
|
//@todo improve address validation |
|
23
|
5 |
|
Assertion::nullOrString($value, 'Must be a string.'); |
|
24
|
5 |
|
return is_null($value) ? new self : new self($value); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
3 |
|
public static function makeEmpty(): self |
|
28
|
|
|
{ |
|
29
|
3 |
|
return new self; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
2 |
|
public function isEmpty(): bool |
|
33
|
|
|
{ |
|
34
|
2 |
|
return $this->address === ''; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** @param self $comparator */ |
|
38
|
1 |
|
public function equals($comparator): bool |
|
39
|
|
|
{ |
|
40
|
1 |
|
Assertion::isInstanceOf($comparator, self::class); |
|
41
|
1 |
|
return $this->toNative() === $comparator->toNative(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
3 |
|
public function toNative(): string |
|
45
|
|
|
{ |
|
46
|
3 |
|
return $this->address; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
2 |
|
public function __toString(): string |
|
50
|
|
|
{ |
|
51
|
2 |
|
return (string)$this->address; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
5 |
|
private function __construct(string $address = '') |
|
55
|
|
|
{ |
|
56
|
5 |
|
$this->address = $address; |
|
57
|
5 |
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|