Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Model/Metrics/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ private function formatPercent(?float $pc, bool $warn = true): string
return \sprintf('%.1f%%', $pc);
}

public function format(?float $value, bool $warn = true): string
public function format(?float $value, bool $warn = true): string|float|int|null
{
if (null === $value) {
return '';
return null;
}

if (PHP_INT_MAX === (int) $value) {
return '∞';
}

return match ($this) {
Format::Rounded => (string) round($value),
Format::Rounded2p => (string) round($value, 2),
Format::Rounded => round($value),
Format::Rounded2p => round($value, 2),
Format::Percent => $this->formatPercent($value, $warn),
Format::Disk, Format::Memory => FormatterHelper::formatMemory((int) $value),
};
Expand Down
31 changes: 30 additions & 1 deletion src/Service/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ public function render(array $rows, array $header = [], array $defaultColumns =
$this->renderPlain($rows, $header);
break;

case 'json':
$this->renderJson($rows, $header);
break;

case null:
case 'table':
$this->renderTable($rows, $header);
Expand All @@ -263,7 +267,7 @@ public function render(array $rows, array $header = [], array $defaultColumns =
*/
public function formatIsMachineReadable(): bool
{
return in_array($this->getFormat(), ['csv', 'tsv', 'plain']);
return in_array($this->getFormat(), ['csv', 'tsv', 'plain', 'json']);
}

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

/**
* Renders JSON output.
*
* @param array<array<int|string, string|int|float|TableCell>|TableSeparator> $rows
* @param array<int|string, string|TableCell> $header
*/
protected function renderJson(array $rows, array $header): void
{
// Remove TableSeparator objects.
$rows = array_values(array_filter($rows, '\\is_array'));

$data = [];
foreach ($rows as $row) {
$d = [];

foreach ($header as $k => $h) {
$d[(string) $h] = is_scalar($row[$k]) ? $row[$k] : (string) $row[$k];
}

$data[] = $d;
}

$this->output->write(json_encode(['data' => $data], JSON_THROW_ON_ERROR));
}

/**
* Render plain, line-based output.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Util/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private function formatRow(array $data): string
*
* @return string
*/
protected function formatCell(string|\Stringable $cell): string
protected function formatCell(string|int|float|null|bool|\Stringable $cell): string
{
// Cast cell data to a string.
$cell = (string) $cell;
Expand Down
2 changes: 1 addition & 1 deletion src/Util/PlainFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct()
parent::__construct("\t", "\n");
}

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