PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Services_Trackback/pear/php/Var_Dump/Renderer/Table.php

http://nothing-at-all.googlecode.com/
PHP | 247 lines | 152 code | 10 blank | 85 comment | 15 complexity | 1069360a72412f86ae3874df132d7ac9 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available through the world-wide-web at the following url: |
  11. // | http://www.php.net/license/3_0.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Frederic Poeydomenge <fpoeydomenge at free dot fr> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id$
  20. require_once 'Var_Dump/Renderer/Common.php';
  21. /**
  22. * A concrete renderer for Var_Dump
  23. *
  24. * Returns a table-based representation of a variable in HTML
  25. *
  26. * @package Var_Dump
  27. * @category PHP
  28. * @author Frederic Poeydomenge <fpoeydomenge at free dot fr>
  29. */
  30. class Var_Dump_Renderer_Table extends Var_Dump_Renderer_Common
  31. {
  32. /**
  33. * Default configuration options.
  34. *
  35. * Valid configuration options are :
  36. * show_caption : bool, Show the caption or not
  37. * show_eol : string, String to insert before a newline, or false
  38. * before_num_key : string, Text to insert before a numerical key
  39. * after_num_key : string, Text to insert after a numerical key
  40. * before_str_key : string, Text to insert before a string key
  41. * after_str_key : string, Text to insert after a string key
  42. * before_type : string, Text to insert before a type
  43. * after_type : string, Text to insert after a type
  44. * before_value : string, Text to insert before a value
  45. * after_value : string, Text to insert after a value
  46. * start_table : string, Text to insert before the table
  47. * end_table : string, Text to insert after the table
  48. * start_tr : string, Text to insert before a row
  49. * end_tr : string, Text to insert after a row
  50. * start_tr_alt : string, Text to insert after an alternate row
  51. * end_tr_alt : string, Text to insert after an alternate row
  52. * start_td_key : string, Text to insert before the key cell
  53. * end_td_key : string, Text to insert after the key cell
  54. * start_td_type : string, Text to insert before the type cell
  55. * end_td_type : string, Text to insert after the type cell
  56. * start_td_value : string, Text to insert before the value cell
  57. * end_td_value : string, Text to insert after the value cell
  58. * start_td_colspan : string, Text to insert before a group cell
  59. * end_td_colspan : string, Text to insert after a group cell
  60. * start_caption : string, Text to insert before the caption
  61. * end_caption : string, Text to insert after the caption
  62. *
  63. * @var array
  64. * @access public
  65. */
  66. var $defaultOptions = array(
  67. 'show_caption' => TRUE,
  68. 'show_eol' => FALSE,
  69. 'before_num_key' => '',
  70. 'after_num_key' => '',
  71. 'before_str_key' => '',
  72. 'after_str_key' => '',
  73. 'before_type' => '',
  74. 'after_type' => '',
  75. 'before_value' => '',
  76. 'after_value' => '',
  77. 'start_table' => '<table>',
  78. 'end_table' => '</table>',
  79. 'start_tr' => '<tr>',
  80. 'end_tr' => '</tr>',
  81. 'start_tr_alt' => '<tr>',
  82. 'end_tr_alt' => '</tr>',
  83. 'start_td_key' => '<td>',
  84. 'end_td_key' => '</td>',
  85. 'start_td_type' => '<td>',
  86. 'end_td_type' => '</td>',
  87. 'start_td_value' => '<td>',
  88. 'end_td_value' => '</td>',
  89. 'start_td_colspan' => '<td colspan="2">',
  90. 'end_td_colspan' => '</td>',
  91. 'start_caption' => '<caption>',
  92. 'end_caption' => '</caption>'
  93. );
  94. /**
  95. * Class constructor.
  96. *
  97. * @param array $options Parameters for the rendering.
  98. * @access public
  99. */
  100. function Var_Dump_Renderer_Table($options = array())
  101. {
  102. $this->setOptions($options);
  103. }
  104. /**
  105. * Returns the string representation of a variable
  106. *
  107. * @return string The string representation of the variable.
  108. * @access public
  109. */
  110. function toString()
  111. {
  112. if (count($this->family) == 1) {
  113. return $this->_toString_Single();
  114. } else {
  115. return $this->_toString_Array();
  116. }
  117. }
  118. /**
  119. * Returns the string representation of a single variable
  120. *
  121. * @return string The string representation of a single variable.
  122. * @access private
  123. */
  124. function _toString_Single()
  125. {
  126. $string = htmlspecialchars($this->value[0]);
  127. if ($this->options['show_eol'] !== FALSE) {
  128. $string = str_replace(
  129. "\n",
  130. $this->options['show_eol'] . "\n",
  131. $string
  132. );
  133. }
  134. return
  135. $this->options['start_table'] .
  136. $this->options['start_tr'] .
  137. $this->options['start_td_type'] .
  138. $this->options['before_type'] .
  139. htmlspecialchars($this->type[0]) .
  140. $this->options['after_type'] .
  141. $this->options['end_td_type'] .
  142. $this->options['start_td_value'] .
  143. $this->options['before_value'] .
  144. nl2br($string) .
  145. $this->options['after_value'] .
  146. $this->options['end_td_value'] .
  147. $this->options['end_tr'] .
  148. $this->options['end_table'];
  149. }
  150. /**
  151. * Returns the string representation of a multiple variable
  152. *
  153. * @return string The string representation of a multiple variable.
  154. * @access private
  155. */
  156. function _toString_Array()
  157. {
  158. $txt = '';
  159. $stack = array(0);
  160. $counter = count($this->family);
  161. for ($c = 0 ; $c < $counter ; $c++) {
  162. switch ($this->family[$c]) {
  163. case VAR_DUMP_START_GROUP :
  164. array_push($stack, 0);
  165. if ($this->depth[$c] > 0) {
  166. $txt .= $this->options['start_td_colspan'];
  167. }
  168. $txt .= $this->options['start_table'];
  169. if ($this->options['show_caption']) {
  170. $txt .=
  171. $this->options['start_caption'] .
  172. htmlspecialchars($this->value[$c]) .
  173. $this->options['end_caption'];
  174. }
  175. break;
  176. case VAR_DUMP_FINISH_GROUP :
  177. array_pop($stack);
  178. $txt .= $this->options['end_table'];
  179. if ($this->depth[$c] > 0) {
  180. $txt .=
  181. $this->options['end_td_colspan'] .
  182. $this->options['end_tr'];
  183. }
  184. break;
  185. case VAR_DUMP_START_ELEMENT_NUM :
  186. case VAR_DUMP_START_ELEMENT_STR :
  187. array_push($stack, 1 - array_pop($stack));
  188. $tr = (end($stack) == 1) ? 'start_tr' : 'start_tr_alt';
  189. $comp = ($this->family[$c] == VAR_DUMP_START_ELEMENT_NUM) ? 'num' : 'str';
  190. $txt .=
  191. $this->options[$tr] .
  192. $this->options['start_td_key'] .
  193. $this->options['before_'.$comp.'_key'] .
  194. htmlspecialchars($this->value[$c]) .
  195. $this->options['after_'.$comp.'_key'] .
  196. $this->options['end_td_key'];
  197. break;
  198. case VAR_DUMP_FINISH_ELEMENT :
  199. case VAR_DUMP_FINISH_STRING :
  200. $etr = (end($stack) == 1) ? 'end_tr' : 'end_tr_alt';
  201. if (!is_null($this->value[$c])) {
  202. $string = htmlspecialchars($this->value[$c]);
  203. if ($this->options['show_eol'] !== FALSE) {
  204. $string = str_replace(
  205. "\n",
  206. $this->options['show_eol'] . "\n",
  207. $string
  208. );
  209. }
  210. $txt .=
  211. $this->options['start_td_type'] .
  212. $this->options['before_type'] .
  213. htmlspecialchars($this->type[$c]) .
  214. $this->options['after_type'] .
  215. $this->options['end_td_type'] .
  216. $this->options['start_td_value'] .
  217. $this->options['before_value'] .
  218. nl2br($string) .
  219. $this->options['after_value'] .
  220. $this->options['end_td_value'] .
  221. $this->options[$etr];
  222. } else {
  223. $txt .=
  224. $this->options['start_td_colspan'] .
  225. $this->options['before_type'] .
  226. htmlspecialchars($this->type[$c]) .
  227. $this->options['after_type'] .
  228. $this->options['end_td_colspan'] .
  229. $this->options[$etr];
  230. }
  231. break;
  232. }
  233. }
  234. return $txt;
  235. }
  236. }
  237. ?>