Class Out extends Nb {
public static $charset = 'utf-8';
- public static $types;
- public static $type;
+ protected static $types;
+ protected static $type;
public static function init() {
+ $self = get_class();
self::$types = array(
'human' => [
- 'sep' => (self::p('sep') ? self::p('sep') : " | "),
- 'border' => '_',
+ 'sep' => self::p('sep'," | "),
+ 'corner' => self::p('corner',"+"),
+ 'eol' => self::p('eol',"\n"),
+ 'line' => self::p('line',"-"),
'head' => 'out_human_head',
- 'function' => 'out_human',
+ 'row' => 'out_human',
'end' => 'out_human_end',
],
'sql' => array(),
'sh' => array(
- 'function' => function(&$r) {
+ 'row' => function(&$r) {
$line = [];
foreach ($r as $k=>$v) {
if (!preg_match('/^[a-zA-Z\w_]+$/',$k)) continue;
}
),
- #'php' => array( 'function' => function(&$r) {var_dump($r);},),
+ #'php' => array( 'row' => function(&$r) {var_dump($r);},),
'php' => array(
#'enclose' => array('$VAR=array(',');'),
'enclose' => array('$CONF = array('.NB_EOL,NB_EOL.');'),
- 'eol' => NB_EOL,
+ 'eol' => self::p('eol',NB_EOL),
'rec' => ',',
- #'function' => function(&$r) { echo preg_replace('/[\r\n]/','',var_dump($r,true));},
- #'function' => function(&$r) {print_r($r);}
- #'function' => function(&$r) {var_dump($r);}
- 'function' => function(&$r) {
+ #'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) {
return var_export($r);
echo preg_replace(array(
'/(=>\s*)\n\s+/m',
"),".NB_EOL."array"
),var_export($r,true));
}
- #'function' => function(&$r) { echo preg_replace('/\n/','',var_export($r,true));}
- #'function' => function(&$r) {var_export($r);}
+ #'row' => function(&$r) { echo preg_replace('/\n/','',var_export($r,true));}
+ #'row' => function(&$r) {var_export($r);}
),
- #'var_dump' => array( 'function' => function(&$r) {var_dump($r);},),
- #'print_r' => array( 'function' => function(&$r) {print_r($r);},),
- #'var_export' => array( 'function' => 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);},),
'div' => array(
'is_html' => true,
'tag_enclose' => 'div class="row"',
'tag' => 'div',
'tag_key' => 'label',
- 'function' => 'out_tag',
+ 'row' => 'out_tag',
'head' => 'out_tag_head',
),
'enclose' => array("<center class=\"rows\">".NB_EOL,"</center>".NB_EOL),
'tag_enclose' => 'div class="row"',
'tag' => 'div',
- 'function' => 'out_tag',
+ 'row' => 'out_tag',
'head' => 'out_tag_head',
),
'tag_enclose' => 'tr class="row"',
'tag_head' => 'th',
'tag' => 'td',
- 'function' => 'out_tag',
+ 'row' => 'out_tag',
'head' => 'out_tag_head',
),
'xml' => array(
'enclose' => array('<?xml version="1.0" encoding="utf-8"?'.'>'.NB_EOL."<rows>".NB_EOL,"</rows>".NB_EOL),
- 'function' => 'out_xml',
+ 'eol' => self::p('eol',NB_EOL),
+ 'row' => function (&$row,$o) {
+ echo ($o['eol'] ? ' ' : '')."<row>".$o['eol'];
+ foreach ($row as $k => $v) {
+ echo ($o['eol'] ? ' ' : '')."<$k><![CDATA[".out::scalar($v)."]]></$k>".$o['eol'];
+ }
+ echo ($o['eol'] ? ' ' : '')."</row>".$o['eol'];
+ },
),
- 'text' => array(
- 'eol' => "\n",
- #'function' => 'print',
+ 'txt' => array(
+ 'col' => self::p('col',' : '),
+ 'sep' => self::p('sep',"\n"),
+ 'eol' => self::p('eol',">\n"),
),
'csv' => array(
+ 'sep' => self::p('sep',"\t"),
+ 'eol' => self::p('eol',"\n"),
'enclose' => array("","\n"),
- 'eol' => "\n",
- 'sep' => (self::p('sep') ? self::p('sep') : "\t"),
- 'head' => 'out_csv_head',
- 'function' => 'out_csv',
- ),
+ '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);
+ }
+ },
- 'yaml' => array(
- 'enclose' => array("---\n",""),
- 'function' => 'out_yaml',
- #'function' => 'yaml_emit',
+ 'row' => 'out_csv',
),
+ 'yaml' => [
+ 'enclose' => ["---\n",""],
+ 'row' => function (&$row,&$o) {
+ $yaml = '- '.out::yaml_encode($row);
+ $yaml = preg_replace("/^(?!-)/m"," ",$yaml);
+ echo $yaml;
+ }
+ ],
+
'json' => array(
'enclose' => array('['.NB_EOL,NB_EOL.']'),
- 'function' => 'json_encode',
- 'eol' => NB_EOL,
+ 'row' => function($row) { return json_encode($row); },
+ 'eol' => self::p('eol',NB_EOL),
'rec' => ',',
),
);
- self::$type = self::client_type();
+
+ self::type(self::client_type());
}
public function __construct($opt=[],&$data=[],$head=[]) {
'charset',
'type',
] as $k) {
- if (isset($opt[$k])) self::${$k} = $opt[$k];
+ if (isset($opt[$k])) self::$k($opt[$k]);
unset($opt[$k]);
}
- if (isset($opt['type'])) self::$type = $opt['type'];
- unset($opt['type']);
-
- if (!empty(self::$type) and !empty($data)) return self::rows(self::$type,$data,$head);
+ if (self::type() and !empty($data)) return self::rows(self::type(),$data,$head);
return parent::__construct($opt);
}
public static function charset($set=null) { if (!empty($set)) self::$charset = $set; return self::$charset; }
- public static function types($set=null) { if (!empty($set)) self::$types = $set; return self::$types; }
+
+ public static function types($type=null) {
+ if (!empty($type)) return(empty(self::$types[$type]) ? null : self::$types[$type] );
+ return self::$types;
+ }
+
+ public static function type($set=null) { if (!empty($set)) self::$type = $set; return self::$type; }
public static function scalar($v) {
if ($v === null) return '';
return is_scalar($v) ? $v : json_encode($v);
}
+ public static function type_call($k,&$o,&$row) {
+
+ $type = self::type();
+
+ 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);
+
+ } elseif(is_callable($o[$k]) and is_callable($o[$k])) {
+
+ $call = $o[$k];
+ echo $call($o,$row);
+ return '';
+
+ }
+
+ return '';
+
+ }
+
public static function head(&$o,$head,$data=array()) {
+ $o['header'] = (self::p('header')==='0') ? false : true;
+ # Format $head
if ($head !== false) {
if (is_scalar($head) and !is_bool($head)) {
}
# For sprintf
- unset($o['head_max_len']);
if (empty($data)) {
+ unset($o['head_max_len']);
} else if (self::is_hash($data[0])) {
}
- #bye($o);
+#bye($o);
if (isset($o['enclose'])) echo $o['enclose'][0];
- if (!isset($o['head'])) return;
-
-#var_dump($head); return;
-# NB 01.04.16 if (empty($head) and $head!== false) {
-# NB 01.04.16 if (self::is_hash($data[0])) {
-# NB 01.04.16 $head = array_keys($data[0]);
-# NB 01.04.16 } else {
-# NB 01.04.16 return;
-# NB 01.04.16 }
-# NB 01.04.16 }
-
- if (self::p('header') === '0' ) return;
- if ($head === false) return;
+ if (0
+ or !$o['header']
+ or $head === false
+ #or !isset($o['head'])
+ ) return;
- echo $o['head']($head,$o) . empty($o['eol']) ? '' : $o['eol'];
+ self::type_call('head',$o,$head) . empty($o['eol']) ? '' : $o['eol'];
return $head;
}
public static function row(&$o,&$row) {
+ #self::type_call('row',$o,$row);
# Defined function
- if (isset($o['function'])) {
- if (is_scalar($o['function']) and preg_match('/^out_/',$o['function'])) {
- $o['function']($row,$o);
+ if (isset($o['row'])) {
+
+ if (is_scalar($o['row']) and preg_match('/^out_/',$o['row'])) {
+ $o['row']($row,$o);
} else {
- echo $o['function']($row,$o);
+ echo $o['row']($row,$o);
}
return true;
}
- # Assume html/xml tag like
+ # Assume html/xml tag style
if (isset($o['tag'])) {
return out_tag($row,$o);
}
- # yaml
- if (!self::is_hash($row)) {
- #bye($row);
- }
-
-# NB 10.04.16 if (!self::is_hash($row)) unset($o['head_max_len']);
-# NB 10.04.16 if (is_array($row) and count($row)>1) $o['head_max_len'] = mb_strlen(count($row));
- #if (!self::is_hash($row)) return out_yaml($row,$o);
-
- # text
- foreach ($row as $k => $v) {
- echo sprintf("%"
- .(!empty($o['head_max_len']) ? '-'.($o['head_max_len']+1) : '')
- ."s: %s\n",$k,preg_replace('/\n(\w)/m'
- ,"\n".str_repeat(' ',(empty($o['head_max_len']) ? mb_strlen($k) : $o['head_max_len']+1)+2).'\1'
- ,self::format($v))
- );
- }
+ # txt
+ return self::txt($row,$o);
return true;
}
public static function rows($type,&$data,$head=[],$conf=[]) {
- # Ex: for action=tables when header=0
-
if (empty($data)) return;
- if (is_scalar($data)) $data = array(array($data));
+ if (is_scalar($data)) $data = [[$data]];
if (!isset(self::$types[$type])) self::bye("Unknow type: `$type`");
- $conf = self::$types[$type] + $conf;
+ if ($type) self::type($type);
+ #$conf['type'] = $type;
- #echo $conf['function']($data); return true;
- if (empty($conf['enclose'])) $conf['enclose'] = array('','');
+ #echo $conf['row']($data); return true;
+ $conf = self::$types[$type] + $conf;
+ if (empty($conf['enclose'])) $conf['enclose'] = ['',''];
if (empty($conf['eol'])) $conf['eol'] = '';
- if (self::is_hash($data)) $data = array($data);
- if (is_scalar($data)) $data = array($data);
+ if (self::is_hash($data)) $data = [$data];
+ if (is_scalar($data)) $data = [$data];
$tot = count($data);
$count = 0;
if ($count) self::end($conf);
}
- public static function type($name=null,$key='') {
- if (empty(self::$types[$name])) return null;
- $type = self::$types[$name];
- if ($key) return( (array_key_exists($key,$type)) ? $type[$key] : null );
- return $type;
- }
public static function client_type() {
foreach (array_keys(self::$types) as $t) {
}
public static function format($v,$type=null,$conf=[]) {
- if ($type === null) $type = self::$type;
+ if ($type === null) $type = self::type();
$is_scalar = is_scalar($v);
$v = self::scalar($v);
if ($v==='') $is_scalar = true;
}
public static function is_html($type=null) {
- if ($type === null) $type = self::$type;
- #return self::type($type,'is_html')
+ if ($type === null) $type = self::type();
return (strpos($type,'html') !==false or (
!empty(self::$types[$type]['is_html'])
#and !empty(self::$types[$type]['is_xml'])
#$replace_flags = ENT_COMPAT | ENT_DISALLOWED;
$replace_flags = ENT_COMPAT;
- if ($type === null) $type = self::$type;
+ if ($type === null) $type = self::type();
if (!$type) {
return $v;
} elseif ($type=='html' or !empty(self::$types[$type]['is_html'])) {
$replace_flags = $replace_flags | ENT_HTML5;
- } else { # text
+ } else { # txt
# NB 06.04.16 $v = str_replace("\n",'|',$v);
# NB 06.04.16 $v = preg_replace('/\n/','',$v);
return $v;
}
+ public static function txt(&$row,&$o) {
+ $header = (out::p('header')==='0') ? false : true;
+ foreach ($row as $k => $v) {
+
+ if (!$header) {
+ echo self::format($v).$o['sep'];
+ continue;
+ }
+
+ printf(
+ "%".(!empty($o['head_max_len']) ? '-'.($o['head_max_len']) : '').'s'
+ .$o['col']."%s".$o['sep']
+ ,$k
+ ,preg_replace('/\n(\w)/m'
+ ,$o['sep'].str_repeat(' ',(empty($o['head_max_len']) ? mb_strlen($k,self::$charset) : $o['head_max_len']+1)+2).'\1'
+ ,self::format($v))
+ );
+ }
+ }
+
} Out::init() ; # < Class
/*--------------------------------------------------------------------------*/
// Functions
/*--------------------------------------------------------------------------*/
-//
-// Yaml
-//
-function out_yaml(&$row,$o) {
- $yaml = '- '.out::yaml_encode($row);
- $yaml = preg_replace("/^(?!-)/m"," ",$yaml);
- echo $yaml;
-}
-
-//
-// Csv
-//
-function out_csv_head(&$row,$o) {
- 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);
- }
-}
-
function out_csv(&$row,$o) {
-
$values = array();
foreach (array_values($row) as $k=>$v) {
if (!empty($o['return'])) return join($o['sep'],$values);
echo join($o['sep'],$values);
- #echo str_replace("\n",'\\n',join($o['sep'],$values));
}
//
// Tag
//
-function out_tag_head(&$data,$o) {
+function out_tag_head($o,&$data) {
#if (isset($o['tag_key'])) return ''; # No header, field will be in rows - NB 24.03.16
if (isset($o['tag_head'])) $o['tag'] = $o['tag_head'];
$row = out::ar_first($data);
if (isset($o['tag_enclose'])) echo (NB_EOL ? ' ' : '').'</'.preg_replace('/ .*$/','',$o['tag_enclose']).'>'.NB_EOL;
}
-//
-// Xml
-//
-function out_xml(&$row,$o) {
- echo (NB_EOL ? ' ' : '')."<row>".NB_EOL;
- foreach ($row as $k => $v) {
- echo (NB_EOL ? ' ' : '')."<$k><![CDATA[".out::scalar($v)."]]></$k>".NB_EOL;
- }
- echo (NB_EOL ? ' ' : '')."</row>".NB_EOL;
-}
-
//
// Human
//
-function out_human_head(&$head,&$o) {
+function out_human_head(&$o,&$head) {
#$line = "a\tb\tc\t\t\tg"; bye(explode("\t",$line));
+
if (out::p('header')==='0') return '';
$o['_human'] = [
'head' => 1,
}
function out_human(&$row,&$o) {
- if (empty($GLOBALS['_human_len'])) {
- $GLOBALS['_human_len'] = [];
+
+ # switch options
+ if (!isset($o['mark.human'])) {
if (empty($o['_human'])) $o['_human'] = [];
- $o['tmpfile'] = tmpfile();
+ $o['mark.human'] = 1;
- # switch for later use of out_csv()
$o['sep.human'] = $o['sep'];
$o['sep'] = "\t";
+ $o['eol.human'] = $o['eol'];
+ $o['eol'] = "";
+ }
+
+ if (empty($GLOBALS['_human_len'])) {
+ $GLOBALS['_human_len'] = [];
+ $o['tmpfile'] = tmpfile();
+
# no echo with out_csv()
$o['return'] = true;
}
$line = out_csv($row,$o);
$i = 0;
foreach (explode($o['sep'],$line) as $v) {
-# NB 16.09.16 if (preg_match('/^utf-?8$/i',out::$charset)) {
-# NB 16.09.16 $len = strlen( utf8_decode( $v ) );
-# NB 16.09.16 } else {
-# NB 16.09.16 $len = mb_strlen($v,out::$charset);
-# NB 16.09.16 }
$len = mb_strlen($v,out::$charset);
if (0
or empty($GLOBALS['_human_len'][$i])
}
#debug($line);
- return fwrite($o['tmpfile'],$line."\n");
+ fwrite($o['tmpfile'],$line."\n");
}
function out_human_end(&$o) {
if (out::p('sep_line') !== '0') {
foreach ($len as $i) {
- $sep_line .= '+'.str_repeat('-',$i+strlen($o['sep.human'])-1);
+ $sep_line .= $o['corner'].str_repeat($o['line'],$i+strlen($o['sep.human'])-1);
}
- $sep_line .= '+'."\n";
+ $sep_line .= $o['corner'].$o['eol.human'];
}
$header = empty($o['_human']['head']) ? 0 : 1;
}
#debug($count);
- if ($header or $count>0)echo ''
+ if ($header or $count>0) echo ''
. ltrim($o['sep.human'])
. join($o['sep.human'],$values)
. rtrim($o['sep.human'])
- ."\n";
+ .$o['eol.human'];
if ($header and !$count) echo $sep_line;
$count++;