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