Skip to content

Commit

Permalink
XML Serializer : Modified order of member functions for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris committed Mar 15, 2018
1 parent 83bd007 commit b6f0860
Showing 1 changed file with 72 additions and 72 deletions.
144 changes: 72 additions & 72 deletions src/Data/Serialization/XmlSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,78 @@ private static function getRootNode($data) {
return $root_note;
}

/**
* @param mixed $data
* @param string|null $parent_node
* @param string $attributes [optional]
*
* @return string
*/
private static function buildXml($data, $parent_node, $attributes = null) {
if (!is_array($data)) {
return self::wrapNode($parent_node, $data, $attributes);
}

$result = '';
$attributes = self::getAttributesString(array_get($data, '@attributes', []));
unset($data['@attributes']);

foreach ($data as $key => $value) {
if (is_numeric($key)) {
$result .= self::buildXml($value, null, $attributes);
} else {
$result .= self::buildXml($value, $key);
}
}

return self::wrapNode($parent_node, $result, $attributes);
}

/**
* Wrap $content in an xml node
* ie <node attrName="attrVal">content</node>
*
* @param string $name
* @param string $content
* @param string $attributes
*
* @return null|string
*/
private static function wrapNode($name, $content = null, $attributes = '') {
if (empty($name)) {
return $content;
}

$node = "<$name";
if ($attributes) {
$node .= ' ' . trim($attributes);
}

if (empty($content)) {
return $node . '/>';
}

return $node . '>' . $content . "</$name>";
}

/**
* @param array $attributes
*
* @return null|string
*/
private static function getAttributesString(array $attributes) {
if (empty($attributes)) {
return null;
}
$result = '';
foreach ($attributes as $k => $v) {
$result .= $k . '="' . $v . '" ';
}


return $result;
}

/**
* @param SimpleXMLElement $xml
*
Expand Down Expand Up @@ -143,76 +215,4 @@ private static function fixNullNodes(array $array) {

return $array;
}

/**
* @param mixed $data
* @param string|null $parent_node
* @param string $attributes [optional]
*
* @return string
*/
private static function buildXml($data, $parent_node, $attributes = null) {
if (!is_array($data)) {
return self::wrapNode($parent_node, $data, $attributes);
}

$result = '';
$attributes = self::getAttributesString(array_get($data, '@attributes', []));
unset($data['@attributes']);

foreach ($data as $key => $value) {
if (is_numeric($key)) {
$result .= self::buildXml($value, null, $attributes);
} else {
$result .= self::buildXml($value, $key);
}
}

return self::wrapNode($parent_node, $result, $attributes);
}

/**
* Wrap $content in an xml node
* ie <node attrName="attrVal">content</node>
*
* @param string $name
* @param string $content
* @param string $attributes
*
* @return null|string
*/
private static function wrapNode($name, $content = null, $attributes = '') {
if (empty($name)) {
return $content;
}

$node = "<$name";
if ($attributes) {
$node .= ' ' . trim($attributes);
}

if (empty($content)) {
return $node . '/>';
}

return $node . '>' . $content . "</$name>";
}

/**
* @param array $attributes
*
* @return null|string
*/
private static function getAttributesString(array $attributes) {
if (empty($attributes)) {
return null;
}
$result = '';
foreach ($attributes as $k => $v) {
$result .= $k . '="' . $v . '" ';
}


return $result;
}
}

0 comments on commit b6f0860

Please sign in to comment.