Source for file ArgumentRenderer.class.php

Documentation is available at ArgumentRenderer.class.php

  1. <?php
  2.  
  3. //require_once(HARMONI."utilities/ArgumentRenderer.interface.php");
  4.  
  5. /**
  6. * An ArgumentRenderer provides functionallity to print/render/format a list of arguments.
  7. * An ArgumentRenderer provides functionallity to print/render/format a list of arguments.
  8. *
  9. * @package harmoni.utilities
  10. *
  11. * @copyright Copyright &copy; 2005, Middlebury College
  12. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  13. *
  14. * @version $Id: ArgumentRenderer.class.php,v 1.6 2006/01/13 16:11:05 adamfranco Exp $
  15. */
  16. class ArgumentRenderer {
  17.  
  18. /**
  19. * Renders one argument.
  20. * Renders one argument by printing its type and value. In 'detailed' mode,
  21. * includes some additional information (i.e., for arrays, prints all elements
  22. * of the array; for objects, prints the object structure).
  23. * @param mixed $argument The argument to render.
  24. * @param boolean $isDetailed If TRUE, will print additional details.
  25. * @param boolean $shouldPrint If TRUE, will print on screen; If FALSE, will not
  26. * print, but just return the result as a string.
  27. * @param integer $trim If >0, will trim the argument to to the given length
  28. * @return string The output of the method. This will be output to the browser
  29. * if $shouldPrint is set to TRUE.
  30. * @access public
  31. ***/
  32. function renderOneArgument($argument, $isDetailed = false, $shouldPrint = false, $trim = 0) {
  33. $result = "Unknown";
  34. // NULL type
  35. if (is_null($argument)) {
  36. $result = "NULL";
  37. }
  38. // Boolean type
  39. elseif (is_bool($argument)) {
  40. $result = "Boolean: " . ($argument ? "true" : "false");
  41. }
  42. // String type
  43. elseif (is_string($argument)) {
  44. if ($trim > 0 && strlen($argument) > $trim)
  45. $result = "String: \"".substr($argument, 0, $trim)."\"...(trimmed)";
  46. else
  47. $result = "String: \"$argument\"";
  48. if ($isDetailed)
  49. $result .= " (length = ".strlen($argument).")";
  50. }
  51. // Integer type
  52. elseif (is_integer($argument)) {
  53. $result = "Integer: $argument";
  54. }
  55. // Float type
  56. elseif (is_float($argument)) {
  57. $result = "Float: $argument";
  58. }
  59. // Array type
  60. elseif (is_array($argument)) {
  61. $result = "Array: " . count($argument) . " elements";
  62. if ($isDetailed && count($argument) > 0) {
  63. $result .= " {\n";
  64. foreach ($argument as $key => $elt) {
  65. $result .= " [".$key."] => ";
  66. $result .= ArgumentRenderer::renderOneArgument($elt,false,false, $trim);
  67. $result .= "\n";
  68. }
  69. $result .= "}";
  70. }
  71. }
  72. // Resource type
  73. elseif (is_resource($argument)) {
  74. $result = "Resource: ".get_resource_type($argument);
  75. }
  76. // Object type
  77. elseif (is_object($argument)) {
  78. $result = "Object: " . get_class($argument);
  79. if ($isDetailed) {
  80. $memberVars = get_object_vars($argument);
  81. if (count($memberVars) > 0) {
  82. $result .= "\nMember variables: {\n";
  83. foreach (get_object_vars($argument) as $key => $elt) {
  84. $result .= " [".$key."] => ";
  85. $result .= ArgumentRenderer::renderOneArgument($elt,false,false, $trim);
  86. $result .= "\n";
  87. }
  88. $result .= "}";
  89. }
  90. }
  91. }
  92.  
  93. // print if necessary
  94. if ($shouldPrint)
  95. echo $result;
  96. return $result;
  97. }
  98.  
  99. /**
  100. * Renders many arguments.
  101. * Renders many arguments by printing their types and values and comma delimiting them.
  102. * In 'detailed' mode, includes some additional information (i.e., for arrays,
  103. * prints all elements of the array; for objects, prints the object structure).
  104. * @param array $arguments The arguments to render.
  105. * @param boolean $isDetailed If TRUE, will print additional details.
  106. * @param boolean $shouldPrint If TRUE, will print on screen; If FALSE, will not
  107. * @param integer $trim If >0, will trim the argument to to the given length
  108. * print, but just return the result as a string.
  109. * @return string The output of the method. This will be output to the browser
  110. * if $shouldPrint is set to TRUE. Returns FALSE, if something goes wrong.
  111. * @access public
  112. ***/
  113. function renderManyArguments($arguments, $isDetailed = false, $shouldPrint = false, $trim = 0) {
  114. // see if $arguments is an array
  115. if (!is_array($arguments))
  116. return false;
  117. // make sure $arguments is not empty
  118. if (count($arguments) == 0)
  119. return false;
  120. // render each element of $arguments
  121. $resultArray = array();
  122. foreach (array_keys($arguments) as $i => $key )
  123. $resultArray[] = ArgumentRenderer::renderOneArgument($arguments[$key],$isDetailed,false, $trim);
  124. $glue = ($isDetailed) ? ",\n" : ", ";
  125. $result = implode($glue, $resultArray);
  126. // print if necessary
  127. if ($shouldPrint)
  128. echo $result;
  129. return $result;
  130. }
  131. }
  132.  
  133. ?>

Documentation generated on Wed, 19 Sep 2007 10:21:30 -0400 by phpDocumentor 1.3.0RC3