<?php
namespace ModulesAbstract\ElectronicInvoice; class SimpleXmlElementExtended extends \Engine\SimpleXmlElement { public function getDefinedNamespaces() { return [ null => null, 'etsi' => 'http://uri.etsi.org/01903/v1.3.2#', 'ds' => 'http://www.w3.org/2000/09/xmldsig#', ]; } public function asCanonicalDefault() { $xmlStr = $this->asDOMElement()->C14N(false, false, null); return $xmlStr; } public function asCanonicalExclusive() { $xmlStr = $this->asDOMElement()->C14N(true, false, null); return $xmlStr; } public function injectNamespace(string $ns, string $uri, ?string $nsNode=null) { if ($nsNode===null) { $nsNode = $this; } if ($ns==='xmlns') { $nsNode->asDOMElement()->setAttributeNS('http://www.w3.org/2000/xmlns/', "$ns", $uri); return $this; } else { $nsNode->addAttribute("xmlns:xmlns:$ns", $uri); } $ret = $this->clone(); if ($ret===null) { return $this; } return $ret; } public function add($name, $value = NULL, $ns = NULL) { list($namespace, $tag) = explode(":", $name); if ($tag===null) { $tag = $namespace; $namespace = null; } return parent::addChild($name, $value, ($this->getDefinedNamespaces()[$namespace]?:$ns)); } public function addWithoutNS($name, $value = NULL) { $newElm = new \DOMElement($name, $value); $this->asDOMElement()->appendChild($newElm); return simplexml_import_dom($newElm, get_class($this)); } public function xpath($expression) :array|null|false { foreach ($this->getDefinedNamespaces() as $ns=>$uri) { if ($uri) { $this->registerXPathNamespace($ns, $uri); } } return parent::xpath($expression); } public function get($name) { return $this->getRaw($name); } public function getRaw($name) { list($namespace, $tag) = explode(":", $name); if ($tag===null) { $tag = $namespace; $namespace = null; } $ret = $this->children($namespace, true)->{$tag}; return $ret; } public function getSafe($name) { $ret = $this->getRaw($name); if ($ret->count()==0) { $ret = new \ModulesAbstract\ElectronicInvoice\SimpleXmlElementExtended('<?xml version="1.0" encoding="UTF-8"?><empty/>'); } return $ret; } public function asXML($filename = null) : string|bool { $xmlStr = parent::asXML(); $possibleHeaderReplacements = [ '<?xml version="1.0"?>' => '<?xml version="1.0" encoding="UTF-8"?>', ]; foreach ($possibleHeaderReplacements as $search=>$replacement) { if (strpos($xmlStr, $search)===0) { $xmlStr = $replacement. substr($xmlStr, strlen($search)); } } return $xmlStr; } public function asXMLAdvanced($inlineHeader=null, $showHeader=null) : string { $xmlStr = parent::asXML(); $possibleHeaderReplacements = []; if ($inlineHeader===true) { $possibleHeaderReplacements["<?xml version=\"1.0\"?>\r\n"] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; $possibleHeaderReplacements["<?xml version=\"1.0\"?>\n"] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; $possibleHeaderReplacements["<?xml version=\"1.0\"?>\r"] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; } if ($inlineHeader===false) { $possibleHeaderReplacements["<?xml version=\"1.0\"?>\r\n"] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; $possibleHeaderReplacements["<?xml version=\"1.0\"?>\n"] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; $possibleHeaderReplacements["<?xml version=\"1.0\"?>\r"] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; } $possibleHeaderReplacements['<?xml version="1.0"?>'] = '<?xml version="1.0" encoding="UTF-8"?>'; $headerReplaced = false; foreach ($possibleHeaderReplacements as $search=>$replacement) { if (strpos($xmlStr, $search)===0) { $xmlStr = $replacement. substr($xmlStr, strlen($search)); $headerReplaced = true; break; } } if ($showHeader===true && !$headerReplaced) { $xmlStr = '<?xml version="1.0" encoding="UTF-8"?>'.$xmlStr; } if ($showHeader===false && $headerReplaced) { $xmlStr = substr($xmlStr, strlen('<?xml version="1.0" encoding="UTF-8"?>')); } return $xmlStr; } } 