Passed
Push — develop ( 5cc981...e09742 )
by Remco
04:10
created

Address::get_postal_code()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\Adyen
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 10
	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 10
		if ( 2 !== strlen( $country ) ) {
86 1
			throw new InvalidArgumentException(
87 1
				sprintf(
88 1
					'Given country `%s` not ISO 3166-1 alpha-2 value.',
89 1
					$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 9
		$fields = array_filter( array( $house_number_or_name, $city, $postal_code, $state_or_province ) );
102
103 9
		if ( empty( $street ) && ! empty( $fields ) ) {
104 1
			throw new InvalidArgumentException(
105 1
				'The name of the street is required if either `houseNumberOrName`, `city`, `postalCode`, or `stateOrProvince` are provided.'
106
			);
107
		}
108
109
		/*
110
		 * The postal code.
111
		 *
112
		 * 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.
113
		 *
114
		 * Required if either `houseNumberOrName`, `street`, `city`, or `stateOrProvince` are provided.
115
		 */
116 8
		$fields = array_filter( array( $house_number_or_name, $street, $city, $state_or_province ) );
117
118 8
		if ( empty( $postal_code ) && ! empty( $fields ) ) {
119 1
			throw new InvalidArgumentException(
120 1
				'The postal code is required if either `houseNumberOrName`, `street`, `city`, or `stateOrProvince` are provided.'
121
			);
122
		}
123
124 7
		if ( ! empty( $postal_code ) ) {
125 6
			$max = ( 'US' === $country ) ? 5 : 10;
126
127 6
			if ( strlen( $postal_code ) > $max ) {
128 1
				throw new InvalidArgumentException(
129 1
					sprintf(
130 1
						'Given postal code `%s` is longer then `%d` digits.',
131 1
						$postal_code,
132 1
						$max
133
					)
134
				);
135
			}
136
		}
137
138
		/*
139
		 * The name of the city.
140
		 *
141
		 * Required if either `houseNumberOrName`, `street`, `postalCode`, or `stateOrProvince` are provided.
142
		 */
143 6
		$fields = array_filter( array( $house_number_or_name, $street, $postal_code, $state_or_province ) );
144
145 6
		if ( empty( $city ) && ! empty( $fields ) ) {
146 1
			throw new InvalidArgumentException(
147 1
				'The name of the city is required if either `houseNumberOrName`, `street`, `postalCode`, or `stateOrProvince` are provided.'
148
			);
149
		}
150
151
		/*
152
		 * 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.
153
		 *
154
		 * Required for an address in the USA or Canada if either `houseNumberOrName`, `street`, `city`, or `postalCode` are provided.
155
		 */
156 5
		$fields = array_filter( array( $house_number_or_name, $street, $city, $postal_code ) );
157
158 5
		if ( empty( $state_or_province ) && in_array( $country, array( 'CA', 'US' ), true ) && ! empty( $fields ) ) {
159 1
			throw new InvalidArgumentException(
160 1
				'State or province is required for an address in the USA or Canada if either `houseNumberOrName`, `street`, `city`, or `postalCode` are provided.'
161
			);
162
		}
163
164 4
		if ( ! empty( $state_or_province ) ) {
165 2
			$max = in_array( $country, array( 'CA', 'US' ), true ) ? 2 : 3;
166
167 2
			if ( strlen( $state_or_province ) > $max ) {
168 1
				throw new InvalidArgumentException(
169 1
					sprintf(
170 1
						'Given state or province `%s` is longer then `%d` digits.',
171 1
						$state_or_province,
172 1
						$max
173
					)
174
				);
175
			}
176
		}
177
178
		// Ok.
179 3
		$this->country              = $country;
180 3
		$this->street               = $street;
181 3
		$this->house_number_or_name = $house_number_or_name;
182 3
		$this->postal_code          = $postal_code;
183 3
		$this->city                 = $city;
184 3
		$this->state_or_province    = $state_or_province;
185 3
	}
186
187
	/**
188
	 * Get city.
189
	 *
190
	 * @return string|null
191
	 */
192 3
	public function get_city() {
193 3
		return $this->city;
194
	}
195
196
	/**
197
	 * Get country.
198
	 *
199
	 * @return string
200
	 */
201 3
	public function get_country() {
202 3
		return $this->country;
203
	}
204
205
	/**
206
	 * Get house number or name.
207
	 *
208
	 * @return string|null
209
	 */
210 3
	public function get_house_number_or_name() {
211 3
		return $this->house_number_or_name;
212
	}
213
214
	/**
215
	 * Get postal code.
216
	 *
217
	 * @return string|null
218
	 */
219 3
	public function get_postal_code() {
220 3
		return $this->postal_code;
221
	}
222
223
	/**
224
	 * Get state or province.
225
	 *
226
	 * @return string|null
227
	 */
228 3
	public function get_state_or_province() {
229 3
		return $this->state_or_province;
230
	}
231
232
	/**
233
	 * Get street.
234
	 *
235
	 * @return string|null
236
	 */
237 3
	public function get_street() {
238 3
		return $this->street;
239
	}
240
241
	/**
242
	 * Get JSON.
243
	 *
244
	 * @return object
245
	 */
246
	public function get_json() {
247
		// Properties.
248
		$properties = array(
249
			'country' => $this->country,
250
		);
251
252
		// Map.
253
		$map = array(
254
			'city'                 => 'city',
255
			'house_number_or_name' => 'houseNumberOrName',
256
			'postal_code'          => 'postalCode',
257
			'state_or_province'    => 'stateOrProvince',
258
			'street'               => 'street',
259
		);
260
261
		foreach ( $map as $property => $adyen_property ) {
262
			if ( null !== $this->{$property} ) {
263
				$properties[ $adyen_property ] = $this->{$property};
264
			}
265
		}
266
267
		// Return object.
268
		$object = (object) $properties;
269
270
		return $object;
271
	}
272
}
273