Migrated conversion utilities to the Convert class.

This commit is contained in:
Josh Sherman 2010-11-25 00:15:25 -05:00
parent dce116fa54
commit aed0942654
5 changed files with 156 additions and 136 deletions

153
classes/Convert.php Normal file
View file

@ -0,0 +1,153 @@
<?php
/**
* Converter
*
* PHP version 5
*
* Licensed under The MIT License
* Redistribution of these files must retain the above copyright notice.
*
* @author Josh Sherman <josh@gravityblvd.com>
* @copyright Copyright 2007-2010, Gravity Boulevard, LLC
* @license http://www.opensource.org/licenses/mit-license.html
* @package PICKLES
* @link http://p.ickl.es
*/
/**
* Convert Class
*
* Collection of statically called methods to help aid in converting formats.
*/
class Convert
{
/**
* To JSON
*
* Encodes passed variable as JSON.
*
* Requires PHP 5 >= 5.2.0 or PECL json >= 1.2.0
* Note: PECL json 1.2.1 is included /vendors
*
* @link http://json.org/
* @link http://us.php.net/json_encode
* @link http://pecl.php.net/package/json
*
* @static
* @param mixed $variable variable to convert
* @return JSON encoded string
*/
public static function toJSON($variable)
{
if (JSON_AVAILABLE)
{
return json_encode($variable);
}
else
{
return '{ "status": "error", "message": "json_encode() not found" }';
}
}
/**
* 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 =
* array('children' => array(
* 'child' => array(
* array('name' => 'Wendy Darling'),
* array('name' => 'John Darling'),
* array('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)
{
if ($level == 0)
{
$xml = '<' . key($array) . '>' . ($format ? "\n" : '') . self::arrayToXML(current($array), $format, $level + 1) . '</' . key($array) . '>' . ($format ? "\n" : '');
}
else
{
$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 : array($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;
}
}
?>

View file

@ -185,33 +185,6 @@ abstract class Display_Common extends Object
{
return true;
}
/**
* JSON Encode
*
* Encodes module return data as JSON.
*
* Requires PHP 5 >= 5.2.0 or PECL json >= 1.2.0
* Note: PECL json 1.2.1 is included /vendors
*
* @link http://json.org/
* @link http://us.php.net/json_encode
* @link http://pecl.php.net/package/json
*
* @return JSON encoded string
* @todo This really should be moved into some sort of converter class. I couldn't settle on a name, so it will live here for now.
*/
public function jsonEncode()
{
if (JSON_AVAILABLE)
{
echo json_encode($this->module_return);
}
else
{
echo '{ "status": "error", "message": "json_encode() not found" }';
}
}
/**
* Rendering Method

View file

@ -19,13 +19,6 @@
* JSON Display
*
* Displays data in JavaScript Object Notation.
*
* Requires PHP 5 >= 5.2.0 or PECL json >= 1.2.0
* Note: PECL json 1.2.1 is included /vendors
*
* @link http://json.org/
* @link http://us.php.net/json_encode
* @link http://pecl.php.net/package/json
*/
class Display_JSON extends Display_Common
{
@ -34,7 +27,7 @@ class Display_JSON extends Display_Common
*/
public function render()
{
return $this->jsonEncode();
echo Convert::toJSON($this->module_return);
}
}

View file

@ -108,7 +108,7 @@ class Display_PHP extends Display_Common
}
else
{
$this->jsonEncode();
echo Convert::toJSON($this->module_return);
}
}
}

View file

@ -34,106 +34,7 @@ class Display_XML extends Display_Common
*/
public function render()
{
echo $this->arrayToXML($this->module_return);
}
/**
* 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 =
* array('children' => array(
* 'child' => array(
* array('name' => 'Wendy Darling'),
* array('name' => 'John Darling'),
* array('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>
*
* @access private
* @param array $array array to convert into XML
* @return string generated XML
*/
private function arrayToXML($array, $format = false, $level = 0)
{
if ($level == 0)
{
$xml = '<' . key($array) . '>' . ($format ? "\n" : '') . $this->arrayToXML(current($array), $format, $level + 1) . '</' . key($array) . '>' . ($format ? "\n" : '');
}
else
{
$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 : array($node2 => $value2));
$xml .= ($format ? str_repeat("\t", $level) : '');
$xml .= '<' . $node . '>' . ($format ? "\n" : '');
$xml .= $this->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;
echo Convert::arrayToXML($this->module_return);
}
}