| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2024 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* This class implements common methods to manipulate the immutable |
| 16 |
* structure of a SimpleXMLElement object from a SOAP XML message. |
| 17 |
* |
| 18 |
* @since 1.17.1 (J) - 1.7.1 (WP) |
| 19 |
* |
| 20 |
* @see SimpleXMLElement |
| 21 |
* @see DomDocument |
| 22 |
*/ |
| 23 |
class VBOXmlSoap extends SimpleXMLElement |
| 24 |
{ |
| 25 |
/** |
| 26 |
* Appends the provided node. |
| 27 |
* |
| 28 |
* @param SimpleXMLElement $node The node to search. |
| 29 |
* |
| 30 |
* @return mixed The added child on success, false otherwise. |
| 31 |
*/ |
| 32 |
public function append(SimpleXMLElement $node) |
| 33 |
{ |
| 34 |
// create a DOM starting from the current XML |
| 35 |
$dom = dom_import_simplexml($this); |
| 36 |
|
| 37 |
// import the VCM node onto the update node |
| 38 |
$appendNode = $dom->ownerDocument->importNode(dom_import_simplexml($node), $deep = true); |
| 39 |
|
| 40 |
if ($appendNode) { |
| 41 |
// append the new node into the specified position |
| 42 |
return $dom->appendChild($appendNode); |
| 43 |
} |
| 44 |
|
| 45 |
return false; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Finds the provided node within the document and replaces it |
| 50 |
* with the latter. |
| 51 |
* |
| 52 |
* @param SimpleXMLElement $node The node to search. |
| 53 |
* |
| 54 |
* @return mixed The replaced child on success, false otherwise. |
| 55 |
*/ |
| 56 |
public function replace(SimpleXMLElement $node) |
| 57 |
{ |
| 58 |
// create a DOM starting from the current XML |
| 59 |
$dom = dom_import_simplexml($this); |
| 60 |
|
| 61 |
// returns a copy of the node to import and associates it with the current document |
| 62 |
$importNode = $dom->ownerDocument->importNode(dom_import_simplexml($node), $deep = true); |
| 63 |
|
| 64 |
if ($importNode) { |
| 65 |
// replace the existing node with the provided one |
| 66 |
$return = $dom->parentNode->replaceChild($importNode, $dom); |
| 67 |
} |
| 68 |
|
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Formats the XML string. |
| 74 |
* |
| 75 |
* @return string |
| 76 |
*/ |
| 77 |
public function formatXml() |
| 78 |
{ |
| 79 |
$xmlFeed = $this->asXml(); |
| 80 |
|
| 81 |
if (!$xmlFeed) { |
| 82 |
// prevent errors with empty XML strings |
| 83 |
return ''; |
| 84 |
} |
| 85 |
|
| 86 |
// format XML string feed |
| 87 |
$dom = new DOMDocument; |
| 88 |
$dom->preserveWhiteSpace = false; |
| 89 |
$dom->loadXML($xmlFeed); |
| 90 |
$dom->formatOutput = true; |
| 91 |
|
| 92 |
return $dom->saveXML(); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Returns the SimpleXMLElement object of the main Soap body message. |
| 97 |
* |
| 98 |
* @param string $ns The XML namespace. |
| 99 |
* @param bool $isPrefix Whether the namespace should be regarded as a prefix. |
| 100 |
* @param string $node The default body node name. |
| 101 |
* |
| 102 |
* @return SimpleXMLElement |
| 103 |
*/ |
| 104 |
public function getSoapBody($ns = 'soap', $isPrefix = true, $node = 'Body') |
| 105 |
{ |
| 106 |
return $this->children($ns, $isPrefix)->{$node}->children(); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Returns the SimpleXMLElement object from a recursive list of nested namespaces. |
| 111 |
* Useful to access a node that belongs to multiple and different namespaces. |
| 112 |
* |
| 113 |
* @param array $recursiveNs List of recursive namespace instruction pairs (ns, isPrefix, node). |
| 114 |
* |
| 115 |
* @return SimpleXMLElement |
| 116 |
* |
| 117 |
* @since 1.17.2 (J) - 1.7.2 (WP) |
| 118 |
*/ |
| 119 |
public function getSoapRecursiveNsElement(array $recursiveNs) |
| 120 |
{ |
| 121 |
// starting element |
| 122 |
$element = $this; |
| 123 |
|
| 124 |
// scan the list of namespace instructions to advance the elements |
| 125 |
foreach ($recursiveNs as $ind => $pairs) { |
| 126 |
// instructions |
| 127 |
list($ns, $isPrefix, $node) = $pairs; |
| 128 |
|
| 129 |
$node = (string) ($node ?? ''); |
| 130 |
|
| 131 |
if (!$node) { |
| 132 |
// abort |
| 133 |
return $this; |
| 134 |
} |
| 135 |
|
| 136 |
// attempt to get the requested element |
| 137 |
$childElement = $element->children((string) ($ns ?? ''), (bool) ($isPrefix ?? true)); |
| 138 |
|
| 139 |
if (!isset($childElement)) { |
| 140 |
// abort |
| 141 |
return $this; |
| 142 |
} |
| 143 |
|
| 144 |
if (count($recursiveNs) - 1 === $ind) { |
| 145 |
// exit from namespace |
| 146 |
$childElement = $childElement->children(); |
| 147 |
} else { |
| 148 |
// enter the next node |
| 149 |
$childElement = $childElement->{$node}; |
| 150 |
} |
| 151 |
|
| 152 |
// advance the element |
| 153 |
$element = $childElement; |
| 154 |
} |
| 155 |
|
| 156 |
return $element; |
| 157 |
} |
| 158 |
} |
| 159 |
|