1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\Address; |
4
|
|
|
|
5
|
|
|
use Broadway\Serializer\SerializableInterface; |
6
|
|
|
use CultuurNet\UDB3\JsonLdSerializableInterface; |
7
|
|
|
use CultuurNet\UDB3\Model\ValueObject\Geography\Address as Udb3ModelAddress; |
8
|
|
|
use ValueObjects\Geography\Country; |
9
|
|
|
use ValueObjects\Geography\CountryCode; |
10
|
|
|
|
11
|
|
|
final class Address implements SerializableInterface, JsonLdSerializableInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $countryCode; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Locality |
20
|
|
|
*/ |
21
|
|
|
protected $locality; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var PostalCode |
25
|
|
|
*/ |
26
|
|
|
protected $postalCode; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Street |
30
|
|
|
*/ |
31
|
|
|
protected $streetAddress; |
32
|
|
|
|
33
|
|
|
public function __construct( |
34
|
|
|
Street $streetAddress, |
35
|
|
|
PostalCode $postalCode, |
36
|
|
|
Locality $locality, |
37
|
|
|
Country $country |
38
|
|
|
) { |
39
|
|
|
$this->streetAddress = $streetAddress; |
40
|
|
|
$this->postalCode = $postalCode; |
41
|
|
|
$this->locality = $locality; |
42
|
|
|
$this->countryCode = $country->getCode()->toNative(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getCountry(): Country |
46
|
|
|
{ |
47
|
|
|
return Country::fromNative($this->countryCode); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getLocality(): Locality |
51
|
|
|
{ |
52
|
|
|
return $this->locality; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getPostalCode(): PostalCode |
56
|
|
|
{ |
57
|
|
|
return $this->postalCode; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getStreetAddress(): Street |
61
|
|
|
{ |
62
|
|
|
return $this->streetAddress; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
View Code Duplication |
public function serialize(): array |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
return [ |
68
|
|
|
'streetAddress' => $this->streetAddress->toNative(), |
69
|
|
|
'postalCode' => $this->postalCode->toNative(), |
70
|
|
|
'addressLocality' => $this->locality->toNative(), |
71
|
|
|
'addressCountry' => $this->countryCode, |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public static function deserialize(array $data): Address |
76
|
|
|
{ |
77
|
|
|
return new self( |
78
|
|
|
new Street($data['streetAddress']), |
79
|
|
|
new PostalCode($data['postalCode']), |
80
|
|
|
new Locality($data['addressLocality']), |
81
|
|
|
Country::fromNative($data['addressCountry']) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
View Code Duplication |
public function toJsonLd(): array |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
return [ |
88
|
|
|
'addressCountry' => $this->countryCode, |
89
|
|
|
'addressLocality' => $this->locality->toNative(), |
90
|
|
|
'postalCode' => $this->postalCode->toNative(), |
91
|
|
|
'streetAddress' => $this->streetAddress->toNative(), |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function sameAs(Address $otherAddress): bool |
96
|
|
|
{ |
97
|
|
|
return $this->toJsonLd() === $otherAddress->toJsonLd(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public static function fromUdb3ModelAddress(Udb3ModelAddress $address): Address |
101
|
|
|
{ |
102
|
|
|
return new self( |
103
|
|
|
new Street($address->getStreet()->toString()), |
104
|
|
|
new PostalCode($address->getPostalCode()->toString()), |
105
|
|
|
new Locality($address->getLocality()->toString()), |
106
|
|
|
new Country(CountryCode::fromNative($address->getCountryCode()->toString())) |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.