-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvToMDTable.php
More file actions
57 lines (47 loc) · 1.3 KB
/
csvToMDTable.php
File metadata and controls
57 lines (47 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
include 'vendor/autoload.php';
$resultsFile = $argv[1] ?? 'results.csv';
$fileReader = new \SOB\CSV\FileReader($resultsFile);
$lengths = [];
foreach ($fileReader as $row)
{
foreach ($row as $field => $value)
{
if (is_numeric($value) && strpos($value, '.'))
{
$value = number_format((float)$value, 7);
}
if (!isset($lengths[$field]))
{
$lengths[$field] = 0;
}
$lengths[$field] = max($lengths[$field], strlen($value));
$lengths[$field] = max($lengths[$field], strlen($field));
}
}
$parts = explode('.', $resultsFile);
$mdFile = $parts[0] . '.md';
$output = '|' . implode('|', padRow(array_combine(array_keys($row), array_keys($row)), $lengths)) . "|\n";
foreach ($row as $field => $label)
{
$row[$field] = str_repeat('-', $lengths[$field]);
}
$output .= '|' . implode('|', $row) . "|\n";
foreach ($fileReader as $row)
{
$output .= '|' . implode('|', padRow($row, $lengths)) . "|\n";
}
file_put_contents($mdFile, $output);
function padRow(array $row, array $lengths) : array
{
foreach ($row as $field => $label)
{
$value = $row[$field];
if (is_numeric($value) && strpos($value, '.'))
{
$value = number_format((float)$value, 7);
}
$row[$field] = str_pad($value, $lengths[$field], pad_type:is_numeric($label) ? STR_PAD_LEFT : STR_PAD_RIGHT);
}
return $row;
}