<?php
namespace Microsoft\VisualBasic\Data\csv {
class TableView {
public static function ToHTMLTable($data, $project = null, $bootstrap = true) {
$project = self::FieldProjects($data, $project);
$project = self::Extract($project);
$theads = "";
foreach($project["title"] as $title) {
$th = "<th>$title</th>";
$theads = $theads . $th . "\n";
}
$project = $project["fields"];
$rows = "";
foreach($data as $array) {
$td = "";
foreach($project as $field) {
$td = $td . "<td>{$array[$field]}</td>";
}
$rows = $rows . "<tr>
$td
</tr>";
}
if ($bootstrap) {
$bootstrap = "table table-hover";
} else {
$bootstrap = "";
}
return "<table class='$bootstrap'>
<thead>
<tr>$theads</tr>
</thead>
<tbody>
$rows
</tbody>
</table>";
}
public static function ToMarkdownTable($data, $project = null) {
}
public static function FieldProjects($array, $project) {
# 确保域不是空的,即需要写入csv文件的列不是空集合
if (!$project) {
$project = [];
foreach (array_keys($array[0]) as $fieldName) {
$project[$fieldName] = $fieldName;
}
} else if (is_string($project)) {
# 是 A|B|C|D|E 这种格式
# 则进行切割
$fields = [];
foreach (explode("|", $project) as $fieldName) {
$fields[$fieldName] = $fieldName;
}
$project = $fields;
} else if (is_array($project)) {
$fields = [];
foreach($project as $ref) {
if (is_string($ref)) {
$fields[$ref] = $ref;
} else {
list($ref, $title) = \Utils::Tuple($ref);
$fields[$ref] = $title;
}
}
$project = $fields;
} else {
throw new \exception("Unsupport data type!");
}
return $project;
}
public static function Extract($project) {
$names = [];
$fields = [];
foreach ($project as $field => $title) {
array_push($names, $title);
array_push($fields, $field);
}
return [
"fields" => $fields,
"title" => $names
];
}
}
}