|
4 | 4 |
|
5 | 5 | use Bpost\BpostApiClient\Bpost; |
6 | 6 | use Bpost\BpostApiClient\Bpost\Order\Box\OpeningHour\Day; |
| 7 | +use Bpost\BpostApiClient\Bpost\Order\Box\Option\CashOnDelivery; |
7 | 8 | use Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging; |
8 | 9 | use Bpost\BpostApiClient\Bpost\Order\Box\Option\Option; |
9 | 10 | use Bpost\BpostApiClient\BpostException; |
10 | 11 | use Bpost\BpostApiClient\Common\ComplexAttribute; |
11 | 12 | use Bpost\BpostApiClient\Common\XmlHelper; |
| 13 | +use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidLengthException; |
| 14 | +use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException; |
12 | 15 | use Bpost\BpostApiClient\Exception\BpostNotImplementedException; |
13 | 16 | use Bpost\BpostApiClient\Exception\XmlException\BpostXmlInvalidItemException; |
14 | 17 | use DomDocument; |
@@ -232,24 +235,10 @@ public static function createFromXML(SimpleXMLElement $nationalXml, National $se |
232 | 235 | ); |
233 | 236 | } |
234 | 237 |
|
235 | | - if (isset($nationalXml->options) && !empty($nationalXml->options)) { |
| 238 | + if (!empty($nationalXml->options)) { |
236 | 239 | /** @var SimpleXMLElement $optionData */ |
237 | | - foreach ($nationalXml->options as $optionData) { |
238 | | - $optionData = $optionData->children(Bpost::NS_V3_COMMON); |
239 | | - |
240 | | - if (in_array($optionData->getName(), array( |
241 | | - Messaging::MESSAGING_TYPE_INFO_DISTRIBUTED, |
242 | | - Messaging::MESSAGING_TYPE_INFO_NEXT_DAY, |
243 | | - Messaging::MESSAGING_TYPE_INFO_REMINDER, |
244 | | - Messaging::MESSAGING_TYPE_KEEP_ME_INFORMED, |
245 | | - )) |
246 | | - ) { |
247 | | - $option = Messaging::createFromXML($optionData); |
248 | | - } else { |
249 | | - $option = self::getOptionFromOptionData($optionData); |
250 | | - } |
251 | | - |
252 | | - $self->addOption($option); |
| 240 | + foreach ($nationalXml->options->children(Bpost::NS_V3_COMMON) as $optionData) { |
| 241 | + $self->addOption(self::getOptionFromOptionData($optionData)); |
253 | 242 | } |
254 | 243 | } |
255 | 244 |
|
@@ -280,15 +269,30 @@ public static function createFromXML(SimpleXMLElement $nationalXml, National $se |
280 | 269 | * @return Option |
281 | 270 | * |
282 | 271 | * @throws BpostNotImplementedException |
| 272 | + * @throws BpostInvalidLengthException |
| 273 | + * @throws BpostInvalidValueException |
283 | 274 | */ |
284 | 275 | protected static function getOptionFromOptionData(SimpleXMLElement $optionData) |
285 | 276 | { |
286 | | - $className = '\\Bpost\\BpostApiClient\\Bpost\\Order\\Box\\Option\\' . ucfirst($optionData->getName()); |
287 | | - XmlHelper::assertMethodCreateFromXmlExists($className); |
| 277 | + switch ($optionData->getName()) { |
| 278 | + case Messaging::MESSAGING_TYPE_INFO_DISTRIBUTED: |
| 279 | + case Messaging::MESSAGING_TYPE_INFO_NEXT_DAY: |
| 280 | + case Messaging::MESSAGING_TYPE_INFO_REMINDER: |
| 281 | + case Messaging::MESSAGING_TYPE_KEEP_ME_INFORMED: |
| 282 | + return Messaging::createFromXML($optionData); |
288 | 283 |
|
289 | | - return call_user_func( |
290 | | - array($className, 'createFromXML'), |
291 | | - $optionData |
292 | | - ); |
| 284 | + case 'cod': |
| 285 | + return CashOnDelivery::createFromXML($optionData); |
| 286 | + |
| 287 | + default: |
| 288 | + $className = '\\Bpost\\BpostApiClient\\Bpost\\Order\\Box\\Option\\' . ucfirst($optionData->getName()); |
| 289 | + |
| 290 | + XmlHelper::assertMethodCreateFromXmlExists($className); |
| 291 | + |
| 292 | + return call_user_func( |
| 293 | + array($className, 'createFromXML'), |
| 294 | + $optionData |
| 295 | + ); |
| 296 | + } |
293 | 297 | } |
294 | 298 | } |
0 commit comments