Skip to content

PHP-CS-Fixer #17

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

Open
wants to merge 2 commits into
base: master
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.php_cs.dist
.idea
/vendor
/composer.lock
93 changes: 60 additions & 33 deletions LSS/Array2XML.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*/
namespace LSS;

use \DomDocument;
use \Exception;
use DOMDocument;
use Exception;

/**
* Array2XML: A class to convert array in PHP to XML
Expand Down Expand Up @@ -56,33 +56,44 @@
* $xml = Array2XML::createXML('root_node_name', $php_array);
* echo $xml->saveXML();
*/
class Array2XML {

class Array2XML
{
/**
* @var DOMDocument
* @var DOMDocument|null
*/
private static $xml = null;

/**
* @var string
*/
private static $encoding = 'UTF-8';

/**
* Initialize the root XML node [optional]
* @param $version
* @param $encoding
* @param $format_output
*
* @param string $version
* @param string $encoding
* @param bool $format_output
*
* @return void
*/
public static function init($version = '1.0', $encoding = 'UTF-8', $format_output = true) {
public static function init($version = '1.0', $encoding = 'UTF-8', $format_output = true)
{
self::$xml = new DomDocument($version, $encoding);
self::$xml->formatOutput = $format_output;
self::$encoding = $encoding;
}

/**
* Convert an Array to XML
*
* @param string $node_name - name of the root node to be converted
* @param array $arr - aray to be converterd
* @param array $arr - array to be converted
*
* @return DomDocument
*/
public static function &createXML($node_name, $arr = array()) {
public static function &createXML($node_name, $arr = array())
{
$xml = self::getXMLRoot();
$xml->appendChild(self::convert($node_name, $arr));

Expand All @@ -94,21 +105,22 @@ public static function &createXML($node_name, $arr = array()) {
* Convert an Array to XML.
*
* @param string $node_name
* Name of the root node to be converted.
* @param array $arr
* Array to be converted.
* Name of the root node to be converted
* @param array $arr
* Array to be converted
*
* @throws \Exception
*
* @return \DOMNode
*/
private static function &convert($node_name, $arr = array()) {
private static function &convert($node_name, $arr = array())
{

//print_arr($node_name);
$xml = self::getXMLRoot();
$node = $xml->createElement($node_name);

if (is_array($arr)) {
if (\is_array($arr)) {
// get the attributes first.;
if (isset($arr['@attributes'])) {
foreach ($arr['@attributes'] as $key => $value) {
Expand All @@ -127,33 +139,34 @@ private static function &convert($node_name, $arr = array()) {
unset($arr['@value']); //remove the key from the array once done.
//return from recursion, as a note with value cannot have child nodes.
return $node;
} else if (isset($arr['@cdata'])) {
}
if (isset($arr['@cdata'])) {
$node->appendChild($xml->createCDATASection(self::bool2str($arr['@cdata'])));
unset($arr['@cdata']); //remove the key from the array once done.
//return from recursion, as a note with cdata cannot have child nodes.
return $node;
}
else if (isset($arr['@comment']) && is_string($arr['@comment'])) {
if (isset($arr['@comment']) && \is_string($arr['@comment'])) {
$node->appendChild($xml->createComment(self::bool2str($arr['@comment'])));
unset($arr['@comment']);
}
else if (isset($arr['@xml'])) {
} elseif (isset($arr['@xml'])) {
$fragment = $xml->createDocumentFragment();
$fragment->appendXML($arr['@xml']);
$node->appendChild($fragment);
unset($arr['@xml']);

return $node;
}
}

//create subnodes using recursion
if (is_array($arr)) {
if (\is_array($arr)) {
// recurse to get the node for that key
foreach ($arr as $key => $value) {
if (!self::isValidTagName($key)) {
throw new Exception('[Array2XML] Illegal character in tag name. tag: ' . $key . ' in node: ' . $node_name);
}
if (is_array($value) && is_numeric(key($value))) {
if (\is_array($value) && \is_numeric(\key($value))) {
// MORE THAN ONE NODE OF ITS KIND;
// if the new array is numeric index, means it is array of nodes of the same kind
// it should follow the parent key name
Expand All @@ -170,40 +183,54 @@ private static function &convert($node_name, $arr = array()) {

// after we are done with all the keys in the array (if it is one)
// we check if it has any text value, if yes, append it.
if (!is_array($arr)) {
if (!\is_array($arr)) {
$node->appendChild($xml->createTextNode(self::bool2str($arr)));
}

return $node;
}

/*
/**
* Get the root XML node, if there isn't one, create it.
*
* @return DOMDocument
*/
private static function getXMLRoot() {
private static function getXMLRoot()
{
if (empty(self::$xml)) {
self::init();
}

return self::$xml;
}

/*
/**
* Get string representation of boolean value
*
* @param bool|mixed $v
*
* @return mixed|string
*/
private static function bool2str($v) {
private static function bool2str($v)
{
//convert boolean to text value.
$v = $v === true ? 'true' : $v;
$v = $v === false ? 'false' : $v;
return $v;

return $v === false ? 'false' : $v;
}

/*
/**
* Check if the tag name or attribute name contains illegal characters
* Ref: http://www.w3.org/TR/xml/#sec-common-syn
*
* @param string $tag
*
* @return bool
*/
private static function isValidTagName($tag) {
private static function isValidTagName($tag)
{
$pattern = '/^[a-z_]+[a-z0-9\:\-\.\_]*[^:]*$/i';
return preg_match($pattern, $tag, $matches) && $matches[0] == $tag;

return \preg_match($pattern, $tag, $matches) && $matches[0] === $tag;
}
}

Loading