PortableInfobox/services/Helpers/SimpleXmlUtil.php

46 lines
990 B
PHP
Raw Normal View History

<?php
namespace Wikia\PortableInfobox\Helpers;
class SimpleXmlUtil {
private static $instance = null;
private function __construct() {
}
/**
* @return null|SimpleXmlUtil
*/
public static function getInstance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self;
}
2015-06-16 11:45:27 +00:00
return self::$instance;
}
/**
* Gets contents of SimpleXML node as XML string
* Will return empty string for a non-node argument
*
* @param $node
2015-06-16 11:45:27 +00:00
*
* @return string
*/
public function getInnerXML( $node ) {
$innerXML = '';
2015-06-16 11:45:27 +00:00
// check for empty nodes, strlen used for "0" strings match
if ( $node instanceof \SimpleXMLElement && ( strlen( (string)$node ) || $node->count() ) ) {
2015-05-19 13:31:56 +00:00
$domElement = dom_import_simplexml( $node );
if ( ( $domElement instanceof \DOMElement ) && ( $domElement->hasChildNodes() ) ) {
2015-05-19 13:31:56 +00:00
foreach ( $domElement->childNodes as $child ) {
$innerXML .= $child->ownerDocument->saveXML( $child );
}
}
}
2015-06-16 11:45:27 +00:00
return $innerXML;
}
}