if ($size) {
$align = $size>0 ? STR_PAD_LEFT : STR_PAD_RIGHT;
$size = abs($size);
- if ($count>1) $size -= mb_strlen($o['sep'],out::$charset);
- $v = out::mb_str_pad($v, $size, ' ',$align);
+ if ($count>1) $size -= mb_strlen($o['sep'],self::$charset);
+ $v = self::mb_str_pad($v, $size, ' ',$align);
}
return $v;
'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'];
+ echo ($o['eol'] ? ' ' : '')."<$k><![CDATA[".self::scalar($v)."]]></$k>".$o['eol'];
}
echo ($o['eol'] ? ' ' : '')."</row>".$o['eol'];
},
'yaml' => [
'enclose' => ["---\n",""],
'row' => function (&$o,&$row) {
- $yaml = '- '.out::yaml_encode($row);
+ $yaml = '- '.self::yaml_encode($row);
$yaml = preg_replace("/^(?!-)/m"," ",$yaml);
echo $yaml;
}
}
public static function type_call($k,&$o,&$row=[]) {
-
static $methods;
if (!isset($o[$k])) {
$call = $o[$k];
echo $call($o,$row);
+ # Internal method
+ } elseif(is_scalar($o[$k]) and preg_match('/^out_tag|out_human/',$o[$k])) {
+ $f = $o[$k];
+ echo self::$f($o,$row);
+
# String
} elseif(is_scalar($o[$k])) {
echo $o[$k];
# Assume html/xml tag style
} elseif (isset($o['tag'])) {
- $ret = out_tag($o,$row);
+ $ret = self::out_tag($o,$row);
} else {
# txt
*/
public static function row_parse(&$row,$o=[]) {
foreach ($row as $k => $v) {
- $row[$k] = out::format($v,null,$o);
+ $row[$k] = self::format($v,null,$o);
}
if (isset($o['row_parse_post'])) {
$o['row_parse_post']($row);
if (!empty($o['quote'])) $v = $o['quote'] .
str_replace($o['quote'],$o['quote_escape'].$o['quote'],$v)
. $o['quote'];
- $values[] = str_replace("\n",'\\n',out::scalar($v));
+ $values[] = str_replace("\n",'\\n',self::scalar($v));
}
if (!empty($o['return'])) return join($o['sep'],$values);
);
}
-} Out::init() ; # < Class
-/*--------------------------------------------------------------------------*/
-// Functions
-/*--------------------------------------------------------------------------*/
-
-//
-// Tag
-//
-function out_tag_head($o,&$data) {
- if (isset($o['tag_head'])) $o['tag'] = $o['tag_head'];
- $row = out::ar_first($data);
- if (!out::is_hash($row)) $row = $data;
- if (!is_array($row)) return 0;
-
- # For object
- if (out::is_hash($row)) $row = array_combine(array_keys($row),array_keys($row));
-
- # Avoid: <th class="0">name</th>
- if (!out::is_hash($row)) $row = array_combine(array_values($row),array_values($row));
- out_tag($o,$row);
-}
-
-function out_tag(&$o,&$row) {
- if (isset($o['tag_enclose'])) echo (NB_EOL ? ' ' : '').'<'.$o['tag_enclose'].'>'.NB_EOL;
-
- out::row_parse($row,$o);
- foreach ($row as $k => $v) {
-
- # Inside tag, ex: label
- if (isset($o['tag_key'])) {
- $v = '<'.$o['tag_key'].'>'.$k.'</'.preg_replace('/ .*$/','',$o['tag_key']).'>'.$v;
- }
-
- # Tag = o[tag] or key
- if (isset($o['tag'])) {
- $class = is_numeric($k) ? '' : ' class="'.$k.'"';
- $k = $o['tag'];
- } else {
- $class = '';
- }
-
- echo ''
- .(NB_EOL ? ' ' : '')
- ."<$k$class>$v</$k>"
- .NB_EOL
- ;
+ //
+ // Tag
+ //
+ public static function out_tag_head($o,&$data) {
+ if (isset($o['tag_head'])) $o['tag'] = $o['tag_head'];
+ $row = self::ar_first($data);
+ if (!self::is_hash($row)) $row = $data;
+ if (!is_array($row)) return 0;
+
+ # For object
+ if (self::is_hash($row)) $row = array_combine(array_keys($row),array_keys($row));
+
+ # Avoid: <th class="0">name</th>
+ if (!self::is_hash($row)) $row = array_combine(array_values($row),array_values($row));
+ self::out_tag($o,$row);
}
- if (isset($o['tag_enclose'])) echo (NB_EOL ? ' ' : '').'</'.preg_replace('/ .*$/','',$o['tag_enclose']).'>'.NB_EOL;
-}
+ public static function out_tag(&$o,&$row) {
+ if (isset($o['tag_enclose'])) echo (NB_EOL ? ' ' : '').'<'.$o['tag_enclose'].'>'.NB_EOL;
-//
-// Human
-//
-function out_human_head(&$o,$head) {
- #$line = "a\tb\tc\t\t\tg"; bye(explode("\t",$line));
-
- if (!out::header()) return '';
- $o['_human'] = [
- 'head' => 1,
- ];
- if (out::is_hash($head)) $head = array_keys($head);
- return out_human($o,$head);
-}
+ self::row_parse($row,$o);
+ foreach ($row as $k => $v) {
-function out_human(&$o,&$row) {
+ # Inside tag, ex: label
+ if (isset($o['tag_key'])) {
+ $v = '<'.$o['tag_key'].'>'.$k.'</'.preg_replace('/ .*$/','',$o['tag_key']).'>'.$v;
+ }
- # Init options
- if (empty($o['_human'])) $o['_human'] = [];
- if (!isset($o['_human']['len'])) {
- $o['_human']['len'] = [];
+ # Tag = o[tag] or key
+ if (isset($o['tag'])) {
+ $class = is_numeric($k) ? '' : ' class="'.$k.'"';
+ $k = $o['tag'];
+ } else {
+ $class = '';
+ }
- $o['_human']['sep'] = $o['sep'];
- $o['sep'] = "\t";
+ echo ''
+ .(NB_EOL ? ' ' : '')
+ ."<$k$class>$v</$k>"
+ .NB_EOL
+ ;
+ }
- $o['_human']['eol'] = $o['eol'];
- $o['eol'] = "";
+ if (isset($o['tag_enclose'])) echo (NB_EOL ? ' ' : '').'</'.preg_replace('/ .*$/','',$o['tag_enclose']).'>'.NB_EOL;
+ }
- $o['tmpfile'] = tmpfile();
+ //
+ // Human
+ //
+ public static function out_human_head(&$o,$head) {
+ #$line = "a\tb\tc\t\t\tg"; bye(explode("\t",$line));
- # no csv echo call
- $o['return'] = true;
- $o['_human']['len'] = [];
+ if (!self::header()) return '';
+ $o['_human'] = [
+ 'head' => 1,
+ ];
+ if (self::is_hash($head)) $head = array_keys($head);
+ return self::out_human($o,$head);
}
- $line = out::row_csv($o,$row);
- $line = str_replace('\n',' ',$line);
+ public static function out_human(&$o,&$row) {
- # Search max length string
- $i = 0;
- foreach (explode($o['sep'],$line) as $v) {
- $len = mb_strlen($v,out::$charset);
+ # Init options
+ if (empty($o['_human'])) $o['_human'] = [];
+ if (!isset($o['_human']['len'])) {
+ $o['_human']['len'] = [];
- if (0
- or empty($o['_human']['len'][$i])
- or $len > $o['_human']['len'][$i]
- ) $o['_human']['len'][$i] = $len;
+ $o['_human']['sep'] = $o['sep'];
+ $o['sep'] = "\t";
- $i++;
- }
+ $o['_human']['eol'] = $o['eol'];
+ $o['eol'] = "";
- if (!isset($o['tmpfile'])) {
- echo "\n";
- out::bye("Option tmpfile is missing, wrong uage of this funtion!");
- }
+ $o['tmpfile'] = tmpfile();
- fwrite($o['tmpfile'],$line."\n");
+ # no csv echo call
+ $o['return'] = true;
+ $o['_human']['len'] = [];
+ }
-}
+ $line = self::row_csv($o,$row);
+ $line = str_replace('\n',' ',$line);
-function out_human_end(&$o) {
- $len = $o['_human']['len'];
- fseek($o['tmpfile'], 0);
+ # Search max length string
+ $i = 0;
+ foreach (explode($o['sep'],$line) as $v) {
+ $len = mb_strlen($v,self::$charset);
+
+ if (0
+ or empty($o['_human']['len'][$i])
+ or $len > $o['_human']['len'][$i]
+ ) $o['_human']['len'][$i] = $len;
- $sep_line = '';
- if (out::p('sep_line') !== '0') {
+ $i++;
+ }
- foreach ($len as $i) {
- $sep_line .= $o['corner'].str_repeat($o['line'],$i+strlen($o['_human']['sep'])-1);
+ if (!isset($o['tmpfile'])) {
+ echo "\n";
+ self::bye("Option tmpfile is missing, wrong uage of this funtion!");
}
- $sep_line .= $o['corner'].$o['_human']['eol'];
+
+ fwrite($o['tmpfile'],$line."\n");
+
}
- $header = empty($o['_human']['head']) ? 0 : 1;
- $count = 0;
- while (($line = fgets($o['tmpfile'])) !== false) {
+ public static function out_human_end(&$o) {
+ $len = $o['_human']['len'];
+ fseek($o['tmpfile'], 0);
- $values = [];
- $i = 0;
- foreach (explode($o['sep'],rtrim($line,"\n")) as $v) {
- $values[] = out::mb_str_pad($v, $len[$i], ' ', STR_PAD_RIGHT);
- $i++;
+ $sep_line = '';
+ if (self::p('sep_line') !== '0') {
+
+ foreach ($len as $i) {
+ $sep_line .= $o['corner'].str_repeat($o['line'],$i+strlen($o['_human']['sep'])-1);
+ }
+ $sep_line .= $o['corner'].$o['_human']['eol'];
}
- # header
- if (!$count) echo $sep_line;
+ $header = empty($o['_human']['head']) ? 0 : 1;
+ $count = 0;
+ while (($line = fgets($o['tmpfile'])) !== false) {
- if ($header or $count>0) echo ''
- . ltrim($o['_human']['sep'])
- . join($o['_human']['sep'],$values)
- . rtrim($o['_human']['sep'])
- .$o['_human']['eol'];
+ $values = [];
+ $i = 0;
+ foreach (explode($o['sep'],rtrim($line,"\n")) as $v) {
+ $values[] = self::mb_str_pad($v, $len[$i], ' ', STR_PAD_RIGHT);
+ $i++;
+ }
- if ($header and !$count) echo $sep_line;
- $count++;
- }
+ # header
+ if (!$count) echo $sep_line;
+
+ if ($header or $count>0) echo ''
+ . 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($o['_human']);
+ fclose($o['tmpfile']);
+ unset($o['_human']);
- if ($header) $count--;
- if (!$count) return;
+ if ($header) $count--;
+ if (!$count) return;
- echo $sep_line;
- if ($header) echo "$count Records\n";
+ echo $sep_line;
+ if ($header) echo "$count Records\n";
-}
-# < Functions
+ }
+
+} Out::init() ; # < Class
/****************************************************************************/
#die("A NB_EOL B \n");
*/
#$data = array( array('a'=>'A'), array('b'=>'B'), array('c'=>'C') );
$field = array();
-$o = out::rows(!empty($argv[2]) ? $argv[2] : 'csv',$data,$field);
+$o = self::rows(!empty($argv[2]) ? $argv[2] : 'csv',$data,$field);
?>