protected static $type;
public static function init() {
- $self = get_class();
self::$types = array(
'human' => [
'end' => 'out_human_end',
],
- 'sql' => array(),
+ 'sql' => array(), # Code still in db/table.php !
'sh' => array(
- 'row' => function(&$r) {
+ 'preff' => self::p('preff',""),
+ 'row' => function(&$o,&$r) {
$line = [];
foreach ($r as $k=>$v) {
if (!preg_match('/^[a-zA-Z\w_]+$/',$k)) continue;
$v = str_replace('"','\\"',$v);
- $line[] = "$k=\"$v\"";
+ $line[] = $o['preff']."$k=\"$v\"";
}
if ($line) echo join(' ',$line).";\n";
}
),
- #'php' => array( 'row' => function(&$r) {var_dump($r);},),
'php' => array(
- #'enclose' => array('$VAR=array(',');'),
- 'enclose' => array('$CONF = array('.NB_EOL,NB_EOL.');'),
+ 'enclose' => [
+ '<?php'.(NB_EOL ? NB_EOL:' ') . '$CONF = array('.NB_EOL
+ ,NB_EOL.');' . (NB_EOL ? NB_EOL:' ').'?'.'>'.NB_EOL
+ ],
'eol' => self::p('eol',NB_EOL),
'rec' => ',',
- #'row' => function(&$r) { echo preg_replace('/[\r\n]/','',var_dump($r,true));},
- #'row' => function(&$r) {print_r($r);}
- #'row' => function(&$r) {var_dump($r);}
- 'row' => function(&$r) {
+ 'row' => function(&$o,&$r) {
return var_export($r);
echo preg_replace(array(
'/(=>\s*)\n\s+/m',
"),".NB_EOL."array"
),var_export($r,true));
}
- #'row' => function(&$r) { echo preg_replace('/\n/','',var_export($r,true));}
- #'row' => function(&$r) {var_export($r);}
),
- #'var_dump' => array( 'row' => function(&$r) {var_dump($r);},),
- #'print_r' => array( 'row' => function(&$r) {print_r($r);},),
- #'var_export' => array( 'row' => function(&$r) {var_export($r);},),
+ 'php_var_dump' => array( 'row' => function(&$o,&$r) {var_dump($r);},),
+ 'php_print_r' => array( 'row' => function(&$o,&$r) {print_r($r);},),
+ 'php_var_export' => array( 'row' => function(&$o,&$r) {var_export($r);},),
'div' => array(
'is_html' => true,
'xml' => array(
'enclose' => array('<?xml version="1.0" encoding="utf-8"?'.'>'.NB_EOL."<rows>".NB_EOL,"</rows>".NB_EOL),
'eol' => self::p('eol',NB_EOL),
- 'row' => function (&$row,$o) {
+ 'row' => function (&$o,&$row) {
echo ($o['eol'] ? ' ' : '')."<row>".$o['eol'];
foreach ($row as $k => $v) {
echo ($o['eol'] ? ' ' : '')."<$k><![CDATA[".out::scalar($v)."]]></$k>".$o['eol'];
'txt' => array(
'col' => self::p('col',' : '),
'sep' => self::p('sep',"\n"),
- 'eol' => self::p('eol',">\n"),
+ 'eol' => self::p('eol',"\n"),
),
'csv' => array(
'sep' => self::p('sep',"\t"),
'eol' => self::p('eol',"\n"),
'enclose' => array("","\n"),
- 'head' => function(&$o,&$row) {
- if (!is_array($row)) echo 0;
- echo "\r";
- if (out::is_hash($row)) {
- $ar = array_keys($row);
- return out_csv($ar,$o);
- } else {
- return out_csv($row,$o);
- }
- },
-
- 'row' => 'out_csv',
),
'yaml' => [
'enclose' => ["---\n",""],
- 'row' => function (&$row,&$o) {
+ 'row' => function (&$o,&$row) {
$yaml = '- '.out::yaml_encode($row);
$yaml = preg_replace("/^(?!-)/m"," ",$yaml);
echo $yaml;
'json' => array(
'enclose' => array('['.NB_EOL,NB_EOL.']'),
- 'row' => function($row) { return json_encode($row); },
+ 'row' => function(&$o,&$row) { return json_encode($row); },
'eol' => self::p('eol',NB_EOL),
'rec' => ',',
),
return is_scalar($v) ? $v : json_encode($v);
}
- public static function type_call($k,&$o,&$row) {
+ public static function type_call($k,&$o,&$row=[]) {
- $type = self::type();
+ static $methods;
if (!isset($o[$k])) {
- # Try internal method
- $call = self::type() ? self::type()."_$k" : '';
- $call = str_replace('_row','',$call);
- #if ($call and in_array($call,get_class_methods(get_class()))) return call_user_func_array([get_class(),$call],$params);
- #if ($call and in_array($call,get_class_methods(get_class()))) debug($call);
- if ($call and in_array($call,get_class_methods(get_class()))) return self::$call($o,$row);
+ # Cache methods
+ if (!isset($methods))
+ foreach (get_class_methods(get_class()) as $m) $methods[$m]=1
+ ;
+
+ # Try internal function
+ $call = $k.'_'.self::type();
+ if (!$call or empty($methods[$call])) return false;
+ echo self::$call($o,$row);
- } elseif(is_callable($o[$k]) and is_callable($o[$k])) {
+ # External function
+ } elseif(is_callable($o[$k])) {
$call = $o[$k];
echo $call($o,$row);
- return '';
+ # String
+ } elseif(is_scalar($o[$k])) {
+ echo $o[$k];
+
+ } else {
+ return false;
}
- return '';
+ return true;
}
- public static function head(&$o,$head,$data=array()) {
+ public static function head(&$o,$head,$data=[]) {
$o['header'] = (self::p('header')==='0') ? false : true;
# Format $head
public static function row(&$o,&$row) {
- #self::type_call('row',$o,$row);
- # Defined function
- if (isset($o['row'])) {
-
- if (is_scalar($o['row']) and preg_match('/^out_/',$o['row'])) {
- $o['row']($row,$o);
- } else {
- echo $o['row']($row,$o);
- }
- return true;
- }
+ if (self::type_call('row',$o,$row)) return true;
# Assume html/xml tag style
if (isset($o['tag'])) {
- return out_tag($row,$o);
+ return out_tag($o,$row);
}
# txt
- return self::txt($row,$o);
+ return self::row_txt($o,$row);
return true;
}
}
public static function end(&$o) {
- if (isset($o['end'])) echo $o['end']($o);
+ #if (isset($o['end'])) echo $o['end']($o);
+ self::type_call('end',$o);
if (isset($o['enclose'])) echo $o['enclose'][1];
}
$head = self::head($conf,$head,$data);
foreach ($data as $row) {
- if ($count>0) self::concat($conf);
- $count++;
# Transform simple array into hash
if (isset($row[0]) and $head) {
}
+ if ($count>0) self::concat($conf);
+ $count++;
self::row($conf,$row);
- #echo " ($count)";
}
return $v;
}
- public static function txt(&$row,&$o) {
- $header = (out::p('header')==='0') ? false : true;
+ public static function row_txt(&$o,&$row) {
+ $header = (self::p('header')==='0') ? false : true;
+ if (!isset($o['col'])) $o['col'] = '';
foreach ($row as $k => $v) {
if (!$header) {
}
}
-} Out::init() ; # < Class
-/*--------------------------------------------------------------------------*/
-// Functions
-/*--------------------------------------------------------------------------*/
+ public static function head_csv(&$o,&$row) {
+ if (!is_array($row)) echo 0;
+ if (self::is_hash($row)) {
+ $ar = array_keys($row);
+ return self::row_csv($o,$ar);
+ } else {
+ return self::row_csv($o,$row);
+ }
+ }
-function out_csv(&$row,$o) {
- $values = array();
+ public static function row_csv(&$o,$row) {
+ $values = [];
- foreach (array_values($row) as $k=>$v) {
- $values[] = str_replace("\n",'\\n',out::scalar($v));
+ foreach (array_values($row) as $k=>$v) {
+ $values[] = str_replace("\n",'\\n',out::scalar($v));
+ }
+
+ if (!empty($o['return'])) return join($o['sep'],$values);
+ echo join($o['sep'],$values);
}
- if (!empty($o['return'])) return join($o['sep'],$values);
- echo join($o['sep'],$values);
-}
+} Out::init() ; # < Class
+/*--------------------------------------------------------------------------*/
+// Functions
+/*--------------------------------------------------------------------------*/
//
// Tag
# Avoid: <th class="0">name</th>
if (!out::is_hash($row)) $row = array_combine(array_values($row),array_values($row));
- out_tag($row,$o);
+ out_tag($o,$row);
}
-function out_tag(&$row,&$o) {
+function out_tag(&$o,&$row) {
if (isset($o['tag_enclose'])) echo (NB_EOL ? ' ' : '').'<'.$o['tag_enclose'].'>'.NB_EOL;
foreach ($row as $k => $v) {
//
// Human
//
-function out_human_head(&$o,&$head) {
+function out_human_head(&$o,$head) {
#$line = "a\tb\tc\t\t\tg"; bye(explode("\t",$line));
if (out::p('header')==='0') return '';
'head' => 1,
];
if (out::is_hash($head)) $head = array_keys($head);
- return out_human($head,$o);
+ return out_human($o,$head);
}
-function out_human(&$row,&$o) {
+function out_human(&$o,&$row) {
- # switch options
- if (!isset($o['mark.human'])) {
- if (empty($o['_human'])) $o['_human'] = [];
- $o['mark.human'] = 1;
+ # Init options
+ if (empty($o['_human'])) $o['_human'] = [];
+ if (!isset($o['_human']['len'])) {
+ $o['_human']['len'] = [];
- $o['sep.human'] = $o['sep'];
+ $o['_human']['sep'] = $o['sep'];
$o['sep'] = "\t";
- $o['eol.human'] = $o['eol'];
+ $o['_human']['eol'] = $o['eol'];
$o['eol'] = "";
- }
- if (empty($GLOBALS['_human_len'])) {
- $GLOBALS['_human_len'] = [];
$o['tmpfile'] = tmpfile();
- # no echo with out_csv()
+ # no csv echo call
$o['return'] = true;
+ $o['_human']['len'] = [];
}
- $line = out_csv($row,$o);
+ $line = out::row_csv($o,$row);
+
+ # Search max length string
$i = 0;
foreach (explode($o['sep'],$line) as $v) {
$len = mb_strlen($v,out::$charset);
+
if (0
- or empty($GLOBALS['_human_len'][$i])
- or $len > $GLOBALS['_human_len'][$i]
- ) $GLOBALS['_human_len'][$i] = $len;
+ or empty($o['_human']['len'][$i])
+ or $len > $o['_human']['len'][$i]
+ ) $o['_human']['len'][$i] = $len;
+
$i++;
}
- #debug($line);
+ if (!isset($o['tmpfile'])) {
+ echo "\n";
+ out::bye("Option tmpfile is missing, wrong uage of this funtion!");
+ }
fwrite($o['tmpfile'],$line."\n");
}
function out_human_end(&$o) {
-#debug($GLOBALS['_human_len']);
- $len = $GLOBALS['_human_len'];
- #bye($len);
+ $len = $o['_human']['len'];
fseek($o['tmpfile'], 0);
$sep_line = '';
if (out::p('sep_line') !== '0') {
foreach ($len as $i) {
- $sep_line .= $o['corner'].str_repeat($o['line'],$i+strlen($o['sep.human'])-1);
+ $sep_line .= $o['corner'].str_repeat($o['line'],$i+strlen($o['_human']['sep'])-1);
}
- $sep_line .= $o['corner'].$o['eol.human'];
+ $sep_line .= $o['corner'].$o['_human']['eol'];
}
$header = empty($o['_human']['head']) ? 0 : 1;
$values = [];
$i = 0;
foreach (explode($o['sep'],rtrim($line,"\n")) as $v) {
- #if ($v=='Comunicación') $values[] = str_pad($v, $len[$i]+1, ' ', STR_PAD_LEFT);
$values[] = out::mb_str_pad($v, $len[$i], ' ', STR_PAD_RIGHT);
$i++;
}
# header
- if (!$count) {
- echo $sep_line;
- }
+ if (!$count) echo $sep_line;
- #debug($count);
if ($header or $count>0) echo ''
- . ltrim($o['sep.human'])
- . join($o['sep.human'],$values)
- . rtrim($o['sep.human'])
- .$o['eol.human'];
+ . ltrim($o['_human']['sep'])
+ . join($o['_human']['sep'],$values)
+ . rtrim($o['_human']['sep'])
+ .$o['_human']['eol'];
if ($header and !$count) echo $sep_line;
$count++;
}
fclose($o['tmpfile']);
- unset($GLOBALS['_human_len']);
unset($o['_human']);
if ($header) $count--;