Skip to content

Commit a2e93e6

Browse files
committed
Add support for JSON in metrics commands
1 parent d5b249c commit a2e93e6

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

src/Model/Metrics/Format.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,19 @@ private function formatPercent(?float $pc, bool $warn = true): string
3535
return \sprintf('%.1f%%', $pc);
3636
}
3737

38-
public function format(?float $value, bool $warn = true): string
38+
public function format(?float $value, bool $warn = true): string|float|int|null
3939
{
4040
if (null === $value) {
41-
return '';
41+
return null;
4242
}
4343

4444
if (PHP_INT_MAX === (int) $value) {
4545
return '';
4646
}
4747

4848
return match ($this) {
49-
Format::Rounded => (string) round($value),
50-
Format::Rounded2p => (string) round($value, 2),
49+
Format::Rounded => round($value),
50+
Format::Rounded2p => round($value, 2),
5151
Format::Percent => $this->formatPercent($value, $warn),
5252
Format::Disk, Format::Memory => FormatterHelper::formatMemory((int) $value),
5353
};

src/Service/Table.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ public function render(array $rows, array $header = [], array $defaultColumns =
244244
$this->renderPlain($rows, $header);
245245
break;
246246

247+
case 'json':
248+
$this->renderJson($rows, $header);
249+
break;
250+
247251
case null:
248252
case 'table':
249253
$this->renderTable($rows, $header);
@@ -263,7 +267,7 @@ public function render(array $rows, array $header = [], array $defaultColumns =
263267
*/
264268
public function formatIsMachineReadable(): bool
265269
{
266-
return in_array($this->getFormat(), ['csv', 'tsv', 'plain']);
270+
return in_array($this->getFormat(), ['csv', 'tsv', 'plain', 'json']);
267271
}
268272

269273
/**
@@ -384,6 +388,31 @@ protected function renderCsv(array $rows, array $header, string $delimiter = ','
384388
$this->output->write((new Csv($delimiter, "\n"))->format($rows));
385389
}
386390

391+
/**
392+
* Renders JSON output.
393+
*
394+
* @param array<array<int|string, string|int|float|TableCell>|TableSeparator> $rows
395+
* @param array<int|string, string|TableCell> $header
396+
*/
397+
protected function renderJson(array $rows, array $header): void
398+
{
399+
// Remove TableSeparator objects.
400+
$rows = array_values(array_filter($rows, '\\is_array'));
401+
402+
$data = [];
403+
foreach ($rows as $row) {
404+
$d = [];
405+
406+
foreach ($header as $k => $h) {
407+
$d[(string) $h] = is_scalar($row[$k]) ? $row[$k] : (string) $row[$k];
408+
}
409+
410+
$data[] = $d;
411+
}
412+
413+
$this->output->write(json_encode(['data' => $data], JSON_THROW_ON_ERROR));
414+
}
415+
387416
/**
388417
* Render plain, line-based output.
389418
*

src/Util/Csv.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private function formatRow(array $data): string
5757
*
5858
* @return string
5959
*/
60-
protected function formatCell(string|\Stringable $cell): string
60+
protected function formatCell(string|int|float|null|bool|\Stringable $cell): string
6161
{
6262
// Cast cell data to a string.
6363
$cell = (string) $cell;

src/Util/PlainFormat.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function __construct()
1717
parent::__construct("\t", "\n");
1818
}
1919

20-
protected function formatCell(string|\Stringable $cell): string
20+
protected function formatCell(string|int|float|null|bool|\Stringable $cell): string
2121
{
2222
// Replace any newline or tab characters with a space.
2323
return \preg_replace('#[\r\n\t]+#', ' ', (string) $cell);

0 commit comments

Comments
 (0)