1 <?php
2 /**
3 * Copyright 2016 Klarna AB.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 namespace Klarna\XMLRPC;
18
19 /**
20 * Address is an object of convenience, to parse and create addresses.
21 */
22 class Address
23 {
24 /**
25 * Email address.
26 *
27 * @var string
28 */
29 protected $email;
30
31 /**
32 * Phone number.
33 *
34 * @var string
35 */
36 protected $telno;
37
38 /**
39 * Cellphone number.
40 *
41 * @var string
42 */
43 protected $cellno;
44
45 /**
46 * First name.
47 *
48 * @var string
49 */
50 protected $fname;
51
52 /**
53 * Last name.
54 *
55 * @var string
56 */
57 protected $lname;
58
59 /**
60 * Company name.
61 *
62 * @var string
63 */
64 protected $company;
65
66 /**
67 * Care of, C/O.
68 *
69 * @var string
70 */
71 protected $careof;
72
73 /**
74 * Street address.
75 *
76 * @var string
77 */
78 protected $street;
79
80 /**
81 * Zip code.
82 *
83 * @var string
84 */
85 protected $zip;
86
87 /**
88 * City.
89 *
90 * @var string
91 */
92 protected $city;
93
94 /**
95 * Country constant.
96 *
97 * @var int
98 */
99 protected $country;
100
101 /**
102 * House number.
103 * Only for NL and DE!
104 *
105 * @var string
106 */
107 protected $houseNo;
108
109 /**
110 * House extension.
111 * Only for NL!
112 *
113 * @var string
114 */
115 protected $houseExt;
116
117 /**
118 * When using {@link Klarna::getAddresses()} this might be guessed
119 * depending on type used.
120 *
121 * Signifies if address is for a company or a private person.
122 * If isCompany is null, then it is unknown and will be assumed to
123 * be a private person.
124 *
125 * <b>Note</b>:<br>
126 * This has no effect on transmitted data.
127 *
128 * @var bool|null
129 */
130 public $isCompany = null;
131
132 /**
133 * Class constructor.
134 *
135 * Calls the set methods for all arguments.
136 *
137 * @param string $email Email address.
138 * @param string $telno Phone number.
139 * @param string $cellno Cellphone number.
140 * @param string $fname First name.
141 * @param string $lname Last name.
142 * @param string $careof Care of, C/O.
143 * @param string $street Street address.
144 * @param string $zip Zip code.
145 * @param string $city City.
146 * @param string|int $country Country constant or two letter code.
147 * @param string $houseNo House number, only used in DE and NL.
148 * @param string $houseExt House extension, only used in NL.
149 *
150 * @throws Exceptions\KlarnaException
151 */
152 public function __construct(
153 $email = '',
154 $telno = '',
155 $cellno = '',
156 $fname = '',
157 $lname = '',
158 $careof = '',
159 $street = '',
160 $zip = '',
161 $city = '',
162 $country = null,
163 $houseNo = '',
164 $houseExt = ''
165 ) {
166 //Set all string values to ""
167 $this->company = '';
168 $this->setTelno($telno);
169 $this->setCareof($careof);
170 $this->setCellno($cellno);
171 $this->setCity($city);
172 $this->setEmail($email);
173 $this->setFirstName($fname);
174 $this->setLastName($lname);
175 $this->setStreet($street);
176 $this->setZipCode($zip);
177 $this->setHouseNumber($houseNo);
178 $this->setHouseExt($houseExt);
179
180 if ($country !== null) {
181 $this->setCountry($country);
182 }
183 }
184
185 /**
186 * Returns the email address.
187 *
188 * @return string
189 */
190 public function getEmail()
191 {
192 return $this->email;
193 }
194
195 /**
196 * Sets the email address.
197 *
198 * @param string $email email address
199 */
200 public function setEmail($email)
201 {
202 if (!is_string($email)) {
203 $email = strval($email);
204 }
205
206 $this->email = $email;
207 }
208
209 /**
210 * Returns the phone number.
211 *
212 * @return string
213 */
214 public function getTelno()
215 {
216 return $this->telno;
217 }
218
219 /**
220 * Sets the phone number.
221 *
222 * @param string $telno telno
223 */
224 public function setTelno($telno)
225 {
226 if (!is_string($telno)) {
227 $telno = strval($telno);
228 }
229 $this->telno = $telno;
230 }
231
232 /**
233 * Returns the cellphone number.
234 *
235 * @return string
236 */
237 public function getCellno()
238 {
239 return $this->cellno;
240 }
241
242 /**
243 * Sets the cellphone number.
244 *
245 * @param string $cellno mobile number
246 */
247 public function setCellno($cellno)
248 {
249 if (!is_string($cellno)) {
250 $cellno = strval($cellno);
251 }
252
253 $this->cellno = $cellno;
254 }
255
256 /**
257 * Returns the first name.
258 *
259 * @return string
260 */
261 public function getFirstName()
262 {
263 return $this->fname;
264 }
265
266 /**
267 * Sets the first name.
268 *
269 * @param string $fname firstname
270 */
271 public function setFirstName($fname)
272 {
273 if (!is_string($fname)) {
274 $fname = strval($fname);
275 }
276
277 $this->fname = $fname;
278 }
279
280 /**
281 * Returns the last name.
282 *
283 * @return string
284 */
285 public function getLastName()
286 {
287 return $this->lname;
288 }
289
290 /**
291 * Sets the last name.
292 *
293 * @param string $lname lastname
294 */
295 public function setLastName($lname)
296 {
297 if (!is_string($lname)) {
298 $lname = strval($lname);
299 }
300
301 $this->lname = $lname;
302 }
303
304 /**
305 * Returns the company name.
306 *
307 * @return string
308 */
309 public function getCompanyName()
310 {
311 return $this->company;
312 }
313
314 /**
315 * Sets the company name.
316 * If the purchase results in a company purchase,
317 * reference person will be used from first and last name,
318 * or the value set with {@link Klarna::setReference()}.
319 *
320 * @param string $company company name
321 *
322 * @see Klarna::setReference
323 */
324 public function setCompanyName($company)
325 {
326 if (!is_string($company)) {
327 $company = strval($company);
328 }
329
330 $this->company = $company;
331 }
332
333 /**
334 * Returns the care of, C/O.
335 *
336 * @return string
337 */
338 public function getCareof()
339 {
340 return $this->careof;
341 }
342
343 /**
344 * Sets the care of, C/O.
345 *
346 * @param string $careof care of address
347 */
348 public function setCareof($careof)
349 {
350 if (!is_string($careof)) {
351 $careof = strval($careof);
352 }
353
354 $this->careof = $careof;
355 }
356
357 /**
358 * Returns the street address.
359 *
360 * @return string
361 */
362 public function getStreet()
363 {
364 return $this->street;
365 }
366
367 /**
368 * Sets the street address.
369 *
370 * @param string $street street address
371 */
372 public function setStreet($street)
373 {
374 if (!is_string($street)) {
375 $street = strval($street);
376 }
377
378 $this->street = $street;
379 }
380
381 /**
382 * Returns the zip code.
383 *
384 * @return string
385 */
386 public function getZipCode()
387 {
388 return $this->zip;
389 }
390
391 /**
392 * Sets the zip code.
393 *
394 * @param string $zip zip code
395 */
396 public function setZipCode($zip)
397 {
398 if (!is_string($zip)) {
399 $zip = strval($zip);
400 }
401
402 $zip = str_replace(' ', '', $zip); //remove spaces
403
404 $this->zip = $zip;
405 }
406
407 /**
408 * Returns the city.
409 *
410 * @return string
411 */
412 public function getCity()
413 {
414 return $this->city;
415 }
416
417 /**
418 * Sets the city.
419 *
420 * @param string $city city
421 */
422 public function setCity($city)
423 {
424 if (!is_string($city)) {
425 $city = strval($city);
426 }
427
428 $this->city = $city;
429 }
430
431 /**
432 * Returns the country as a integer constant.
433 *
434 * @return int {@link Country}
435 */
436 public function getCountry()
437 {
438 return $this->country;
439 }
440
441 /**
442 * Returns the country as a two letter representation.
443 *
444 * @throws Exceptions\KlarnaException
445 *
446 * @return string E.g. 'de', 'dk', ...
447 */
448 public function getCountryCode()
449 {
450 return Country::getCode($this->country);
451 }
452
453 /**
454 * Sets the country, use either a two letter representation or the integer
455 * constant.
456 *
457 * @param int $country {@link Country}
458 *
459 * @throws Exceptions\KlarnaException
460 */
461 public function setCountry($country)
462 {
463 if ($country === null) {
464 throw new \InvalidArgumentException('Expected $country to be a string');
465 }
466
467 if (is_numeric($country)) {
468 if (!is_int($country)) {
469 $country = intval($country);
470 }
471 $this->country = $country;
472
473 return;
474 }
475 if (strlen($country) == 2 || strlen($country) == 3) {
476 $this->setCountry(Country::fromCode($country));
477
478 return;
479 }
480 throw new \RuntimeException("Failed to set country! ({$country})");
481 }
482
483 /**
484 * Returns the house number.<br>
485 * Only used in Germany and Netherlands.<br>.
486 *
487 * @return string
488 */
489 public function getHouseNumber()
490 {
491 return $this->houseNo;
492 }
493
494 /**
495 * Sets the house number.<br>
496 * Only used in Germany and Netherlands.<br>.
497 *
498 * @param string $houseNo house number
499 */
500 public function setHouseNumber($houseNo)
501 {
502 if (!is_string($houseNo)) {
503 $houseNo = strval($houseNo);
504 }
505
506 $this->houseNo = $houseNo;
507 }
508
509 /**
510 * Returns the house extension.<br>
511 * Only used in Netherlands.<br>.
512 *
513 * @return string
514 */
515 public function getHouseExt()
516 {
517 return $this->houseExt;
518 }
519
520 /**
521 * Sets the house extension.<br>
522 * Only used in Netherlands.<br>.
523 *
524 * @param string $houseExt house extension
525 */
526 public function setHouseExt($houseExt)
527 {
528 if (!is_string($houseExt)) {
529 $houseExt = strval($houseExt);
530 }
531
532 $this->houseExt = $houseExt;
533 }
534
535 /**
536 * Returns an associative array representing this object.
537 *
538 * @return array
539 */
540 public function toArray()
541 {
542 return array(
543 'email' => $this->getEmail(),
544 'telno' => $this->getTelno(),
545 'cellno' => $this->getCellno(),
546 'fname' => $this->getFirstName(),
547 'lname' => $this->getLastName(),
548 'company' => $this->getCompanyName(),
549 'careof' => $this->getCareof(),
550 'street' => $this->getStreet(),
551 'house_number' => $this->getHouseNumber(),
552 'house_extension' => $this->getHouseExt(),
553 'zip' => $this->getZipCode(),
554 'city' => $this->getCity(),
555 'country' => $this->getCountry(),
556 );
557 }
558 }
559