1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ValueObjects\Geography; |
4
|
|
|
|
5
|
|
|
use ValueObjects\Util\Util; |
6
|
|
|
use ValueObjects\ValueObjectInterface; |
7
|
|
|
use ValueObjects\StringLiteral\StringLiteral; |
8
|
|
|
|
9
|
|
|
class Address implements ValueObjectInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Name of the addressee (natural person or company) |
13
|
|
|
* |
14
|
|
|
* @var String |
15
|
|
|
*/ |
16
|
|
|
protected $name; |
17
|
|
|
|
18
|
|
|
/** @var Street */ |
19
|
|
|
protected $street; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* District/City area |
23
|
|
|
* |
24
|
|
|
* @var String |
25
|
|
|
*/ |
26
|
|
|
protected $district; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* City/Town/Village |
30
|
|
|
* |
31
|
|
|
* @var String |
32
|
|
|
*/ |
33
|
|
|
protected $city; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Region/County/State |
37
|
|
|
* |
38
|
|
|
* @var String |
39
|
|
|
*/ |
40
|
|
|
protected $region; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Postal code/P.O. Box/ZIP code |
44
|
|
|
* |
45
|
|
|
* @var String |
46
|
|
|
*/ |
47
|
|
|
protected $postalCode; |
48
|
|
|
|
49
|
|
|
/** @var Country */ |
50
|
|
|
protected $country; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns a new Address from native PHP arguments |
54
|
|
|
* |
55
|
|
|
* @param string $name |
|
|
|
|
56
|
|
|
* @param string $street_name |
|
|
|
|
57
|
|
|
* @param string $street_number |
|
|
|
|
58
|
|
|
* @param string $district |
|
|
|
|
59
|
|
|
* @param string $city |
|
|
|
|
60
|
|
|
* @param string $region |
|
|
|
|
61
|
|
|
* @param string $postal_code |
|
|
|
|
62
|
|
|
* @param string $country_code |
|
|
|
|
63
|
|
|
* |
64
|
|
|
* @return self |
65
|
|
|
* @throws \BadMethodCallException |
66
|
|
|
*/ |
67
|
2 |
|
public static function fromNative() |
68
|
|
|
{ |
69
|
2 |
|
$args = \func_get_args(); |
70
|
|
|
|
71
|
2 |
|
if (\count($args) != 8) { |
72
|
1 |
|
throw new \BadMethodCallException('You must provide exactly 8 arguments: 1) addressee name, 2) street name, 3) street number, 4) district, 5) city, 6) region, 7) postal code, 8) country code.'); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
$name = new StringLiteral($args[0]); |
76
|
1 |
|
$street = new Street(new StringLiteral($args[1]), new StringLiteral($args[2])); |
77
|
1 |
|
$district = new StringLiteral($args[3]); |
78
|
1 |
|
$city = new StringLiteral($args[4]); |
79
|
1 |
|
$region = new StringLiteral($args[5]); |
80
|
1 |
|
$postalCode = new StringLiteral($args[6]); |
81
|
1 |
|
$country = Country::fromNative($args[7]); |
82
|
|
|
|
83
|
1 |
|
return new self($name, $street, $district, $city, $region, $postalCode, $country); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns a new Address object |
88
|
|
|
* |
89
|
|
|
* @param String $name |
90
|
|
|
* @param Street $street |
91
|
|
|
* @param String $district |
92
|
|
|
* @param String $city |
93
|
|
|
* @param String $region |
94
|
|
|
* @param String $postalCode |
95
|
|
|
* @param Country $country |
96
|
|
|
*/ |
97
|
11 |
|
public function __construct(StringLiteral $name, Street $street, StringLiteral $district, StringLiteral $city, StringLiteral $region, StringLiteral $postalCode, Country $country) |
98
|
|
|
{ |
99
|
11 |
|
$this->name = $name; |
|
|
|
|
100
|
11 |
|
$this->street = $street; |
101
|
11 |
|
$this->district = $district; |
|
|
|
|
102
|
11 |
|
$this->city = $city; |
|
|
|
|
103
|
11 |
|
$this->region = $region; |
|
|
|
|
104
|
11 |
|
$this->postalCode = $postalCode; |
|
|
|
|
105
|
11 |
|
$this->country = $country; |
106
|
11 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Tells whether two Address are equal |
110
|
|
|
* |
111
|
|
|
* @param ValueObjectInterface $address |
112
|
|
|
* |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
2 |
|
public function sameValueAs(ValueObjectInterface $address) |
116
|
|
|
{ |
117
|
2 |
|
if (FALSE === Util::classEquals($this, $address)) { |
118
|
1 |
|
return FALSE; |
119
|
|
|
} |
120
|
|
|
|
121
|
2 |
|
return $this->getName()->sameValueAs($address->getName()) && |
|
|
|
|
122
|
2 |
|
$this->getStreet()->sameValueAs($address->getStreet()) && |
|
|
|
|
123
|
2 |
|
$this->getDistrict()->sameValueAs($address->getDistrict()) && |
|
|
|
|
124
|
2 |
|
$this->getCity()->sameValueAs($address->getCity()) && |
|
|
|
|
125
|
2 |
|
$this->getRegion()->sameValueAs($address->getRegion()) && |
|
|
|
|
126
|
2 |
|
$this->getPostalCode()->sameValueAs($address->getPostalCode()) && |
|
|
|
|
127
|
2 |
|
$this->getCountry()->sameValueAs($address->getCountry()); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Returns addressee name |
132
|
|
|
* |
133
|
|
|
* @return String |
134
|
|
|
*/ |
135
|
4 |
|
public function getName() |
136
|
|
|
{ |
137
|
4 |
|
return clone $this->name; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Returns street |
142
|
|
|
* |
143
|
|
|
* @return Street |
144
|
|
|
*/ |
145
|
4 |
|
public function getStreet() |
146
|
|
|
{ |
147
|
4 |
|
return clone $this->street; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Returns district |
152
|
|
|
* |
153
|
|
|
* @return String |
154
|
|
|
*/ |
155
|
3 |
|
public function getDistrict() |
156
|
|
|
{ |
157
|
3 |
|
return clone $this->district; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Returns city |
162
|
|
|
* |
163
|
|
|
* @return String |
164
|
|
|
*/ |
165
|
4 |
|
public function getCity() |
166
|
|
|
{ |
167
|
4 |
|
return clone $this->city; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Returns region |
172
|
|
|
* |
173
|
|
|
* @return String |
174
|
|
|
*/ |
175
|
4 |
|
public function getRegion() |
176
|
|
|
{ |
177
|
4 |
|
return clone $this->region; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Returns postal code |
182
|
|
|
* |
183
|
|
|
* @return String |
184
|
|
|
*/ |
185
|
4 |
|
public function getPostalCode() |
186
|
|
|
{ |
187
|
4 |
|
return clone $this->postalCode; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Returns country |
192
|
|
|
* |
193
|
|
|
* @return Country |
194
|
|
|
*/ |
195
|
4 |
|
public function getCountry() |
196
|
|
|
{ |
197
|
4 |
|
return clone $this->country; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Returns a string representation of the Address in US standard format. |
202
|
|
|
* |
203
|
|
|
* @return string |
204
|
|
|
*/ |
205
|
1 |
|
public function __toString() |
206
|
|
|
{ |
207
|
|
|
$format = <<<ADDR |
208
|
|
|
%s |
209
|
|
|
%s |
210
|
|
|
%s %s %s |
211
|
1 |
|
%s |
212
|
|
|
ADDR; |
213
|
|
|
|
214
|
1 |
|
$addressString = \sprintf($format, $this->getName(), $this->getStreet(), $this->getCity(), $this->getRegion(), $this->getPostalCode(), $this->getCountry()); |
215
|
|
|
|
216
|
1 |
|
return $addressString; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
function jsonSerialize() |
|
|
|
|
220
|
|
|
{ |
221
|
|
|
return [ |
222
|
|
|
'parts' => [ |
223
|
|
|
'name' => $this->getName(), |
224
|
|
|
'street' => $this->getStreet(), |
225
|
|
|
'city' => $this->getCity(), |
226
|
|
|
'region' => $this->getRegion(), |
227
|
|
|
'postalCode' => $this->getPostalCode(), |
228
|
|
|
'country' => $this->getCountry(), |
229
|
|
|
], |
230
|
|
|
'formatted' => (string)$this, |
231
|
|
|
]; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
|
235
|
|
|
} |
236
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.