public static $types;
public static $type;
public static function init() {
+
self::$types = array(
+
'human' => [
- #'enclose' => array("","\n"),
- #'eol' => "\n",
'sep' => (self::p('sep') ? self::p('sep') : " | "),
'border' => '_',
'head' => 'out_human_head',
'function' => 'out_human',
- 'end' => function($o) { echo $o['line']."\n"; }
+ 'end' => 'out_human_end',
],
'sql' => array(),
'sh' => array(
- '__head' => function($r) {
- $line = [];
- foreach ($r as $k) {
- if (!preg_match('/^[a-zA-Z\w_]+$/',$k)) continue;
- $line[] = "$k=\"\"";
- }
- if ($line) echo join(' ',$line).";\n";
- },
'function' => function(&$r) {
$line = [];
foreach ($r as $k=>$v) {
'head' => 'out_csv_head',
'function' => 'out_csv',
),
+
'yaml' => array(
'enclose' => array("---\n",""),
'function' => 'out_yaml',
#'function' => 'yaml_emit',
),
+
'json' => array(
'enclose' => array('['.NB_EOL,NB_EOL.']'),
'function' => 'json_encode',
'eol' => NB_EOL,
'rec' => ',',
),
+
);
self::$type = self::client_type();
}
if (is_scalar($o['function']) and preg_match('/^out_/',$o['function'])) {
$o['function']($row,$o);
} else {
- echo $o['function']($row);
+ echo $o['function']($row,$o);
}
return true;
}
}
public static function end(&$o) {
- if (isset($o['enclose'])) echo $o['enclose'][1];
if (isset($o['end'])) echo $o['end']($o);
+ if (isset($o['enclose'])) echo $o['enclose'][1];
}
public static function rows_get($type,$data,$head=array()) {
echo $yaml;
}
-//
-// Human
-//
-function out_human_head(&$head,&$o) {
- if (!is_array($head)) {
- echo 0;
- return;
- }
- # For no scalar search max length
- $o['fields_max_len'] = [];
-
- foreach ($o['data'] as $id=>$rec) {
- $i = -1;
- foreach ($rec as $k=>$v) {
- $i++;
- if (!is_scalar($v)) continue;
- if (0
- or empty($o['fields_max_len'][$i])
- or strlen($v) >= $o['fields_max_len'][$i]
- ) $o['fields_max_len'][$i] = mb_strlen($v);
- #echo strlen($v)."\n";
- }
- }
-
- if (out::is_hash($head)) {
- $head = array_keys($head);
- }
-
- $i = -1;
- foreach ($head as $k => $v) {
- $i++;
- if (!is_scalar($v)) continue;
- if (0
- or empty($o['fields_max_len'][$i])
- or strlen($v) >= $o['fields_max_len'][$i]
- ) $o['fields_max_len'][$i] = mb_strlen($v);
- #echo strlen($v)."\n";
- }
-
- #debug($line);
- #$o['line'] = $line;
- #echo "LENGTH: "; bye( $o['fields_max_len'] );
- #echo "LENGTH: ";
- #bye([ 'fields_max_len' => $o['fields_max_len'], 'data' => $o['data'], ]);
-
- $line = '+';
- foreach ($o['fields_max_len'] as $i=>$v) {
- $line .= str_repeat('-',$v+strlen(trim($o['sep']))+1).'+';
- }
- $o['line'] = $line;
- echo "$line\n";
- out_human($head,$o);
- echo "$line\n";
-}
-
-function out_human(&$row,$o) {
- #debug($o['fields_max_len']);
-
- if (!count($row)) return;
- $i = 0;
- $values = [];
- foreach (array_values($row) as $k=>$v) {
- #if(mb_strlen($v)!=$o['fields_max_len'][$i]) debug(mb_strlen($v).' == '.$o['fields_max_len'][$i]);
- #debug("%".$o['fields_max_len'][$i]."s");
-
- $values[] = sprintf("%".'-'.$o['fields_max_len'][$i]
- #.(empty($o['fields_max_len']) or empty($o['fields_max_len'][$i]) ? '' : '-'.$o['fields_max_len'][$i])
- ."s",$v);
-
- $i++;
- }
-
- #if (count($row))
- echo ''
- . ltrim($o['sep'])
- . join($o['sep'],$values)
- . rtrim($o['sep'])
- ."\n";
-}
-
//
// Csv
//
echo "\r";
if (out::is_hash($row)) {
$ar = array_keys($row);
- out_csv($ar,$o);
+ return out_csv($ar,$o);
} else {
- out_csv($row,$o);
+ return out_csv($row,$o);
}
}
$values[] = str_replace("\n",'\\n',out::scalar($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) {
#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'];
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 ? ' ' : '')."</row>".NB_EOL;
}
+//
+// Human
+//
+function out_human_head(&$head,&$o) {
+ $o['return'] = true;
+ $o['sep.human'] = $o['sep'];
+ $o['sep'] = "\t";
+ $GLOBALS['_human_len'] = [];
+ $o['tmpfile'] = tmpfile();
+ #$line = "a\tb\tc\t\t\tg"; bye(explode("\t",$line));
+ if (out::is_hash($head)) $head = array_keys($head);
+ return out_human($head,$o);
+}
+
+function out_human(&$row,&$o) {
+
+ $line = out_csv($row,$o);
+ $i = 0;
+ foreach (explode($o['sep'],$line) as $v) {
+ $len = strlen($v);
+ if (0
+ or empty($GLOBALS['_human_len'][$i])
+ or $len > $GLOBALS['_human_len'][$i]
+ ) $GLOBALS['_human_len'][$i] = $len;
+ $i++;
+ }
+
+ #debug($line);
+ return fwrite($o['tmpfile'],$line."\n");
+}
+
+function out_human_end(&$o) {
+#debug($GLOBALS['_human_len']);
+ $len = $GLOBALS['_human_len'];
+ #debug($len);
+ fseek($o['tmpfile'], 0);
+
+ $sep_line = '';
+ if (out::p('sep_line') !== '0') {
+ foreach ($len as $i) {
+ $sep_line .= '+'.str_repeat('-',$i+2);
+ }
+ $sep_line .= '+'."\n";
+ }
+
+ $count = 0;
+ while (($line = fgets($o['tmpfile'])) !== false) {
+ $values = [];
+ $i = 0;
+ foreach (explode($o['sep'],rtrim($line,"\n")) as $v) {
+ $values[] = sprintf("%".'-'.$len[$i]."s",$v);
+ $i++;
+ }
+ if (!$count) echo $sep_line;
+
+ echo ''
+ . ltrim($o['sep.human'])
+ . join($o['sep.human'],$values)
+ . rtrim($o['sep.human'])
+ ."\n";
+
+ if (!$count) echo $sep_line;
+ $count++;
+ }
+
+ fclose($o['tmpfile']);
+ if (!$count) return;
+ echo $sep_line;
+ print "$count Records\n";
+}
# < Functions
/****************************************************************************/