| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* E2pdf Zip Helper |
| 5 |
* |
| 6 |
* @copyright Copyright 2017 https://e2pdf.com |
| 7 |
* @license GPL v2 |
| 8 |
* @version 1 |
| 9 |
* @link https://e2pdf.com |
| 10 |
* @since 0.00.01 |
| 11 |
*/ |
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
die('Access denied.'); |
| 14 |
} |
| 15 |
|
| 16 |
class Helper_E2pdf_Xml { |
| 17 |
|
| 18 |
private $xml; |
| 19 |
|
| 20 |
/** |
| 21 |
* Check if extension simplexml available |
| 22 |
* |
| 23 |
* @return bool |
| 24 |
*/ |
| 25 |
public function check() { |
| 26 |
if (extension_loaded('simplexml')) { |
| 27 |
return true; |
| 28 |
} else { |
| 29 |
return false; |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Create new XML |
| 35 |
* |
| 36 |
* @param string $key - Wrapper of XML |
| 37 |
* |
| 38 |
* @return object - XML Object |
| 39 |
*/ |
| 40 |
public function create($key = false) { |
| 41 |
$this->set('xml', new Helper_E2pdf_SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><' . $key . '></' . $key . '>')); |
| 42 |
return $this->get('xml'); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Get XML file in Base64 |
| 47 |
* |
| 48 |
* @return string |
| 49 |
*/ |
| 50 |
public function get_xml() { |
| 51 |
if ($this->get('xml')) { |
| 52 |
return base64_encode($this->get('xml')->asXML()); |
| 53 |
} |
| 54 |
return ''; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Set option |
| 59 |
* |
| 60 |
* @param string $key - Key of option |
| 61 |
* @param string $value - Value of option |
| 62 |
* |
| 63 |
* @return bool - Status of setting option |
| 64 |
*/ |
| 65 |
public function set($key, $value) { |
| 66 |
if (!isset($this->xml)) { |
| 67 |
$this->xml = new stdClass(); |
| 68 |
} |
| 69 |
$this->xml->$key = $value; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Get option by key |
| 74 |
* |
| 75 |
* @param string $key - Key to get assigned option value |
| 76 |
* |
| 77 |
* @return mixed |
| 78 |
*/ |
| 79 |
public function get($key) { |
| 80 |
if (isset($this->xml->$key)) { |
| 81 |
return $this->xml->$key; |
| 82 |
} else { |
| 83 |
return false; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get Element Attribute Value |
| 89 |
* |
| 90 |
* @param object $element - XML Element |
| 91 |
* @param string $attribute - Attribute Name |
| 92 |
* |
| 93 |
* @return string|bool |
| 94 |
*/ |
| 95 |
public function get_node_value($element, $attribute = '') { |
| 96 |
$value = ""; |
| 97 |
if (is_object($element) && $attribute && isset($element->attributes) && $element->attributes->getNamedItem($attribute)) { |
| 98 |
$value = $element->attributes->getNamedItem($attribute)->nodeValue; |
| 99 |
} |
| 100 |
return $value; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Check Element Attribute |
| 105 |
* |
| 106 |
* @param object $element - XML Element |
| 107 |
* @param string $attribute - Attribute Name |
| 108 |
* |
| 109 |
* @return string|bool |
| 110 |
*/ |
| 111 |
public function check_node_value($element, $attribute = '') { |
| 112 |
if (is_object($element) && $attribute && isset($element->attributes) && $element->attributes->getNamedItem($attribute)) { |
| 113 |
return true; |
| 114 |
} |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Set Element Attribute |
| 120 |
* |
| 121 |
* @param object $element - XML Element |
| 122 |
* @param string $attribute - Attribute Name |
| 123 |
* @param string $value - Attribute Value |
| 124 |
* @param bool $parent - if need to modify parent node |
| 125 |
* |
| 126 |
* @return object |
| 127 |
*/ |
| 128 |
public function set_node_value($element, $attribute = '', $value = '', $parent = false) { |
| 129 |
|
| 130 |
if ($parent) { |
| 131 |
if (is_object($element) && $attribute && isset($element->parentNode->attributes)) { |
| 132 |
if ($element->parentNode->attributes->getNamedItem($attribute)) { |
| 133 |
$element->parentNode->attributes->getNamedItem($attribute)->nodeValue = $value; |
| 134 |
} elseif ($this->get('dom')) { |
| 135 |
$attr = $this->get('dom')->createAttribute($attribute); |
| 136 |
$attr->value = $value; |
| 137 |
$element->parentNode->appendChild($attr); |
| 138 |
} |
| 139 |
} |
| 140 |
} else { |
| 141 |
if (is_object($element) && $attribute && isset($element->attributes)) { |
| 142 |
if ($element->attributes->getNamedItem($attribute)) { |
| 143 |
$element->attributes->getNamedItem($attribute)->nodeValue = $value; |
| 144 |
} elseif ($this->get('dom')) { |
| 145 |
$attr = $this->get('dom')->createAttribute($attribute); |
| 146 |
$attr->value = $value; |
| 147 |
$element->appendChild($attr); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
return $element; |
| 153 |
} |
| 154 |
|
| 155 |
} |
| 156 |
|