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
|
|
|
|
13
|
|
|
final class Address implements ValueObjectInterface |
14
|
|
|
{ |
15
|
|
|
private ?string $name; |
16
|
|
|
|
17
|
|
|
private ?string $address1; |
18
|
|
|
|
19
|
|
|
private ?string $address2; |
20
|
|
|
|
21
|
|
|
private ?string $city; |
22
|
|
|
|
23
|
|
|
private ?string $postcode; |
24
|
|
|
|
25
|
|
|
private ?string $country; |
26
|
|
|
|
27
|
|
|
/** @param self $comparator */ |
28
|
|
|
public function equals($comparator): bool |
29
|
|
|
{ |
30
|
|
|
Assertion::isInstanceOf($comparator, self::class); |
31
|
|
|
return $this->toNative() === $comparator->toNative(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** @param array $value */ |
35
|
|
|
public static function fromNative($value): self |
36
|
|
|
{ |
37
|
|
|
Assertion::isArray($value, 'Trying to create address from unsupported value type.'); |
38
|
|
|
return new self( |
39
|
|
|
$value['name'] ?? null, |
40
|
|
|
$value['address1'] ?? null, |
41
|
|
|
$value['address2'] ?? null, |
42
|
|
|
$value['city'] ?? null, |
43
|
|
|
$value['postcode'] ?? null, |
44
|
|
|
$value['country'] ?? null |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function toNative(): array |
49
|
|
|
{ |
50
|
|
|
return array_filter([ |
51
|
|
|
'name' => $this->name, |
52
|
|
|
'address1' => $this->address1, |
53
|
|
|
'address2' => $this->address2, |
54
|
|
|
'city' => $this->city, |
55
|
|
|
'postcode' => $this->postcode, |
56
|
|
|
'country' => $this->country |
57
|
|
|
]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function __toString(): string |
61
|
|
|
{ |
62
|
|
|
return implode(PHP_EOL, $this->toNative()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function __construct( |
66
|
|
|
string $name = null, |
67
|
|
|
string $address1 = null, |
68
|
|
|
string $address2 = null, |
69
|
|
|
string $city = null, |
70
|
|
|
string $postcode = null, |
71
|
|
|
string $country = null |
72
|
|
|
) { |
73
|
|
|
$this->name = $name; |
74
|
|
|
$this->address1 = $address1; |
75
|
|
|
$this->address2 = $address2; |
76
|
|
|
$this->city = $city; |
77
|
|
|
$this->postcode = $postcode; |
78
|
|
|
$this->country = $country; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|