Dropped Convert class
All it contained was an array to XML function. Moved off to joshtronic/php-array2xml
This commit is contained in:
parent
ce45dc0dbe
commit
ecb075b344
2 changed files with 0 additions and 148 deletions
121
src/Convert.php
121
src/Convert.php
|
@ -1,121 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Converter
|
|
||||||
*
|
|
||||||
* Licensed under The MIT License
|
|
||||||
* Redistribution of these files must retain the above copyright notice.
|
|
||||||
*
|
|
||||||
* @copyright Copyright 2007-2014, Josh Sherman
|
|
||||||
* @license http://www.opensource.org/licenses/mit-license.html
|
|
||||||
* @link https://github.com/joshtronic/pickles
|
|
||||||
* @package Pickles
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace Pickles;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert Class
|
|
||||||
*
|
|
||||||
* Collection of statically called methods to help aid in converting data
|
|
||||||
* formats.
|
|
||||||
*/
|
|
||||||
class Convert
|
|
||||||
{
|
|
||||||
// {{{ Array to XML
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Array to XML
|
|
||||||
*
|
|
||||||
* Converts an array into XML tags (recursive). This method expects the
|
|
||||||
* passed array to be formatted very specifically to accomodate the fact
|
|
||||||
* that an array's format isn't quite the same as well-formed XML.
|
|
||||||
*
|
|
||||||
* Input Array =
|
|
||||||
* ['children' => [
|
|
||||||
* 'child' => [
|
|
||||||
* ['name' => 'Wendy Darling'],
|
|
||||||
* ['name' => 'John Darling'],
|
|
||||||
* ['name' => 'Michael Darling'],
|
|
||||||
* ],
|
|
||||||
* ]]
|
|
||||||
*
|
|
||||||
* Output XML =
|
|
||||||
* <children>
|
|
||||||
* <child><name>Wendy Darling</name></child>
|
|
||||||
* <child><name>John Darling</name></child>
|
|
||||||
* <child><name>Michael Darling</name></child>
|
|
||||||
* </children>
|
|
||||||
*
|
|
||||||
* @static
|
|
||||||
* @param array $array array to convert into XML
|
|
||||||
* @return string generated XML
|
|
||||||
*/
|
|
||||||
public static function arrayToXML($array, $format = false, $level = 0)
|
|
||||||
{
|
|
||||||
$xml = '';
|
|
||||||
|
|
||||||
if (is_array($array))
|
|
||||||
{
|
|
||||||
foreach ($array as $node => $value)
|
|
||||||
{
|
|
||||||
// Checks if the value is an array
|
|
||||||
if (is_array($value))
|
|
||||||
{
|
|
||||||
foreach ($value as $node2 => $value2)
|
|
||||||
{
|
|
||||||
if (is_array($value2))
|
|
||||||
{
|
|
||||||
// Nest the value if the node is an integer
|
|
||||||
$new_value = (is_int($node2) ? $value2 : [$node2 => $value2]);
|
|
||||||
|
|
||||||
$xml .= ($format ? str_repeat("\t", $level) : '');
|
|
||||||
$xml .= '<' . $node . '>' . ($format ? "\n" : '');
|
|
||||||
$xml .= self::arrayToXML($new_value, $format, $level + 1);
|
|
||||||
$xml .= ($format ? str_repeat("\t", $level) : '');
|
|
||||||
$xml .= '</' . $node . '>' . ($format ? "\n" : '');
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (is_int($node2))
|
|
||||||
{
|
|
||||||
$node2 = $node;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Checks for special characters
|
|
||||||
if (htmlspecialchars($value2) != $value2)
|
|
||||||
{
|
|
||||||
$xml .= ($format ? str_repeat("\t", $level) : '');
|
|
||||||
$xml .= '<' . $node2 . '><![CDATA[' . $value2 . ']]></' . $node2 . '>' . ($format ? "\n" : '');
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$xml .= ($format ? str_repeat("\t", $level) : '');
|
|
||||||
$xml .= '<' . $node2 . '>' . $value2 . '</' . $node2 . '>' . ($format ? "\n" : '');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Checks for special characters
|
|
||||||
if (htmlspecialchars($value) != $value)
|
|
||||||
{
|
|
||||||
$xml .= ($format ? str_repeat("\t", $level) : '');
|
|
||||||
$xml .= '<' . $node . '><![CDATA[' . $value . ']]></' . $node . '>' . ($format ? "\n" : '');
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$xml .= ($format ? str_repeat("\t", $level) : '');
|
|
||||||
$xml .= '<' . $node . '>' . $value . '</' . $node . '>' . ($format ? "\n" : '');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $xml;
|
|
||||||
}
|
|
||||||
|
|
||||||
// }}}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,27 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
class ConvertTest extends PHPUnit_Framework_TestCase
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @dataProvider providerArrayToXML
|
|
||||||
*/
|
|
||||||
public function testArrayToXML($a, $b, $c)
|
|
||||||
{
|
|
||||||
$this->assertEquals(Pickles\Convert::arrayToXML($a, $b), $c);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function providerArrayToXML()
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
['foo', false, ''],
|
|
||||||
[['foo', 'bar'], false, '<0>foo</0><1>bar</1>'],
|
|
||||||
[['foo', 'bar'], true, "<0>foo</0>\n<1>bar</1>\n"],
|
|
||||||
[['foo' => 'bar'], false, '<foo>bar</foo>'],
|
|
||||||
[['foo' => 'b & r'], false, '<foo><![CDATA[b & r]]></foo>'],
|
|
||||||
[['children' => ['child' => ['foo', 'bar']]], false, '<children><child>foo</child><child>bar</child></children>'],
|
|
||||||
[['children' => ['child' => ['foo & bar']]], false, '<children><child><![CDATA[foo & bar]]></child></children>'],
|
|
||||||
[['children' => ['child' => ['foo', 'bar']]], true, "<children>\n\t<child>foo</child>\n\t<child>bar</child>\n</children>\n"],
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue