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\Assert; |
12
|
|
|
use Daikon\Interop\Assertion; |
13
|
|
|
use Daikon\Interop\MakeEmptyInterface; |
14
|
|
|
use Daikon\ValueObject\ValueObjectInterface; |
15
|
|
|
|
16
|
|
|
final class Address implements MakeEmptyInterface, ValueObjectInterface |
17
|
|
|
{ |
18
|
|
|
private ?string $address; |
19
|
|
|
|
20
|
|
|
/** @param string|null $value */ |
21
|
6 |
|
public static function fromNative($value): self |
22
|
|
|
{ |
23
|
|
|
//@todo improve bitcoin address validation |
24
|
6 |
|
Assert::that($value, 'Must be a valid string.')->nullOr()->string()->notBlank(); |
25
|
6 |
|
return new self($value); |
26
|
|
|
} |
27
|
|
|
|
28
|
3 |
|
public static function makeEmpty(): self |
29
|
|
|
{ |
30
|
3 |
|
return new self; |
31
|
|
|
} |
32
|
|
|
|
33
|
2 |
|
public function isEmpty(): bool |
34
|
|
|
{ |
35
|
2 |
|
return $this->address === null; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** @param self $comparator */ |
39
|
1 |
|
public function equals($comparator): bool |
40
|
|
|
{ |
41
|
1 |
|
Assertion::isInstanceOf($comparator, self::class); |
42
|
1 |
|
return $this->toNative() === $comparator->toNative(); |
43
|
|
|
} |
44
|
|
|
|
45
|
3 |
|
public function toNative(): ?string |
46
|
|
|
{ |
47
|
3 |
|
return $this->address; |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
public function __toString(): string |
51
|
|
|
{ |
52
|
2 |
|
return (string)$this->address; |
53
|
|
|
} |
54
|
|
|
|
55
|
6 |
|
private function __construct(?string $address = null) |
56
|
|
|
{ |
57
|
6 |
|
$this->address = $address; |
58
|
6 |
|
} |
59
|
|
|
} |
60
|
|
|
|