Files
DomFramework/outputtxt.php
2014-05-13 12:00:47 +00:00

124 lines
3.3 KiB
PHP

<?php
/** DomFramework
@package domframework
@author Dominique Fournier <dominique@fournier38.fr> */
require_once ("output.php");
/** Display in Text the datas provided */
class outputtxt extends output
{
/** Display an array on table format (TRUE) or tree format (FALSE) */
public $table = TRUE;
/** Display in Text the datas 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
// 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 datas
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";
}
}