* @license BSD */ //namespace Domframework; require_once ("domframework/output.php"); /** Display in Text the data provided */ class outputtxt extends output { /** Display an array on table format (TRUE) or tree format (FALSE) */ public $table = TRUE; /** Display in Text the data provided @param mixed $data The data to be displayed */ public function out ($data) { @header("Cache-Control: no-store, no-cache, must-revalidate"); @header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); @header('Last-Modified: '.gmdate ('D, d M Y H:i:s').' GMT'); @header('Cache-Control: post-check=0, pre-check=0', false); @header('Pragma: no-cache'); @header ("Content-Type: text/plain"); if ($data === TRUE) echo "OK\n"; elseif ($data === FALSE) echo "FALSE\n"; elseif (is_string ($data)) echo "$data\n"; elseif (is_integer ($data)) echo "$data\n"; elseif (is_array ($data)) { if ($this->table === true) { // TABLE FORMAT // Look at sizes of each field $sizes = array (); foreach ($data as $line) { if (! is_array ($line)) { // 1 dimension array : force in tree format $this->table = false; break; } foreach ($line as $key=>$val) { if (is_array ($val)) { // 3 dimensions array : force in tree format $this->table = false; break 2; } if (! isset ($sizes[$key])) $sizes[$key] = strlen ($val); else $sizes[$key] = max ($sizes[$key], strlen ($val)); } } if ($this->table === true) { // Display the table if it is not a tree format // Check if the titles are longer than the data foreach (reset ($data) as $key=>$val) { if (strlen ($key) > $sizes[$key]) $sizes[$key] = strlen ($key); } // Prepare the border $line = reset ($data); $border = ""; $border .= "+"; $col = 0; foreach ($line as $key=>$val) { if ($col > 0) $border .= "+"; $border .= str_repeat ("-", 2+$sizes[$key]); $col ++; } $border .= "+\n"; echo "$border"; echo "| "; // Display titles $col = 0; foreach ($line as $key=>$val) { if ($col > 0) echo " | "; $start = round (($sizes[$key]-strlen ($key))/2); echo str_repeat (" ", $start); echo $key; echo str_repeat (" ", $sizes[$key]-$start-strlen ($key)); $col ++; } echo " |\n"; echo "$border"; // Display data foreach ($data as $line) { echo "| "; $col = 0; foreach ($line as $key=>$val) { if ($col > 0) echo " | "; echo sprintf ("%".$sizes[$key]."s", $val); $col ++; } echo " |\n"; } echo "$border"; } } if ($this->table === false) { // TREE FORMAT echo substr (print_r ($data, TRUE), 8, -3)."\n"; } } elseif ($data === NULL) echo "NULL\n"; else echo "TODO : ".gettype ($rc)." on ".__FILE__.":".__LINE__."\n"; if (!defined ("PHPUNIT")) exit; } }