1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the daikon-cqrs/value-object 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 Daikon\ValueObject; |
10
|
|
|
|
11
|
|
|
use Daikon\Interop\Assertion; |
12
|
|
|
use Daikon\Interop\MakeEmptyInterface; |
13
|
|
|
|
14
|
|
|
final class Address implements ValueObjectInterface, MakeEmptyInterface |
15
|
|
|
{ |
16
|
|
|
private ?string $name; |
17
|
|
|
|
18
|
|
|
private ?string $address1; |
19
|
|
|
|
20
|
|
|
private ?string $address2; |
21
|
|
|
|
22
|
|
|
private ?string $city; |
23
|
|
|
|
24
|
|
|
private ?string $postcode; |
25
|
|
|
|
26
|
|
|
private ?string $country; |
27
|
|
|
|
28
|
|
|
/** @param self $comparator */ |
29
|
|
|
public function equals($comparator): bool |
30
|
|
|
{ |
31
|
|
|
Assertion::isInstanceOf($comparator, self::class); |
32
|
|
|
return $this->toNative() === $comparator->toNative(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** @param array $value */ |
36
|
|
|
public static function fromNative($value): self |
37
|
|
|
{ |
38
|
|
|
Assertion::isArray($value, 'Trying to create address from unsupported value type.'); |
39
|
|
|
return new self( |
40
|
|
|
$value['name'] ?? null, |
41
|
|
|
$value['address1'] ?? null, |
42
|
|
|
$value['address2'] ?? null, |
43
|
|
|
$value['city'] ?? null, |
44
|
|
|
$value['postcode'] ?? null, |
45
|
|
|
$value['country'] ?? null |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function toNative(): array |
50
|
|
|
{ |
51
|
|
|
return array_filter([ |
52
|
|
|
'name' => $this->name, |
53
|
|
|
'address1' => $this->address1, |
54
|
|
|
'address2' => $this->address2, |
55
|
|
|
'city' => $this->city, |
56
|
|
|
'postcode' => $this->postcode, |
57
|
|
|
'country' => $this->country |
58
|
|
|
]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public static function makeEmpty(): self |
62
|
|
|
{ |
63
|
|
|
return new self; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function isEmpty(): bool |
67
|
|
|
{ |
68
|
|
|
return empty($this->toNative()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function __toString(): string |
72
|
|
|
{ |
73
|
|
|
return implode(PHP_EOL, $this->toNative()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function __construct( |
77
|
|
|
string $name = null, |
78
|
|
|
string $address1 = null, |
79
|
|
|
string $address2 = null, |
80
|
|
|
string $city = null, |
81
|
|
|
string $postcode = null, |
82
|
|
|
string $country = null |
83
|
|
|
) { |
84
|
|
|
$this->name = $name; |
85
|
|
|
$this->address1 = $address1; |
86
|
|
|
$this->address2 = $address2; |
87
|
|
|
$this->city = $city; |
88
|
|
|
$this->postcode = $postcode; |
89
|
|
|
$this->country = $country; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|