1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2013-2014 eBay Enterprise, Inc. |
4
|
|
|
* |
5
|
|
|
* NOTICE OF LICENSE |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
8
|
|
|
* that is bundled with this package in the file LICENSE.md. |
9
|
|
|
* It is also available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* @copyright Copyright (c) 2013-2014 eBay Enterprise, Inc. (http://www.ebayenterprise.com/) |
13
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace eBayEnterprise\RetailOrderManagement\Payload\Payment; |
17
|
|
|
|
18
|
|
|
trait TBillingAddress |
19
|
|
|
{ |
20
|
|
|
/** @var array */ |
21
|
|
|
protected $billingLines; |
22
|
|
|
/** @var string */ |
23
|
|
|
protected $billingCity; |
24
|
|
|
/** @var string */ |
25
|
|
|
protected $billingMainDivision; |
26
|
|
|
/** @var string */ |
27
|
|
|
protected $billingCountryCode; |
28
|
|
|
/** @var string */ |
29
|
|
|
protected $billingPostalCode; |
30
|
|
|
|
31
|
|
|
public function getBillingLines() |
32
|
|
|
{ |
33
|
|
|
return is_array($this->billingLines) ? implode("\n", $this->billingLines) : null; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function setBillingLines($lines) |
37
|
|
|
{ |
38
|
|
|
$this->billingLines = $this->cleanAddressLines($lines); |
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Make sure we have max 4 address lines of 70 chars max |
44
|
|
|
* |
45
|
|
|
* If there are more than 4 lines concatenate all extra lines with the 4th line. |
46
|
|
|
* |
47
|
|
|
* Truncate any lines to 70 chars max. |
48
|
|
|
* |
49
|
|
|
* @param string $lines |
50
|
|
|
* @return array or null |
51
|
|
|
*/ |
52
|
|
|
abstract protected function cleanAddressLines($lines); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Aggregate the billing address lines into the BillingAddress node |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
protected function serializeBillingAddress() |
60
|
|
|
{ |
61
|
|
|
$lines = []; |
62
|
|
|
$billingLines = is_array($this->billingLines) ? $this->billingLines : []; |
63
|
|
|
$idx = 0; |
64
|
|
|
foreach ($billingLines as $line) { |
65
|
|
|
$idx++; |
66
|
|
|
$lines[] = sprintf( |
67
|
|
|
'<Line%d>%s</Line%1$d>', |
68
|
|
|
$idx, |
69
|
|
|
$this->xmlEncode($line) |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return sprintf( |
74
|
|
|
'<BillingAddress>%s<City>%s</City>%s<CountryCode>%s</CountryCode>%s</BillingAddress>', |
75
|
|
|
implode('', $lines), |
76
|
|
|
$this->xmlEncode($this->getBillingCity()), |
77
|
|
|
$this->serializeOptionalXmlEncodedValue('MainDivision', $this->getBillingMainDivision()), |
78
|
|
|
$this->xmlEncode($this->getBillingCountryCode()), |
79
|
|
|
$this->serializeOptionalXmlEncodedValue('PostalCode', $this->getBillingPostalCode()) |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getBillingCity() |
84
|
|
|
{ |
85
|
|
|
return $this->billingCity; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function setBillingCity($city) |
89
|
|
|
{ |
90
|
|
|
$this->billingCity = $this->cleanString($city, 35); |
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getBillingMainDivision() |
95
|
|
|
{ |
96
|
|
|
return $this->billingMainDivision; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function setBillingMainDivision($div) |
100
|
|
|
{ |
101
|
|
|
$this->billingMainDivision = $this->cleanString($div, 35); |
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function getBillingCountryCode() |
106
|
|
|
{ |
107
|
|
|
return $this->billingCountryCode; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function setBillingCountryCode($code) |
111
|
|
|
{ |
112
|
|
|
$cleaned = $this->cleanString($code, 40); |
113
|
|
|
$this->billingCountryCode = strlen($cleaned) >= 2 ? $cleaned : null; |
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function getBillingPostalCode() |
118
|
|
|
{ |
119
|
|
|
return $this->billingPostalCode; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function setBillingPostalCode($code) |
123
|
|
|
{ |
124
|
|
|
$this->billingPostalCode = $this->cleanString($code, 15); |
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Trim any white space and return the resulting string truncating to $maxLength. |
130
|
|
|
* |
131
|
|
|
* Return null if the result is an empty string or not a string |
132
|
|
|
* |
133
|
|
|
* @param string $string |
134
|
|
|
* @param int $maxLength |
135
|
|
|
* @return string or null |
136
|
|
|
*/ |
137
|
|
|
abstract protected function cleanString($string, $maxLength); |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Serialize an optional element containing a string. The value will be |
141
|
|
|
* xml-encoded if is not null. |
142
|
|
|
* |
143
|
|
|
* @param string |
144
|
|
|
* @param string |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
abstract protected function serializeOptionalXmlEncodedValue($name, $value); |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* encode the passed in string to be safe for xml if it is not null, |
151
|
|
|
* otherwise simply return the null parameter. |
152
|
|
|
* |
153
|
|
|
* @param string|null |
154
|
|
|
* @return string|null |
155
|
|
|
*/ |
156
|
|
|
abstract protected function xmlEncode($value = null); |
157
|
|
|
} |
158
|
|
|
|