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
|
|
|
* @return self |
59
|
|
|
* @throws \BadMethodCallException |
60
|
|
|
*/ |
61
|
2 |
|
public static function fromNative() |
62
|
|
|
{ |
63
|
2 |
|
$args = \func_get_args(); |
64
|
|
|
|
65
|
2 |
|
if (\count($args) != 8) { |
66
|
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.'); |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
$name = new StringLiteral($args[0]); |
70
|
1 |
|
$street = new Street(new StringLiteral($args[1]), new StringLiteral($args[2])); |
71
|
1 |
|
$district = new StringLiteral($args[3]); |
72
|
1 |
|
$city = new StringLiteral($args[4]); |
73
|
1 |
|
$region = new StringLiteral($args[5]); |
74
|
1 |
|
$postalCode = new StringLiteral($args[6]); |
75
|
1 |
|
$country = Country::fromNative($args[7]); |
76
|
|
|
|
77
|
1 |
|
return new static($name, $street, $district, $city, $region, $postalCode, $country); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns a new Address object |
82
|
|
|
* |
83
|
|
|
* @param StringLiteral $name |
84
|
|
|
* @param Street $street |
85
|
|
|
* @param StringLiteral $district |
86
|
|
|
* @param StringLiteral $city |
87
|
|
|
* @param StringLiteral $region |
88
|
|
|
* @param StringLiteral $postalCode |
89
|
|
|
* @param Country $country |
90
|
|
|
*/ |
91
|
11 |
|
public function __construct(StringLiteral $name, Street $street, StringLiteral $district, StringLiteral $city, StringLiteral $region, StringLiteral $postalCode, Country $country) |
92
|
|
|
{ |
93
|
11 |
|
$this->name = $name; |
94
|
11 |
|
$this->street = $street; |
95
|
11 |
|
$this->district = $district; |
96
|
11 |
|
$this->city = $city; |
97
|
11 |
|
$this->region = $region; |
98
|
11 |
|
$this->postalCode = $postalCode; |
99
|
11 |
|
$this->country = $country; |
100
|
11 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Tells whether two Address are equal |
104
|
|
|
* |
105
|
|
|
* @param ValueObjectInterface $address |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
2 |
|
public function sameValueAs(ValueObjectInterface $address) |
109
|
|
|
{ |
110
|
2 |
|
if (false === Util::classEquals($this, $address)) { |
111
|
1 |
|
return false; |
112
|
|
|
} |
113
|
|
|
|
114
|
2 |
|
return $this->getName()->sameValueAs($address->getName()) && |
|
|
|
|
115
|
2 |
|
$this->getStreet()->sameValueAs($address->getStreet()) && |
|
|
|
|
116
|
2 |
|
$this->getDistrict()->sameValueAs($address->getDistrict()) && |
|
|
|
|
117
|
2 |
|
$this->getCity()->sameValueAs($address->getCity()) && |
|
|
|
|
118
|
2 |
|
$this->getRegion()->sameValueAs($address->getRegion()) && |
|
|
|
|
119
|
2 |
|
$this->getPostalCode()->sameValueAs($address->getPostalCode()) && |
|
|
|
|
120
|
2 |
|
$this->getCountry()->sameValueAs($address->getCountry()) |
|
|
|
|
121
|
2 |
|
; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Returns addressee name |
126
|
|
|
* |
127
|
|
|
* @return StringLiteral |
128
|
|
|
*/ |
129
|
4 |
|
public function getName() |
130
|
|
|
{ |
131
|
4 |
|
return clone $this->name; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns street |
136
|
|
|
* |
137
|
|
|
* @return Street |
138
|
|
|
*/ |
139
|
4 |
|
public function getStreet() |
140
|
|
|
{ |
141
|
4 |
|
return clone $this->street; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Returns district |
146
|
|
|
* |
147
|
|
|
* @return StringLiteral |
148
|
|
|
*/ |
149
|
3 |
|
public function getDistrict() |
150
|
|
|
{ |
151
|
3 |
|
return clone $this->district; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Returns city |
156
|
|
|
* |
157
|
|
|
* @return StringLiteral |
158
|
|
|
*/ |
159
|
4 |
|
public function getCity() |
160
|
|
|
{ |
161
|
4 |
|
return clone $this->city; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Returns region |
166
|
|
|
* |
167
|
|
|
* @return StringLiteral |
168
|
|
|
*/ |
169
|
4 |
|
public function getRegion() |
170
|
|
|
{ |
171
|
4 |
|
return clone $this->region; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Returns postal code |
176
|
|
|
* |
177
|
|
|
* @return StringLiteral |
178
|
|
|
*/ |
179
|
4 |
|
public function getPostalCode() |
180
|
|
|
{ |
181
|
4 |
|
return clone $this->postalCode; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Returns country |
186
|
|
|
* |
187
|
|
|
* @return Country |
188
|
|
|
*/ |
189
|
4 |
|
public function getCountry() |
190
|
|
|
{ |
191
|
4 |
|
return clone $this->country; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Returns a string representation of the Address in US standard format. |
196
|
|
|
* |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
1 |
|
public function __toString() |
200
|
|
|
{ |
201
|
|
|
$format = <<<ADDR |
202
|
|
|
%s |
203
|
|
|
%s |
204
|
|
|
%s %s %s |
205
|
|
|
%s |
206
|
1 |
|
ADDR; |
207
|
|
|
|
208
|
1 |
|
$addressString = \sprintf($format, $this->getName(), $this->getStreet(), $this->getCity(), $this->getRegion(), $this->getPostalCode(), $this->getCountry()); |
209
|
|
|
|
210
|
1 |
|
return $addressString; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
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.