Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some additions to PostalAddress and ShipTo #1

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/CXml/Models/Messages/BillTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,33 @@ class BillTo implements RequestInterface
public function parse(\SimpleXMLElement $billToXml): void
{

if ($data = (string) $billToXml->attributes()->isoCountryCode) {
if ($data = (string)$billToXml->attributes()->isoCountryCode) {
$this->isoCountryCode = $data;
}
if ($data = (string) $billToXml->attributes()->addressID) {
if ($data = (string)$billToXml->attributes()->addressID) {
$this->addressId = $data;
}
if ($data = (string) $billToXml->attributes()->addressIDDomain) {
if ($data = (string)$billToXml->attributes()->addressIDDomain) {
$this->addressIdDomain = $data;
}

$this->name = $billToXml->xpath('Name')[0];
$name = $billToXml->xpath('Name');

if ($postalAddressElement = current($billToXml->xpath('PostalAddress'))) {
if ($name) {
$this->name = (string)$name[0];
}

if (strlen($this->name) === 0) {
$this->name = (string)$billToXml->xpath('Address/Name')[0];
}

$postalAddressElement = current($billToXml->xpath('PostalAddress'));

if (!$postalAddressElement) {
$postalAddressElement = current($billToXml->xpath('Address/PostalAddress'));
}

if ($postalAddressElement) {
$this->postalAddress = new PostalAddress();
$this->postalAddress->parse($postalAddressElement);
}
Expand Down
29 changes: 17 additions & 12 deletions src/CXml/Models/Messages/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,18 @@ public function setSenderCredentials(array $senderCredentials): Header
return $this;
}

public function parse(\SimpleXMLElement $headerXml, $validate = false) : void
public function parse(\SimpleXMLElement $headerXml, $validate = false): void
{
$this->senderIdentity = (string)$headerXml->xpath('Sender/Credential/Identity')[0];
$this->senderSharedSecret = (string)$headerXml->xpath('Sender/Credential/SharedSecret')[0];
$identity = $headerXml->xpath('Sender/Credential/Identity');
$sharedSecret = $headerXml->xpath('Sender/Credential/SharedSecret');

if ($identity) {
$this->senderIdentity = (string)$identity[0];
}

if ($sharedSecret) {
$this->senderSharedSecret = (string)$sharedSecret[0];
}


// cXml Spec as of January 2021
Expand All @@ -160,7 +168,7 @@ public function parse(\SimpleXMLElement $headerXml, $validate = false) : void
// sends payload that violates this....
if ($fromCredentials = $headerXml->xpath('From/Credential')) {
$this->fromIdentity = (string)$fromCredentials[0]->xpath('Identity')[0];
$this->fromDomain = (string)$fromCredentials[0]->attributes()->domain;
$this->fromDomain = (string)$fromCredentials[0]->attributes()->domain;

$domainValidation = [];
foreach ($fromCredentials as $fromCredential) {
Expand Down Expand Up @@ -198,7 +206,7 @@ public function parse(\SimpleXMLElement $headerXml, $validate = false) : void
}
}

public function render(\SimpleXMLElement $parentNode) : void
public function render(\SimpleXMLElement $parentNode): void
{
$headerNode = $parentNode->addChild('Header');

Expand All @@ -207,8 +215,7 @@ public function render(\SimpleXMLElement $parentNode) : void
foreach ($this->fromCredentials as $fromCredential) {
$fromCredential->render($fromNode);
}
}
else {
} else {
$this->addNode($headerNode, 'From', htmlspecialchars($this->fromIdentity ?? 'Unknown', ENT_XML1 | ENT_COMPAT, 'UTF-8'));
}

Expand All @@ -217,8 +224,7 @@ public function render(\SimpleXMLElement $parentNode) : void
foreach ($this->toCredentials as $toCredential) {
$toCredential->render($toNode);
}
}
else {
} else {
$this->addNode($headerNode, 'To', 'Unknown');
}

Expand All @@ -227,14 +233,13 @@ public function render(\SimpleXMLElement $parentNode) : void
foreach ($this->senderCredentials as $senderCredential) {
$senderCredential->render($senderNode);
}
}
else {
} else {
$this->addNode($headerNode, 'Sender', $this->getSenderIdentity() ?? 'Unknown')
->addChild('UserAgent', 'Unknown');
}
}

private function addNode(\SimpleXMLElement $parentNode, string $nodeName, string $identity) : \SimpleXMLElement
private function addNode(\SimpleXMLElement $parentNode, string $nodeName, string $identity): \SimpleXMLElement
{
$node = $parentNode->addChild($nodeName);

Expand Down
93 changes: 88 additions & 5 deletions src/CXml/Models/Messages/ItemOut.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ class ItemOut implements RequestInterface
* @var \CXml\Models\Messages\Contact
*/
protected $contact;
protected $unitPrice;
protected $unitPriceCurrency;
protected $comments;
protected $description;

public function parse(\SimpleXMLElement $requestNode): void
{
Expand Down Expand Up @@ -106,16 +110,30 @@ public function parse(\SimpleXMLElement $requestNode): void
}

if ($node = current($requestNode->xpath('ItemID/SupplierPartID'))) {
$this->itemIdSupplierPartID = (string) $node;
$this->itemIdSupplierPartID = (string)$node;
}
if ($node = current($requestNode->xpath('ItemID/SupplierPartAuxiliaryID'))) {
$this->itemIdSupplierPartAuxiliaryID = (string) $node;
$this->itemIdSupplierPartAuxiliaryID = (string)$node;
}
if ($node = current($requestNode->xpath('ItemID/BuyerPartID'))) {
$this->itemIdBuyerPartID = (string) $node;
$this->itemIdBuyerPartID = (string)$node;
}
if ($node = current($requestNode->xpath('ItemID/IdReference'))) {
$this->itemIdIdReference = (string) $node;
$this->itemIdIdReference = (string)$node;
}
if ($node = current($requestNode->xpath('ItemDetail/UnitPrice/Money'))) {
$this->unitPrice = (string)$node;
$attrs = $node->attributes();

if (isset($attrs['currency'])) {
$this->unitPriceCurrency = (string)$attrs['currency'];
}
}
if ($node = current($requestNode->xpath('Comments'))) {
$this->comments = (string)$node;
}
if ($node = current($requestNode->xpath('ItemDetail/Description'))) {
$this->description = (string)$node;
}
}

Expand Down Expand Up @@ -191,7 +209,8 @@ public function getItemIdSupplierPartAuxiliaryID()
*/
public function setItemIdSupplierPartAuxiliaryID(
$itemIdSupplierPartAuxiliaryID
): self {
): self
{
$this->itemIdSupplierPartAuxiliaryID = $itemIdSupplierPartAuxiliaryID;
return $this;
}
Expand Down Expand Up @@ -747,4 +766,68 @@ public function setContact(Contact $contact): self
return $this;
}

/**
* @return mixed
*/
public function getUnitPrice()
{
return $this->unitPrice;
}

/**
* @param mixed $unitPrice
*/
public function setUnitPrice($unitPrice): void
{
$this->unitPrice = $unitPrice;
}

/**
* @return mixed
*/
public function getUnitPriceCurrency()
{
return $this->unitPriceCurrency;
}

/**
* @param mixed $unitPriceCurrency
*/
public function setUnitPriceCurrency($unitPriceCurrency): void
{
$this->unitPriceCurrency = $unitPriceCurrency;
}

/**
* @return mixed
*/
public function getComments()
{
return $this->comments;
}

/**
* @param mixed $comments
*/
public function setComments($comments): void
{
$this->comments = $comments;
}

/**
* @return mixed
*/
public function getDescription()
{
return $this->description;
}

/**
* @param mixed $description
*/
public function setDescription($description): void
{
$this->description = $description;
}

}
33 changes: 24 additions & 9 deletions src/CXml/Models/Messages/PostalAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class PostalAddress implements RequestInterface, MessageInterface
* Optional Properties
*/

