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
|
|
|
/** |
12
|
|
|
* Value object for address information. |
13
|
|
|
*/ |
14
|
|
|
class Address implements SerializableInterface, JsonLdSerializableInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $countryCode; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Locality |
23
|
|
|
*/ |
24
|
|
|
protected $locality; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var PostalCode |
28
|
|
|
*/ |
29
|
|
|
protected $postalCode; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Street |
33
|
|
|
*/ |
34
|
|
|
protected $streetAddress; |
35
|
|
|
|
36
|
|
|
public function __construct( |
37
|
|
|
Street $streetAddress, |
38
|
|
|
PostalCode $postalCode, |
39
|
|
|
Locality $locality, |
40
|
|
|
Country $country |
41
|
|
|
) { |
42
|
|
|
$this->streetAddress = $streetAddress; |
43
|
|
|
$this->postalCode = $postalCode; |
44
|
|
|
$this->locality = $locality; |
45
|
|
|
$this->countryCode = $country->getCode()->toNative(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return Country |
50
|
|
|
*/ |
51
|
|
|
public function getCountry() |
52
|
|
|
{ |
53
|
|
|
return Country::fromNative($this->countryCode); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return Locality |
58
|
|
|
*/ |
59
|
|
|
public function getLocality() |
60
|
|
|
{ |
61
|
|
|
return $this->locality; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return PostalCode |
66
|
|
|
*/ |
67
|
|
|
public function getPostalCode() |
68
|
|
|
{ |
69
|
|
|
return $this->postalCode; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return Street |
74
|
|
|
*/ |
75
|
|
|
public function getStreetAddress() |
76
|
|
|
{ |
77
|
|
|
return $this->streetAddress; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
public function serialize() |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
return [ |
86
|
|
|
'streetAddress' => $this->streetAddress->toNative(), |
87
|
|
|
'postalCode' => $this->postalCode->toNative(), |
88
|
|
|
'addressLocality' => $this->locality->toNative(), |
89
|
|
|
'addressCountry' => $this->countryCode, |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
public static function deserialize(array $data) |
97
|
|
|
{ |
98
|
|
|
return new static( |
99
|
|
|
new Street($data['streetAddress']), |
100
|
|
|
new PostalCode($data['postalCode']), |
101
|
|
|
new Locality($data['addressLocality']), |
102
|
|
|
Country::fromNative($data['addressCountry']) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
View Code Duplication |
public function toJsonLd() |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
return [ |
112
|
|
|
'addressCountry' => $this->countryCode, |
113
|
|
|
'addressLocality' => $this->locality->toNative(), |
114
|
|
|
'postalCode' => $this->postalCode->toNative(), |
115
|
|
|
'streetAddress' => $this->streetAddress->toNative(), |
116
|
|
|
]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param Address $otherAddress |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
|
|
public function sameAs(Address $otherAddress) |
124
|
|
|
{ |
125
|
|
|
return $this->toJsonLd() === $otherAddress->toJsonLd(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param Udb3ModelAddress $address |
130
|
|
|
* @return self |
131
|
|
|
*/ |
132
|
|
|
public static function fromUdb3ModelAddress(Udb3ModelAddress $address) |
133
|
|
|
{ |
134
|
|
|
return new self( |
135
|
|
|
new Street($address->getStreet()->toString()), |
136
|
|
|
new PostalCode($address->getPostalCode()->toString()), |
137
|
|
|
new Locality($address->getLocality()->toString()), |
138
|
|
|
new Country(CountryCode::fromNative($address->getCountryCode()->toString())) |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
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.