Commit 0b644800 authored by Radek Suski's avatar Radek Suski

(+) Support for attributes and custome node names in Arr class

parent eecacab0
......@@ -59,6 +59,7 @@ class Arr
* @param $array
* @param $inDel
* @param $outDel
*
* @return string
*/
public function toString( $array, $inDel = '=', $outDel = ' ' )
......@@ -67,10 +68,10 @@ class Arr
if ( is_array( $array ) && count( $array ) ) {
foreach ( $array as $key => $item ) {
if ( is_array( $item ) ) {
$out[ ] = $this->toString( $item, $inDel, $outDel );
$out[] = $this->toString( $item, $inDel, $outDel );
}
else {
$out[ ] = "{$key}{$inDel}\"{$item}\"";
$out[] = "{$key}{$inDel}\"{$item}\"";
}
}
}
......@@ -89,13 +90,14 @@ class Arr
* Check if given array ia array of integers
*
* @param array $arr
*
* @return bool
*/
public static function is_int( $arr )
{
if ( is_array( $arr ) && count( $arr ) ) {
foreach ( $arr as $i => $k ) {
if ( ( int )$k != $k ) {
if ( ( int ) $k != $k ) {
return false;
}
}
......@@ -105,6 +107,7 @@ class Arr
/**
* @param $arr
*
* @return string
*
*/
......@@ -115,17 +118,17 @@ class Arr
if ( is_array( $this->_arr ) && count( $this->_arr ) ) {
foreach ( $this->_arr as $key => $value ) {
if ( is_array( $value ) && !( is_string( $value ) ) ) {
$out[ ] = "[{$key}]";
$out[] = "[{$key}]";
if ( count( $value ) ) {
foreach ( $value as $k => $v ) {
$k = $this->_cleanIni( $k );
$out[ ] = "{$k} = \"{$v}\"";
$out[] = "{$k} = \"{$v}\"";
}
}
}
else {
$key = $this->_cleanIni( $key );
$out[ ] = "{$key} = \"{$value}\"";
$out[] = "{$key} = \"{$value}\"";
}
}
}
......@@ -134,6 +137,7 @@ class Arr
/**
* @param $txt
*
* @return mixed
*
*/
......@@ -146,6 +150,7 @@ class Arr
* @param array $arr
* @param string $root
* @param bool $returnDOM
*
* @return \DOMDocument|string
*
*/
......@@ -170,16 +175,39 @@ class Arr
protected function _toXML( array $arr, &$node, &$dom )
{
if ( is_array( $arr ) && count( $arr ) ) {
$attributes = null;
foreach ( $arr as $name => $value ) {
if ( is_numeric( $name ) ) {
$name = 'value';
if ( is_array( $value ) && isset( $value[ 'xml-tag' ] ) ) {
$name = $value[ 'xml-tag' ];
$attributes = isset( $value[ 'xml-attributes' ] ) ? $value[ 'xml-attributes' ] : null;
$value = $value[ 'xml-value' ];
}
else {
$name = 'value';
}
}
if ( is_array( $value ) ) {
$nn = $node->appendChild( $dom->createElement( $this->nid( $name ) ) );
if ( $attributes && is_array( $attributes ) && count( $attributes ) ) {
foreach ( $attributes as $attribute => $attributeValue ) {
$domAttribute = $dom->createAttribute( $attribute );
$domAttribute->value = $attributeValue;
$nn->appendChild( $domAttribute );
}
}
$this->_toXML( $value, $nn, $dom );
}
else {
$node->appendChild( $dom->createElement( $this->nid( $name ), preg_replace( '/&(?![#]?[a-z0-9]+;)/i', '&', $value ) ) );
$newElement = $dom->createElement( $this->nid( $name ), preg_replace( '/&(?![#]?[a-z0-9]+;)/i', '&', $value ) );
if ( $attributes && is_array( $attributes ) && count( $attributes ) ) {
foreach ( $attributes as $attribute => $attributeValue ) {
$domAttribute = $dom->createAttribute( $attribute );
$domAttribute->value = $attributeValue;
$newElement->appendChild( $domAttribute );
}
}
$node->appendChild( $newElement );
}
}
}
......@@ -187,17 +215,19 @@ class Arr
/**
* @param $txt
*
* @return string
*
*/
protected function nid( $txt )
{
return $this->legacy ? strtolower( StringUtils::Nid( $txt ) ) : StringUtils::Nid( $txt );
return $this->legacy ? strtolower( StringUtils::Nid( $txt ) ) : StringUtils::Nid( $txt );
}
/**
* @param \DOMDocument $dom
* @param $root
*
* @return array
*/
public function fromXML( $dom, $root )
......@@ -212,6 +242,7 @@ class Arr
* @param $arr
* @param string $root
* @param bool $returnDOM
*
* @internal param \DOMDocument $dom
* @return string
*/
......@@ -224,6 +255,7 @@ class Arr
/**
* @param \DOMNodeList $dom
* @param array $arr
*
* @return void
*/
protected function _fromXML( $dom, &$arr )
......
......@@ -30,7 +30,8 @@ Legend:
------------------
(+) Simple OpenSSL encryption added
(+) Option to replace characters with null in StringUtils::Nid
(+) Option to replace characters with null in StringUtils::Nid
(+) Support for attributes and custome node names in Arr class
(#) Count on non-arrays in PHP 7.2.x (Issue #103)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment