function FormatTrait::writeRows

Writes rows in the requested format.

Parameters

\Symfony\Component\Console\Output\OutputInterface $output: The output.

array<int, array<string, mixed>> $rows: The rows, each keyed by field name.

array<string, string> $headers: The table headers, keyed by field name.

string $format: One of table, json or yaml.

2 calls to FormatTrait::writeRows()
DevelServicesCommand::__invoke in src/Command/DevelServicesCommand.php
Lists the services in the container.
DevelTokenCommand::__invoke in src/Command/DevelTokenCommand.php
Lists the available tokens.

File

src/Command/FormatTrait.php, line 28

Class

FormatTrait
Writes rows of data as a table, JSON or YAML.

Namespace

Drupal\devel\Command

Code

protected function writeRows(OutputInterface $output, array $rows, array $headers, string $format) : void {
  switch ($format) {
    case 'json':
      $output->writeln(json_encode($rows, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR), OutputInterface::OUTPUT_RAW);
      break;

    case 'yaml':
      $output->write(Yaml::encode($rows), FALSE, OutputInterface::OUTPUT_RAW);
      break;

    case 'table':
      (new Table($output))->setHeaders(array_values($headers))
        ->setRows(array_map(static fn(array $row): array => array_map(strval(...), array_values($row)), $rows))
        ->render();
      break;

    default:
      throw new \InvalidArgumentException(sprintf('Unknown format %s. Use table, json or yaml.', $format));
  }
}