update var_export to currect CVS

This commit is contained in:
Michal Čihař
2006-07-19 12:34:26 +00:00
parent 3b751df778
commit 126dd8dd3b
2 changed files with 112 additions and 100 deletions

View File

@@ -6,6 +6,9 @@ $Id$
$Source$
2006-07-19 Michal Čihař <michal@cihar.com>
* libraries/compat/var_export.php: Update to fixed version.
2006-07-19 Sebastian Mendel <cybot_tm@users.sourceforge.net>
* lang/german: updated, typos

View File

@@ -15,7 +15,7 @@
// | Authors: Aidan Lister <aidan@php.net> |
// +----------------------------------------------------------------------+
//
// $Id: var_export.php,v 1.15 2005/12/05 14:24:27 aidan Exp $
// $Id: var_export.php,v 1.18 2006/05/22 00:09:42 arpad Exp $
/**
@@ -25,112 +25,121 @@
* @package PHP_Compat
* @link http://php.net/function.var_export
* @author Aidan Lister <aidan@php.net>
* @version $Revision: 1.15 $
* @version $Revision: 1.18 $
* @since PHP 4.2.0
* @require PHP 4.0.0 (user_error)
*/
if (!function_exists('var_export')) {
function var_export($var, $return = false, $level = 0)
{
// Init
$indent = ' ';
$doublearrow = ' => ';
$lineend = ",\n";
$stringdelim = '\'';
$newline = "\n";
$find = array(null, '\\', '\'');
$replace = array('NULL', '\\\\', '\\\'');
$out = '';
function php_compat_var_export($var, $return = false, $level = 0, $inObject = false)
{
// Init
$indent = ' ';
$doublearrow = ' => ';
$lineend = ",\n";
$stringdelim = '\'';
$newline = "\n";
$find = array(null, '\\', '\'');
$replace = array('NULL', '\\\\', '\\\'');
$out = '';
// Indent
$level++;
for ($i = 1, $previndent = ''; $i < $level; $i++) {
$previndent .= $indent;
}
$varType = gettype($var);
// Handle object indentation oddity
if ($inObject && $varType != 'object') {
$previndent = substr($previndent, 0, -1);
}
// Handle each type
switch ($varType) {
// Array
case 'array':
if ($inObject) {
$out .= $newline . $previndent;
}
$out .= 'array (' . $newline;
foreach ($var as $key => $value) {
// Key
if (is_string($key)) {
// Make key safe
$key = str_replace($find, $replace, $key);
$key = $stringdelim . $key . $stringdelim;
}
// Value
if (is_array($value)) {
$export = php_compat_var_export($value, true, $level);
$value = $newline . $previndent . $indent . $export;
} else {
$value = php_compat_var_export($value, true, $level);
}
// Piece line together
$out .= $previndent . $indent . $key . $doublearrow . $value . $lineend;
}
// End string
$out .= $previndent . ')';
break;
// String
case 'string':
// Make the string safe
for ($i = 0, $c = count($find); $i < $c; $i++) {
$var = str_replace($find[$i], $replace[$i], $var);
}
$out = $stringdelim . $var . $stringdelim;
break;
// Number
case 'integer':
case 'double':
$out = (string) $var;
break;
// Indent
$level++;
for ($i = 1, $previndent = ''; $i < $level; $i++) {
$previndent .= $indent;
}
// Boolean
case 'boolean':
$out = $var ? 'true' : 'false';
break;
// Handle each type
switch (gettype($var)) {
// Array
case 'array':
$out = 'array (' . $newline;
foreach ($var as $key => $value) {
// Key
if (is_string($key)) {
// Make key safe
for ($i = 0, $c = count($find); $i < $c; $i++) {
$var = str_replace($find[$i], $replace[$i], $var);
}
$key = $stringdelim . $key . $stringdelim;
}
// Value
if (is_array($value)) {
$export = var_export($value, true, $level);
$value = $newline . $previndent . $indent . $export;
} else {
$value = var_export($value, true, $level);
}
// NULLs
case 'NULL':
case 'resource':
$out = 'NULL';
break;
// Piece line together
$out .= $previndent . $indent . $key . $doublearrow . $value . $lineend;
}
// Objects
case 'object':
// Start the object export
$out = $newline . $previndent;
$out .= get_class($var) . '::__set_state(array(' . $newline;
// Export the object vars
foreach(get_object_vars($var) as $key => $value) {
$out .= $previndent . $indent . ' ' . $stringdelim . $key . $stringdelim . $doublearrow;
$out .= php_compat_var_export($value, true, $level, true) . $lineend;
}
$out .= $previndent . '))';
break;
}
// End string
$out .= $previndent . ')';
break;
// String
case 'string':
// Make the string safe
for ($i = 0, $c = count($find); $i < $c; $i++) {
$var = str_replace($find[$i], $replace[$i], $var);
}
$out = $stringdelim . $var . $stringdelim;
break;
// Number
case 'integer':
case 'double':
$out = (string) $var;
break;
// Boolean
case 'boolean':
$out = $var ? 'true' : 'false';
break;
// NULLs
case 'NULL':
case 'resource':
$out = 'NULL';
break;
// Objects
case 'object':
// Start the object export
$out = $newline . $previndent . 'class ' . get_class($var) . ' {' . $newline;
// Export the object vars
foreach (get_object_vars($var) as $key => $val) {
$out .= $previndent . ' var $' . $key . ' = ';
if (is_array($val)) {
$export = var_export($val, true, $level);
$out .= $newline . $previndent . $indent . $export . ';' . $newline;
} else {
$out .= var_export($val, true, $level) . ';' . $newline;
}
}
$out .= $previndent . '}';
break;
}
// Method of output
if ($return === true) {
return $out;
} else {
echo $out;
}
// Method of output
if ($return === true) {
return $out;
} else {
echo $out;
}
}
?>
// Define
if (!function_exists('var_export')) {
function var_export($var, $return = false)
{
return php_compat_var_export($var, $return);
}
}