private $isoCountryCode;

/**
* @var string[]
*/
Expand All @@ -38,6 +40,11 @@ public function getCountry()
return $this->country;
}

public function getISOCountryCode()
{
return $this->isoCountryCode;
}

/**
* @param string $country
*
Expand Down Expand Up @@ -163,37 +170,45 @@ public function setPostalCode($postalCode)
return $this;
}

public function parse(\SimpleXMLElement $contactXml) : void
public function parse(\SimpleXMLElement $contactXml): void
{
if ($data = $contactXml->xpath('Country')) {
$this->country = (string) current($data);
$current = current($data);

$attr = $current->attributes();

if ($attr && isset($attr->isoCountryCode)) {
$this->isoCountryCode = (string)$attr->isoCountryCode;
}

$this->country = (string)$current;
}
if ($data = $contactXml->xpath('City')) {
$this->city = (string) current($data);
$this->city = (string)current($data);
}
if ($data = $contactXml->xpath('Street')) {
foreach ($data as $street) {
$this->street[] = (string) $street;
$this->street[] = (string)$street;
}
}

if ($data = $contactXml->xpath('DeliverTo')) {
foreach ($data as $deliverTo) {
$this->deliverTo[] = (string) $deliverTo;
$this->deliverTo[] = (string)$deliverTo;
}
}
if ($data = $contactXml->xpath('Municipality')) {
$this->municipality = (string) current($data);
$this->municipality = (string)current($data);
}
if ($data = $contactXml->xpath('State')) {
$this->state = (string) current($data);
$this->state = (string)current($data);
}
if ($data = $contactXml->xpath('PostalCode')) {
$this->postalCode = (string) current($data);
$this->postalCode = (string)current($data);
}
}

public function render(\SimpleXMLElement $parentNode) : void
public function render(\SimpleXMLElement $parentNode): void
{
if ($this->country) {
$parentNode->addChild('Country', htmlspecialchars($this->country, ENT_XML1 | ENT_COMPAT, 'UTF-8'));
Expand Down
34 changes: 24 additions & 10 deletions src/CXml/Models/Messages/ShipTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,44 @@ class ShipTo implements RequestInterface
/**
* @var \CXml\Models\Messages\PostalAddress
*/
private $address;
private $postalAddress;

