Skip to content

Commit

Permalink
refactor: xml namespaces are stored in php constants
Browse files Browse the repository at this point in the history
  • Loading branch information
kouinkouin committed Oct 13, 2024
1 parent f80989d commit 639557a
Show file tree
Hide file tree
Showing 29 changed files with 146 additions and 389 deletions.
9 changes: 9 additions & 0 deletions src/Bpost.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ class Bpost
const LABEL_FORMAT_A4 = 'A4';
const LABEL_FORMAT_A6 = 'A6';

const NS_V3_GLOBAL = 'http://schema.post.be/shm/deepintegration/v3/';
const NS_V3_COMMON = 'http://schema.post.be/shm/deepintegration/v3/common';
const NS_V3_NATIONAL = 'http://schema.post.be/shm/deepintegration/v3/national';
const NS_V3_INTERNATIONAL = 'http://schema.post.be/shm/deepintegration/v3/international';
const NS_V5_GLOBAL = 'http://schema.post.be/shm/deepintegration/v5/';
const NS_V5_COMMON = 'http://schema.post.be/shm/deepintegration/v5/common';
const NS_V5_NATIONAL = 'http://schema.post.be/shm/deepintegration/v5/national';
const NS_V5_INTERNATIONAL = 'http://schema.post.be/shm/deepintegration/v5/international';

// URL for the api
const API_URL = 'https://shm-rest.bpost.cloud/services/shm';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Bpost\BpostApiClient\Bpost\HttpRequestBuilder;

use Bpost\BpostApiClient\Bpost;
use Bpost\BpostApiClient\Common\ValidatedValue\LabelFormat;
use DOMDocument;
use DOMException;
Expand Down Expand Up @@ -57,7 +58,7 @@ public function getXml()
$document->formatOutput = true;

