PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/pear/PHP/Compat/Function/var_export.php

http://akelosframework.googlecode.com/
PHP | 136 lines | 75 code | 15 blank | 46 comment | 12 complexity | 83e8cfdcb3c47b25058376851adbad14 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP Version 4 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2004 The PHP Group |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 3.0 of the PHP license, |
  8. // | that is bundled with this package in the file LICENSE, and is |
  9. // | available at through the world-wide-web at |
  10. // | http://www.php.net/license/3_0.txt. |
  11. // | If you did not receive a copy of the PHP license and are unable to |
  12. // | obtain it through the world-wide-web, please send a note to |
  13. // | license@php.net so we can mail you a copy immediately. |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Aidan Lister <aidan@php.net> |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: var_export.php,v 1.15 2005/12/05 14:24:27 aidan Exp $
  19. /**
  20. * Replace var_export()
  21. *
  22. * @category PHP
  23. * @package PHP_Compat
  24. * @link http://php.net/function.var_export
  25. * @author Aidan Lister <aidan@php.net>
  26. * @version $Revision: 1.15 $
  27. * @since PHP 4.2.0
  28. * @require PHP 4.0.0 (user_error)
  29. */
  30. if (!function_exists('var_export')) {
  31. function var_export($var, $return = false, $level = 0)
  32. {
  33. // Init
  34. $indent = ' ';
  35. $doublearrow = ' => ';
  36. $lineend = ",\n";
  37. $stringdelim = '\'';
  38. $newline = "\n";
  39. $find = array(null, '\\', '\'');
  40. $replace = array('NULL', '\\\\', '\\\'');
  41. $out = '';
  42. // Indent
  43. $level++;
  44. for ($i = 1, $previndent = ''; $i < $level; $i++) {
  45. $previndent .= $indent;
  46. }
  47. // Handle each type
  48. switch (gettype($var)) {
  49. // Array
  50. case 'array':
  51. $out = 'array (' . $newline;
  52. foreach ($var as $key => $value) {
  53. // Key
  54. if (is_string($key)) {
  55. // Make key safe
  56. for ($i = 0, $c = count($find); $i < $c; $i++) {
  57. $var = str_replace($find[$i], $replace[$i], $var);
  58. }
  59. $key = $stringdelim . $key . $stringdelim;
  60. }
  61. // Value
  62. if (is_array($value)) {
  63. $export = var_export($value, true, $level);
  64. $value = $newline . $previndent . $indent . $export;
  65. } else {
  66. $value = var_export($value, true, $level);
  67. }
  68. // Piece line together
  69. $out .= $previndent . $indent . $key . $doublearrow . $value . $lineend;
  70. }
  71. // End string
  72. $out .= $previndent . ')';
  73. break;
  74. // String
  75. case 'string':
  76. // Make the string safe
  77. for ($i = 0, $c = count($find); $i < $c; $i++) {
  78. $var = str_replace($find[$i], $replace[$i], $var);
  79. }
  80. $out = $stringdelim . $var . $stringdelim;
  81. break;
  82. // Number
  83. case 'integer':
  84. case 'double':
  85. $out = (string) $var;
  86. break;
  87. // Boolean
  88. case 'boolean':
  89. $out = $var ? 'true' : 'false';
  90. break;
  91. // NULLs
  92. case 'NULL':
  93. case 'resource':
  94. $out = 'NULL';
  95. break;
  96. // Objects
  97. case 'object':
  98. // Start the object export
  99. $out = $newline . $previndent . 'class ' . get_class($var) . ' {' . $newline;
  100. // Export the object vars
  101. foreach (get_object_vars($var) as $key => $val) {
  102. $out .= $previndent . ' var $' . $key . ' = ';
  103. if (is_array($val)) {
  104. $export = var_export($val, true, $level);
  105. $out .= $newline . $previndent . $indent . $export . ';' . $newline;
  106. } else {
  107. $out .= var_export($val, true, $level) . ';' . $newline;
  108. }
  109. }
  110. $out .= $previndent . '}';
  111. break;
  112. }
  113. // Method of output
  114. if ($return === true) {
  115. return $out;
  116. } else {
  117. echo $out;
  118. }
  119. }
  120. }
  121. ?>