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\Checkout; |
17
|
|
|
|
18
|
|
View Code Duplication |
trait TPhysicalAddress |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
/** @var array */ |
21
|
|
|
protected $lines; |
22
|
|
|
/** @var string */ |
23
|
|
|
protected $city; |
24
|
|
|
/** @var string */ |
25
|
|
|
protected $mainDivision; |
26
|
|
|
/** @var string */ |
27
|
|
|
protected $countryCode; |
28
|
|
|
/** @var string */ |
29
|
|
|
protected $postalCode; |
30
|
|
|
|
31
|
|
|
public function getLines() |
32
|
|
|
{ |
33
|
|
|
return is_array($this->lines) ? implode("\n", $this->lines) : null; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function setLines($lines) |
37
|
|
|
{ |
38
|
|
|
$this->lines = $this->cleanAddressLines($lines); |
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getCity() |
43
|
|
|
{ |
44
|
|
|
return $this->city; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function setCity($city) |
48
|
|
|
{ |
49
|
|
|
$this->city = $this->cleanString($city, 35); |
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getMainDivision() |
54
|
|
|
{ |
55
|
|
|
return $this->mainDivision; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function setMainDivision($div) |
59
|
|
|
{ |
60
|
|
|
$this->mainDivision = $this->cleanString($div, 35); |
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getCountryCode() |
65
|
|
|
{ |
66
|
|
|
return $this->countryCode; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function setCountryCode($code) |
70
|
|
|
{ |
71
|
|
|
$cleaned = $this->cleanString($code, 40); |
|
|
|
|
72
|
|
|
$this->countryCode = strlen($cleaned) >= 2 ? $cleaned : null; |
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getPostalCode() |
77
|
|
|
{ |
78
|
|
|
return $this->postalCode; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function setPostalCode($code) |
82
|
|
|
{ |
83
|
|
|
$this->postalCode = $this->cleanString($code, 15); |
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Aggregate the address lines into the ShippingAddress node. This is an optional node. |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
protected function serializePhysicalAddress() |
93
|
|
|
{ |
94
|
|
|
$lines = []; |
95
|
|
|
$idx = 0; |
96
|
|
|
$addressLines = is_array($this->lines) ? $this->lines : []; |
97
|
|
|
foreach ($addressLines as $line) { |
98
|
|
|
$idx++; |
99
|
|
|
$lines[] = sprintf( |
100
|
|
|
'<Line%d>%s</Line%1$d>', |
101
|
|
|
$idx, |
102
|
|
|
$this->xmlEncode($line) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
// If we don't have any address lines, we treat as having no address at all. |
106
|
|
|
return $idx ? $this->buildPhysicalAddressNodes($lines) : ''; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Build the Shipping Address Node |
111
|
|
|
* |
112
|
|
|
* @param array lines Street Address |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
protected function buildPhysicalAddressNodes(array $lines) |
116
|
|
|
{ |
117
|
|
|
return sprintf( |
118
|
|
|
'<%s>%s<City>%s</City>%s<CountryCode>%s</CountryCode>%s</%1$s>', |
119
|
|
|
$this->getPhysicalAddressRootNodeName(), |
120
|
|
|
implode('', $lines), |
121
|
|
|
$this->xmlEncode($this->getCity()), |
122
|
|
|
$this->serializeOptionalXmlEncodedValue('MainDivision', $this->getMainDivision()), |
123
|
|
|
$this->xmlEncode($this->getCountryCode()), |
124
|
|
|
$this->serializeOptionalXmlEncodedValue('PostalCode', $this->getPostalCode()) |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Make sure we have max 4 address lines of 70 chars max |
130
|
|
|
* |
131
|
|
|
* If there are more than 4 lines concatenate all extra lines with the 4th line. |
132
|
|
|
* |
133
|
|
|
* Truncate any lines to 70 chars max. |
134
|
|
|
* |
135
|
|
|
* @param string $lines |
136
|
|
|
* @return array or null |
137
|
|
|
*/ |
138
|
|
|
protected function cleanAddressLines($lines) |
139
|
|
|
{ |
140
|
|
|
$finalLines = null; |
141
|
|
|
|
142
|
|
|
if (is_string($lines)) { |
143
|
|
|
$trimmed = trim($lines); |
144
|
|
|
$addressLines = preg_split("/\n/", $trimmed, null, PREG_SPLIT_NO_EMPTY); |
145
|
|
|
|
146
|
|
|
$newLines = []; |
147
|
|
|
foreach ($addressLines as $line) { |
148
|
|
|
$newLines[] = $this->cleanString($line, 70); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if (count($newLines) > 4) { |
152
|
|
|
// concat lines beyond the four allowed down into the last line |
153
|
|
|
$newLines[3] = $this->cleanString(implode(' ', array_slice($newLines, 3)), 70); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$finalLines = array_slice($newLines, 0, 4); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return empty($finalLines) ? null : $finalLines; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Node name wrapping the physical address elements. |
164
|
|
|
* |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
abstract protected function getPhysicalAddressRootNodeName(); |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* encode the passed in string to be safe for xml if it is not null, |
171
|
|
|
* otherwise simply return the null parameter. |
172
|
|
|
* |
173
|
|
|
* @param string|null |
174
|
|
|
* @return string|null |
175
|
|
|
*/ |
176
|
|
|
abstract protected function xmlEncode($value = null); |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* encode the passed in string to be safe for xml if it is not null, |
180
|
|
|
* otherwise simply return the null parameter. |
181
|
|
|
* @param mixed $value |
182
|
|
|
* @return string | null |
183
|
|
|
*/ |
184
|
|
|
abstract protected function serializeOptionalXmlEncodedValue($name, $value); |
185
|
|
|
} |
186
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.