| @@ 18-185 (lines=168) @@ | ||
| 15 | ||
| 16 | namespace eBayEnterprise\RetailOrderManagement\Payload\Checkout; |
|
| 17 | ||
| 18 | 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 | ||
| @@ 18-189 (lines=172) @@ | ||
| 15 | ||
| 16 | namespace eBayEnterprise\RetailOrderManagement\Payload\Payment; |
|
| 17 | ||
| 18 | trait TShippingAddress |
|
| 19 | { |
|
| 20 | /** @var array */ |
|
| 21 | protected $shipToLines; |
|
| 22 | /** @var string */ |
|
| 23 | protected $shipToCity; |
|
| 24 | /** @var string */ |
|
| 25 | protected $shipToMainDivision; |
|
| 26 | /** @var string */ |
|
| 27 | protected $shipToCountryCode; |
|
| 28 | /** @var string */ |
|
| 29 | protected $shipToPostalCode; |
|
| 30 | ||
| 31 | public function getShipToLines() |
|
| 32 | { |
|
| 33 | return is_array($this->shipToLines) ? implode("\n", $this->shipToLines) : null; |
|
| 34 | } |
|
| 35 | ||
| 36 | public function setShipToLines($lines) |
|
| 37 | { |
|
| 38 | $this->shipToLines = $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 | protected function cleanAddressLines($lines) |
|
| 53 | { |
|
| 54 | $finalLines = null; |
|
| 55 | ||
| 56 | if (is_string($lines)) { |
|
| 57 | $trimmed = trim($lines); |
|
| 58 | $addressLines = preg_split("/\n/", $trimmed, null, PREG_SPLIT_NO_EMPTY); |
|
| 59 | ||
| 60 | $newLines = []; |
|
| 61 | foreach ($addressLines as $line) { |
|
| 62 | $newLines[] = $this->cleanString($line, 70); |
|
| 63 | } |
|
| 64 | ||
| 65 | if (count($newLines) > 4) { |
|
| 66 | // concat lines beyond the four allowed down into the last line |
|
| 67 | $newLines[3] = $this->cleanString(implode(' ', array_slice($newLines, 3)), 70); |
|
| 68 | } |
|
| 69 | ||
| 70 | $finalLines = array_slice($newLines, 0, 4); |
|
| 71 | } |
|
| 72 | ||
| 73 | return empty($finalLines) ? null : $finalLines; |
|
| 74 | } |
|
| 75 | ||
| 76 | /** |
|
| 77 | * Aggregate the shipTo address lines into the ShippingAddress node. This is an optional node. |
|
| 78 | * |
|
| 79 | * @return string |
|
| 80 | */ |
|
| 81 | protected function serializeShippingAddress() |
|
| 82 | { |
|
| 83 | $lines = []; |
|
| 84 | $idx = 0; |
|
| 85 | $shipToLines = is_array($this->shipToLines) ? $this->shipToLines : []; |
|
| 86 | foreach ($shipToLines as $line) { |
|
| 87 | $idx++; |
|
| 88 | $lines[] = sprintf( |
|
| 89 | '<Line%d>%s</Line%1$d>', |
|
| 90 | $idx, |
|
| 91 | $this->xmlEncode($line) |
|
| 92 | ); |
|
| 93 | } |
|
| 94 | // If we don't have any address lines, we treat as having no address at all. |
|
| 95 | return ($idx) ? $this->buildShippingAddressNode($lines) : ''; |
|
| 96 | } |
|
| 97 | ||
| 98 | /** |
|
| 99 | * Build the Shipping Address Node |
|
| 100 | * @param array lines Street Address |
|
| 101 | * @return type |
|
| 102 | */ |
|
| 103 | protected function buildShippingAddressNode(array $lines) |
|
| 104 | { |
|
| 105 | return sprintf( |
|
| 106 | '<ShippingAddress>%s<City>%s</City>%s<CountryCode>%s</CountryCode>%s</ShippingAddress>', |
|
| 107 | implode('', $lines), |
|
| 108 | $this->xmlEncode($this->getShipToCity()), |
|
| 109 | $this->serializeOptionalXmlEncodedValue('MainDivision', $this->getShipToMainDivision()), |
|
| 110 | $this->xmlEncode($this->getShipToCountryCode()), |
|
| 111 | $this->serializeOptionalXmlEncodedValue('PostalCode', $this->getShipToPostalCode()) |
|
| 112 | ); |
|
| 113 | } |
|
| 114 | ||
| 115 | public function getShipToCity() |
|
| 116 | { |
|
| 117 | return $this->shipToCity; |
|
| 118 | } |
|
| 119 | ||
| 120 | public function setShipToCity($city) |
|
| 121 | { |
|
| 122 | $this->shipToCity = $this->cleanString($city, 35); |
|
| 123 | return $this; |
|
| 124 | } |
|
| 125 | ||
| 126 | public function getShipToMainDivision() |
|
| 127 | { |
|
| 128 | return $this->shipToMainDivision; |
|
| 129 | } |
|
| 130 | ||
| 131 | public function setShipToMainDivision($div) |
|
| 132 | { |
|
| 133 | $this->shipToMainDivision = $this->cleanString($div, 35); |
|
| 134 | return $this; |
|
| 135 | } |
|
| 136 | ||
| 137 | public function getShipToCountryCode() |
|
| 138 | { |
|
| 139 | return $this->shipToCountryCode; |
|
| 140 | } |
|
| 141 | ||
| 142 | public function setShipToCountryCode($code) |
|
| 143 | { |
|
| 144 | $cleaned = $this->cleanString($code, 40); |
|
| 145 | $this->shipToCountryCode = strlen($cleaned) >= 2 ? $cleaned : null; |
|
| 146 | return $this; |
|
| 147 | } |
|
| 148 | ||
| 149 | public function getShipToPostalCode() |
|
| 150 | { |
|
| 151 | return $this->shipToPostalCode; |
|
| 152 | } |
|
| 153 | ||
| 154 | public function setShipToPostalCode($code) |
|
| 155 | { |
|
| 156 | $this->shipToPostalCode = $this->cleanString($code, 15); |
|
| 157 | return $this; |
|
| 158 | } |
|
| 159 | ||
| 160 | /** |
|
| 161 | * Trim any white space and return the resulting string truncating to $maxLength. |
|
| 162 | * |
|
| 163 | * Return null if the result is an empty string or not a string |
|
| 164 | * |
|
| 165 | * @param string $string |
|
| 166 | * @param int $maxLength |
|
| 167 | * @return string or null |
|
| 168 | */ |
|
| 169 | abstract protected function cleanString($string, $maxLength); |
|
| 170 | ||
| 171 | /** |
|
| 172 | * Serialize an optional element containing a string. The value will be |
|
| 173 | * xml-encoded if is not null. |
|
| 174 | * |
|
| 175 | * @param string |
|
| 176 | * @param string |
|
| 177 | * @return string |
|
| 178 | */ |
|
| 179 | abstract protected function serializeOptionalXmlEncodedValue($name, $value); |
|
| 180 | ||
| 181 | /** |
|
| 182 | * encode the passed in string to be safe for xml if it is not null, |
|
| 183 | * otherwise simply return the null parameter. |
|
| 184 | * |
|
| 185 | * @param string|null |
|
| 186 | * @return string|null |
|
| 187 | */ |
|
| 188 | abstract protected function xmlEncode($value = null); |
|
| 189 | } |
|
| 190 | ||