$batchLabels = $document->createElement('batchLabels');
$batchLabels->setAttribute('xmlns', 'http://schema.post.be/shm/deepintegration/v3/');
$batchLabels->setAttribute('xmlns', Bpost::NS_V3_GLOBAL);
foreach ($this->references as $reference) {
$batchLabels->appendChild(
$document->createElement('order', $reference)
Expand Down
3 changes: 2 additions & 1 deletion src/Bpost/HttpRequestBuilder/ModifyOrderBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Bpost\BpostApiClient\Bpost\HttpRequestBuilder;

use Bpost\BpostApiClient\Bpost;
use Bpost\BpostApiClient\Bpost\Order\Box;
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
use DOMDocument;
Expand Down Expand Up @@ -40,7 +41,7 @@ public function getXml()
$document->formatOutput = true;

$orderUpdate = $document->createElement('orderUpdate');
$orderUpdate->setAttribute('xmlns', 'http://schema.post.be/shm/deepintegration/v3/');
$orderUpdate->setAttribute('xmlns', Bpost::NS_V3_GLOBAL);
$orderUpdate->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$orderUpdate->appendChild(
$document->createElement('status', $this->status)
Expand Down
31 changes: 7 additions & 24 deletions src/Bpost/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Bpost\BpostApiClient\Bpost;

use Bpost\BpostApiClient\Bpost;
use Bpost\BpostApiClient\Bpost\Order\Box;
use Bpost\BpostApiClient\Bpost\Order\Line;
use Bpost\BpostApiClient\Exception\BpostNotImplementedException;
Expand Down Expand Up @@ -158,30 +159,12 @@ public function toXML(DOMDocument $document, $accountId)
$order = $document->createElement(
'tns:order'
);
$order->setAttribute(
'xmlns:common',
'http://schema.post.be/shm/deepintegration/v5/common'
);
$order->setAttribute(
'xmlns:tns',
'http://schema.post.be/shm/deepintegration/v5/'
);
$order->setAttribute(
'xmlns',
'http://schema.post.be/shm/deepintegration/v5/national'
);
$order->setAttribute(
'xmlns:international',
'http://schema.post.be/shm/deepintegration/v5/international'
);
$order->setAttribute(
'xmlns:xsi',
'http://www.w3.org/2001/XMLSchema-instance'
);
$order->setAttribute(
'xsi:schemaLocation',
'http://schema.post.be/shm/deepintegration/v5/'
);
$order->setAttribute('xmlns:common', Bpost::NS_V5_COMMON);
$order->setAttribute('xmlns:tns', Bpost::NS_V5_GLOBAL);
$order->setAttribute('xmlns', Bpost::NS_V5_NATIONAL);
$order->setAttribute('xmlns:international', Bpost::NS_V5_INTERNATIONAL);
$order->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$order->setAttribute('xsi:schemaLocation', Bpost::NS_V5_GLOBAL);

$document->appendChild($order);

Expand Down
7 changes: 4 additions & 3 deletions src/Bpost/Order/Box.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Bpost\BpostApiClient\Bpost\Order;

use Bpost\BpostApiClient\Bpost;
use Bpost\BpostApiClient\Common\XmlHelper;
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
use Bpost\BpostApiClient\Exception\BpostNotImplementedException;
Expand Down Expand Up @@ -232,14 +233,14 @@ public static function createFromXML(SimpleXMLElement $xml)
$box->setSender(
Sender::createFromXML(
$xml->sender->children(
'http://schema.post.be/shm/deepintegration/v3/common'
Bpost::NS_V3_COMMON
)
)
);
}
if (isset($xml->nationalBox)) {
/** @var SimpleXMLElement $nationalBoxData */
$nationalBoxData = $xml->nationalBox->children('http://schema.post.be/shm/deepintegration/v3/national');
$nationalBoxData = $xml->nationalBox->children(Bpost::NS_V3_NATIONAL);

// build classname based on the tag name
$className = '\\Bpost\\BpostApiClient\\Bpost\\Order\\Box\\' . ucfirst($nationalBoxData->getName());
Expand All @@ -255,7 +256,7 @@ public static function createFromXML(SimpleXMLElement $xml)
}
if (isset($xml->internationalBox)) {
/** @var SimpleXMLElement $internationalBoxData */
$internationalBoxData = $xml->internationalBox->children('http://schema.post.be/shm/deepintegration/v3/international');
$internationalBoxData = $xml->internationalBox->children(Bpost::NS_V3_INTERNATIONAL);

// build classname based on the tag name
$className = '\\Bpost\\BpostApiClient\\Bpost\\Order\\Box\\' . ucfirst($internationalBoxData->getName());
Expand Down
7 changes: 3 additions & 4 deletions src/Bpost/Order/Box/At247.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Bpost\BpostApiClient\Bpost\Order\Box;

use Bpost\BpostApiClient\Bpost;
use Bpost\BpostApiClient\Bpost\Order\Box\National\Unregistered;
use Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging;
use Bpost\BpostApiClient\Bpost\Order\ParcelsDepotAddress;
Expand Down Expand Up @@ -322,7 +323,7 @@ public static function createFromXML(SimpleXMLElement $xml, National $self = nul
if (isset($xml->{'at24-7'}->options)) {
/** @var SimpleXMLElement $optionData */
foreach ($xml->{'at24-7'}->options as $optionData) {
$optionData = $optionData->children('http://schema.post.be/shm/deepintegration/v3/common');
$optionData = $optionData->children(Bpost::NS_V3_COMMON);

if (in_array($optionData->getName(), array(Messaging::MESSAGING_TYPE_INFO_DISTRIBUTED))) {
$option = Messaging::createFromXML($optionData);
Expand Down Expand Up @@ -365,9 +366,7 @@ public static function createFromXML(SimpleXMLElement $xml, National $self = nul
}
if (isset($xml->{'at24-7'}->parcelsDepotAddress)) {
/** @var SimpleXMLElement $parcelsDepotAddressData */
$parcelsDepotAddressData = $xml->{'at24-7'}->parcelsDepotAddress->children(
'http://schema.post.be/shm/deepintegration/v3/common'
);
$parcelsDepotAddressData = $xml->{'at24-7'}->parcelsDepotAddress->children(Bpost::NS_V3_COMMON);
$at247->setParcelsDepotAddress(
ParcelsDepotAddress::createFromXML($parcelsDepotAddressData)
);
Expand Down
5 changes: 3 additions & 2 deletions src/Bpost/Order/Box/AtBpost.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Bpost\BpostApiClient\Bpost\Order\Box;

use Bpost\BpostApiClient\Bpost;
use Bpost\BpostApiClient\Bpost\Order\Box\National\ShopHandlingInstruction;
use Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging;
use Bpost\BpostApiClient\Bpost\Order\PugoAddress;
Expand Down Expand Up @@ -281,7 +282,7 @@ public static function createFromXML(SimpleXMLElement $xml, National $self = nul
if (isset($xml->atBpost->options)) {
/** @var SimpleXMLElement $optionData */
foreach ($xml->atBpost->options as $optionData) {
$optionData = $optionData->children('http://schema.post.be/shm/deepintegration/v3/common');
$optionData = $optionData->children(Bpost::NS_V3_COMMON);

if (in_array(
$optionData->getName(),
Expand Down Expand Up @@ -329,7 +330,7 @@ public static function createFromXML(SimpleXMLElement $xml, National $self = nul
if (isset($xml->atBpost->pugoAddress)) {
/** @var SimpleXMLElement $pugoAddressData */
$pugoAddressData = $xml->atBpost->pugoAddress->children(
'http://schema.post.be/shm/deepintegration/v3/common'
Bpost::NS_V3_COMMON
);
$atBpost->setPugoAddress(
PugoAddress::createFromXML($pugoAddressData)
Expand Down
3 changes: 2 additions & 1 deletion src/Bpost/Order/Box/AtHome.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Bpost\BpostApiClient\Bpost\Order\Box;

use Bpost\BpostApiClient\Bpost;
use Bpost\BpostApiClient\Bpost\Order\Receiver;
use Bpost\BpostApiClient\Bpost\ProductConfiguration\Product;
use Bpost\BpostApiClient\Common\XmlHelper;
Expand Down Expand Up @@ -159,7 +160,7 @@ public static function createFromXML(SimpleXMLElement $xml, National $self = nul
if (isset($atHomeXml->receiver)) {
$self->setReceiver(
Receiver::createFromXML(
$atHomeXml->receiver->children('http://schema.post.be/shm/deepintegration/v3/common')
$atHomeXml->receiver->children(Bpost::NS_V3_COMMON)
)
);
}
Expand Down
9 changes: 4 additions & 5 deletions src/Bpost/Order/Box/AtIntlPugo.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Bpost\BpostApiClient\Bpost\Order\Box;

use Bpost\BpostApiClient\Bpost;
use Bpost\BpostApiClient\Bpost\Order\Box\National\ShopHandlingInstruction;
use Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging;
use Bpost\BpostApiClient\Bpost\Order\PugoAddress;
Expand Down Expand Up @@ -252,7 +253,7 @@ public static function createFromXML(\SimpleXMLElement $xml)
if (isset($xml->atIntlPugo->options)) {
/** @var \SimpleXMLElement $optionData */
foreach ($xml->atIntlPugo->options as $optionData) {
$optionData = $optionData->children('http://schema.post.be/shm/deepintegration/v3/common');
$optionData = $optionData->children(Bpost::NS_V3_COMMON);

if (in_array(
$optionData->getName(),
Expand Down Expand Up @@ -288,7 +289,7 @@ public static function createFromXML(\SimpleXMLElement $xml)
if (isset($xml->atIntlPugo->receiver) && $xml->atIntlPugo->receiver != '') {
$self->setReceiver(
Receiver::createFromXML(
$xml->atIntlPugo->receiver->children('http://schema.post.be/shm/deepintegration/v3/common')
$xml->atIntlPugo->receiver->children(Bpost::NS_V3_COMMON)
)
);
}
Expand All @@ -304,9 +305,7 @@ public static function createFromXML(\SimpleXMLElement $xml)
}
if (isset($xml->atIntlPugo->pugoAddress)) {
/** @var \SimpleXMLElement $pugoAddressData */
$pugoAddressData = $xml->atIntlPugo->pugoAddress->children(
'http://schema.post.be/shm/deepintegration/v3/common'
);
$pugoAddressData = $xml->atIntlPugo->pugoAddress->children(Bpost::NS_V3_COMMON);
$self->setPugoAddress(
PugoAddress::createFromXML($pugoAddressData)
);
Expand Down
3 changes: 2 additions & 1 deletion src/Bpost/Order/Box/BpostOnAppointment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Bpost\BpostApiClient\Bpost\Order\Box;

use Bpost\BpostApiClient\Bpost;
use Bpost\BpostApiClient\Bpost\Order\Receiver;
use Bpost\BpostApiClient\Common\XmlHelper;
use Bpost\BpostApiClient\Exception\XmlException\BpostXmlInvalidItemException;
Expand Down Expand Up @@ -125,7 +126,7 @@ public static function createFromXML(SimpleXMLElement $xml, National $self = nul
if (isset($bpostOnAppointmentXml->receiver)) {
$self->setReceiver(
Receiver::createFromXML(
$bpostOnAppointmentXml->receiver->children('http://schema.post.be/shm/deepintegration/v3/common')
$bpostOnAppointmentXml->receiver->children(Bpost::NS_V3_COMMON)
)
);
}
Expand Down
7 changes: 3 additions & 4 deletions src/Bpost/Order/Box/International.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Bpost\BpostApiClient\Bpost\Order\Box;

use Bpost\BpostApiClient\Bpost;
use Bpost\BpostApiClient\Bpost\Order\Box\CustomsInfo\CustomsInfo;
use Bpost\BpostApiClient\Bpost\Order\Box\International\ParcelContent;
use Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging;
Expand Down Expand Up @@ -295,7 +296,7 @@ public static function createFromXML(SimpleXMLElement $xml)
}
if (isset($xml->international->options)) {
/** @var SimpleXMLElement $optionData */
$options = $xml->international->options->children('http://schema.post.be/shm/deepintegration/v3/common');
$options = $xml->international->options->children(Bpost::NS_V3_COMMON);
foreach ($options as $optionData) {
switch ($optionData->getName()) {
case Messaging::MESSAGING_TYPE_INFO_DISTRIBUTED:
Expand All @@ -322,9 +323,7 @@ public static function createFromXML(SimpleXMLElement $xml)
);
}
if (isset($xml->international->receiver)) {
$receiverData = $xml->international->receiver->children(
'http://schema.post.be/shm/deepintegration/v3/common'
);
$receiverData = $xml->international->receiver->children(Bpost::NS_V3_COMMON);
$international->setReceiver(
Receiver::createFromXML($receiverData)
);
Expand Down
3 changes: 2 additions & 1 deletion src/Bpost/Order/Box/National.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Bpost\BpostApiClient\Bpost\Order\Box;

use Bpost\BpostApiClient\Bpost;
use Bpost\BpostApiClient\Bpost\Order\Box\OpeningHour\Day;
use Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging;
use Bpost\BpostApiClient\Bpost\Order\Box\Option\Option;
Expand Down Expand Up @@ -234,7 +235,7 @@ public static function createFromXML(SimpleXMLElement $nationalXml, National $se
if (isset($nationalXml->options) && !empty($nationalXml->options)) {
/** @var SimpleXMLElement $optionData */
foreach ($nationalXml->options as $optionData) {
$optionData = $optionData->children('http://schema.post.be/shm/deepintegration/v3/common');
$optionData = $optionData->children(Bpost::NS_V3_COMMON);

if (in_array($optionData->getName(), array(
Messaging::MESSAGING_TYPE_INFO_DISTRIBUTED,
Expand Down
2 changes: 1 addition & 1 deletion src/Bpost/Order/Box/Option/Insured.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Bpost\BpostApiClient\Bpost\Order\Box\Option;

use Bpost\BpostApiClient\Bpost;
use Bpost\BpostApiClient\Common\XmlHelper;
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
use DomDocument;
Expand Down Expand Up @@ -137,7 +138,6 @@ public function __construct($type, $value = null)
* Return the object as an array for usage in the XML
*
* @param DomDocument $document
* @param string $prefix
*
* @return DomElement
*
Expand Down
3 changes: 2 additions & 1 deletion src/Bpost/Order/Box/Option/Messaging.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Bpost\BpostApiClient\Bpost\Order\Box\Option;

use Bpost\BpostApiClient\Bpost;
use Bpost\BpostApiClient\Common\XmlHelper;
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidLengthException;
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
Expand Down Expand Up @@ -236,7 +237,7 @@ public static function createFromXML(SimpleXMLElement $xml)
{
$messaging = new Messaging($xml->getName(), (string) $xml->attributes()->language);

$children = $xml->children('http://schema.post.be/shm/deepintegration/v3/common');
$children = $xml->children(Bpost::NS_V3_COMMON);
if ((string) $children->emailAddress !== '') {
$messaging->setEmailAddress((string) $children->emailAddress);
}
Expand Down
31 changes: 7 additions & 24 deletions tests/Bpost/Order/Box/AtHomeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Bpost\Order\Box;

use Bpost\BpostApiClient\Bpost;
use Bpost\BpostApiClient\Bpost\Order\Address;
use Bpost\BpostApiClient\Bpost\Order\Box\AtHome;
use Bpost\BpostApiClient\Bpost\Order\Receiver;
Expand Down Expand Up @@ -36,30 +37,12 @@ private function createDomDocument()
*/
private function generateDomDocument(DOMDocument $document, DOMElement $element)
{
$element->setAttribute(
'xmlns:common',
'http://schema.post.be/shm/deepintegration/v5/common'
);
$element->setAttribute(
'xmlns:tns',
'http://schema.post.be/shm/deepintegration/v5/'
);
$element->setAttribute(
'xmlns',
'http://schema.post.be/shm/deepintegration/v5/national'
);
$element->setAttribute(
'xmlns:international',
'http://schema.post.be/shm/deepintegration/v5/international'
);
$element->setAttribute(
'xmlns:xsi',
'http://www.w3.org/2001/XMLSchema-instance'
);
$element->setAttribute(
'xsi:schemaLocation',
'http://schema.post.be/shm/deepintegration/v5/'
);
$element->setAttribute('xmlns:common', Bpost::NS_V5_COMMON);
$element->setAttribute('xmlns:tns', Bpost::NS_V5_GLOBAL);
$element->setAttribute('xmlns', Bpost::NS_V5_NATIONAL);
$element->setAttribute('xmlns:international', Bpost::NS_V5_INTERNATIONAL);
$element->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$element->setAttribute('xsi:schemaLocation', Bpost::NS_V5_GLOBAL);

$document->appendChild($element);

Expand Down
Loading

0 comments on commit 639557a

Please sign in to comment.