1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Address |
4
|
|
|
* |
5
|
|
|
* @author Pronamic <[email protected]> |
6
|
|
|
* @copyright 2005-2019 Pronamic |
7
|
|
|
* @license GPL-3.0-or-later |
8
|
|
|
* @package Pronamic\WordPress\Pay\Gateways\OmniKassa2 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Pronamic\WordPress\Pay\Gateways\Adyen; |
12
|
|
|
|
13
|
|
|
use InvalidArgumentException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Address |
17
|
|
|
* |
18
|
|
|
* @link https://docs.adyen.com/developers/api-reference/common-api/address |
19
|
|
|
* |
20
|
|
|
* @author Remco Tolsma |
21
|
|
|
* @version 1.0.0 |
22
|
|
|
* @since 1.0.0 |
23
|
|
|
*/ |
24
|
|
|
class Address { |
25
|
|
|
/** |
26
|
|
|
* City. |
27
|
|
|
* |
28
|
|
|
* @var string|null |
29
|
|
|
*/ |
30
|
|
|
private $city; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Country. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $country; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* House number or name. |
41
|
|
|
* |
42
|
|
|
* @var string|null |
43
|
|
|
*/ |
44
|
|
|
private $house_number_or_name; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Postal code. |
48
|
|
|
* |
49
|
|
|
* @var string|null |
50
|
|
|
*/ |
51
|
|
|
private $postal_code; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* State or province. |
55
|
|
|
* |
56
|
|
|
* @var string|null |
57
|
|
|
*/ |
58
|
|
|
private $state_or_province; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Street. |
62
|
|
|
* |
63
|
|
|
* @var string|null |
64
|
|
|
*/ |
65
|
|
|
private $street; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Construct address. |
69
|
|
|
* |
70
|
|
|
* @param string $country Country. |
71
|
|
|
* @param string $street Street. |
72
|
|
|
* @param string $house_number_or_name House number or name. |
73
|
|
|
* @param string $postal_code Postal code. |
74
|
|
|
* @param string $city City. |
75
|
|
|
* @param string $state_or_province State or province. |
76
|
|
|
* |
77
|
|
|
* @throws InvalidArgumentException Throws invalid argument exception when Adyen address requirements are not met. |
78
|
|
|
*/ |
79
|
2 |
|
public function __construct( $country, $street = null, $house_number_or_name = null, $postal_code = null, $city = null, $state_or_province = null ) { |
80
|
|
|
/* |
81
|
|
|
* The two-character country code of the address. |
82
|
|
|
* |
83
|
|
|
* The permitted country codes are defined in ISO-3166-1 alpha-2 (e.g. 'NL'). |
84
|
|
|
*/ |
85
|
2 |
|
if ( 2 !== strlen( $country ) ) { |
86
|
|
|
throw new InvalidArgumentException( |
87
|
|
|
sprintf( |
88
|
|
|
'Given country `%s` not ISO 3166-1 alpha-2 value.', |
89
|
|
|
$country |
90
|
|
|
) |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/* |
95
|
|
|
* The name of the street. |
96
|
|
|
* |
97
|
|
|
* > The house number should not be included in this field; it should be separately provided via houseNumberOrName. |
98
|
|
|
* |
99
|
|
|
* Required if either `houseNumberOrName`, `city`, `postalCode`, or `stateOrProvince` are provided. |
100
|
|
|
*/ |
101
|
2 |
|
if ( empty( $street ) && ! empty( array_filter( array( $house_number_or_name, $city, $postal_code, $state_or_province ) ) ) ) { |
102
|
|
|
throw new InvalidArgumentException( |
103
|
|
|
'The name of the street is required if either `houseNumberOrName`, `city`, `postalCode`, or `stateOrProvince` are provided.' |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/* |
108
|
|
|
* The postal code. |
109
|
|
|
* |
110
|
|
|
* A maximum of five (5) digits for an address in the USA, or a maximum of ten (10) characters for an address in all other countries. |
111
|
|
|
* |
112
|
|
|
* Required if either `houseNumberOrName`, `street`, `city`, or `stateOrProvince` are provided. |
113
|
|
|
*/ |
114
|
2 |
|
if ( empty( $postal_code ) && ! empty( array_filter( array( $house_number_or_name, $street, $city, $state_or_province ) ) ) ) { |
115
|
|
|
throw new InvalidArgumentException( |
116
|
|
|
'The postal code is required if either `houseNumberOrName`, `street`, `city`, or `stateOrProvince` are provided.' |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
2 |
|
if ( ! empty( $postal_code ) ) { |
121
|
1 |
|
$max = ( 'US' === $country ) ? 5 : 10; |
122
|
|
|
|
123
|
1 |
|
if ( strlen( $postal_code ) > $max ) { |
124
|
|
|
throw new InvalidArgumentException( |
125
|
|
|
sprintf( |
126
|
|
|
'Given postal code `%s` is longer then `%d` digits.', |
127
|
|
|
$postal_code, |
128
|
|
|
$max |
129
|
|
|
) |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/* |
135
|
|
|
* The name of the city. |
136
|
|
|
* |
137
|
|
|
* Required if either `houseNumberOrName`, `street`, `postalCode`, or `stateOrProvince` are provided. |
138
|
|
|
*/ |
139
|
2 |
|
if ( empty( $city ) && ! empty( array_filter( array( $house_number_or_name, $street, $postal_code, $state_or_province ) ) ) ) { |
140
|
|
|
throw new InvalidArgumentException( |
141
|
|
|
'The name of the city is required if either `houseNumberOrName`, `street`, `postalCode`, or `stateOrProvince` are provided.' |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/* |
146
|
|
|
* Two (2) characters for an address in the USA or Canada, or a maximum of three (3) characters for an address in all other countries. |
147
|
|
|
* |
148
|
|
|
* Required for an address in the USA or Canada if either `houseNumberOrName`, `street`, `city`, or `postalCode` are provided. |
149
|
|
|
*/ |
150
|
2 |
|
if ( empty( $state_or_province ) && in_array( $country, array( 'CA', 'US' ), true ) && ! empty( array_filter( array( $house_number_or_name, $street, $city, $postal_code ) ) ) ) { |
151
|
|
|
throw new InvalidArgumentException( |
152
|
|
|
'State or province is required for an address in the USA or Canada if either `houseNumberOrName`, `street`, `city`, or `postalCode` are provided.' |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
2 |
|
if ( ! empty( $state_or_province ) ) { |
157
|
|
|
$max = in_array( $country, array( 'CA', 'US' ), true ) ? 2 : 3; |
158
|
|
|
|
159
|
|
|
if ( strlen( $state_or_province ) > $max ) { |
160
|
|
|
throw new InvalidArgumentException( |
161
|
|
|
sprintf( |
162
|
|
|
'Given state or province `%s` is longer then `%d` digits.', |
163
|
|
|
$state_or_province, |
164
|
|
|
$max |
165
|
|
|
) |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// Ok. |
171
|
2 |
|
$this->country = $country; |
172
|
2 |
|
$this->street = $street; |
173
|
2 |
|
$this->house_number_or_name = $house_number_or_name; |
174
|
2 |
|
$this->postal_code = $postal_code; |
175
|
2 |
|
$this->city = $city; |
176
|
2 |
|
$this->state_or_province = $state_or_province; |
177
|
|
|
|
178
|
2 |
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Get city. |
182
|
|
|
* |
183
|
|
|
* @return string|null |
184
|
|
|
*/ |
185
|
2 |
|
public function get_city() { |
186
|
2 |
|
return $this->city; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Get country. |
191
|
|
|
* |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
2 |
|
public function get_country() { |
195
|
2 |
|
return $this->country; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Get house number or name. |
200
|
|
|
* |
201
|
|
|
* @return string|null |
202
|
|
|
*/ |
203
|
2 |
|
public function get_house_number_or_name() { |
204
|
2 |
|
return $this->house_number_or_name; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Get postal code. |
209
|
|
|
* |
210
|
|
|
* @return string|null |
211
|
|
|
*/ |
212
|
2 |
|
public function get_postal_code() { |
213
|
2 |
|
return $this->postal_code; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Get state or province. |
218
|
|
|
* |
219
|
|
|
* @return string|null |
220
|
|
|
*/ |
221
|
2 |
|
public function get_state_or_province() { |
222
|
2 |
|
return $this->state_or_province; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Get street. |
227
|
|
|
* |
228
|
|
|
* @return string|null |
229
|
|
|
*/ |
230
|
2 |
|
public function get_street() { |
231
|
2 |
|
return $this->street; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Get JSON. |
236
|
|
|
* |
237
|
|
|
* @return object |
238
|
|
|
*/ |
239
|
|
|
public function get_json() { |
240
|
|
|
$object = (object) array(); |
241
|
|
|
|
242
|
|
|
// City. |
243
|
|
|
if ( null !== $this->city ) { |
244
|
|
|
$object->city = $this->city; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
// Country. |
248
|
|
|
$object->country = $this->country; |
249
|
|
|
|
250
|
|
|
// House number or name. |
251
|
|
|
if ( null !== $this->house_number_or_name ) { |
252
|
|
|
$object->houseNumberOrName = $this->house_number_or_name; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
// Postal code. |
256
|
|
|
if ( null !== $this->postal_code ) { |
257
|
|
|
$object->postalCode = $this->postal_code; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
// State or province. |
261
|
|
|
if ( null !== $this->state_or_province ) { |
262
|
|
|
$object->stateOrProvince = $this->state_or_province; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
// Street. |
266
|
|
|
if ( null !== $this->street ) { |
267
|
|
|
$object->street = $this->street; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
// Return object. |
271
|
|
|
return $object; |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|