protected $isoCountryCode;
protected $addressId;
protected $addressIdDomain;

public function parse(\SimpleXMLElement $billToXml) : void
public function parse(\SimpleXMLElement $billToXml): void
{

if ($data = (string) $billToXml->attributes()->isoCountryCode) {
if ($data = (string)$billToXml->attributes()->isoCountryCode) {
$this->isoCountryCode = $data;
}
if ($data = (string) $billToXml->attributes()->addressID) {
if ($data = (string)$billToXml->attributes()->addressID) {
$this->addressId = $data;
}
if ($data = (string) $billToXml->attributes()->addressIDDomain) {
if ($data = (string)$billToXml->attributes()->addressIDDomain) {
$this->addressIdDomain = $data;
}

$this->name = $billToXml->xpath('Name')[0];
$name = $billToXml->xpath('Name');

if ($postalAddressElement = current($billToXml->xpath('PostalAddress'))) {
$this->address = new PostalAddress();
$this->address->parse($postalAddressElement);
if ($name) {
$this->name = (string)$name[0];
}

if (strlen($this->name) === 0 && $billToXml->xpath('Address/Name')) {
$this->name = (string)$billToXml->xpath('Address/Name')[0];
}

$postalAddressElement = current($billToXml->xpath('PostalAddress'));

if (!$postalAddressElement) {
$postalAddressElement = current($billToXml->xpath('Address/PostalAddress'));
}

if ($postalAddressElement) {
$this->postalAddress = new PostalAddress();
$this->postalAddress->parse($postalAddressElement);
}
}

Expand All @@ -69,7 +83,7 @@ public function setName($name)
/**
* @return \CXml\Models\Messages\PostalAddress
*/
public function getPostalAddress(): PostalAddress
public function getPostalAddress(): ?PostalAddress
{
return $this->postalAddress;
}
Expand Down
Loading