2 namespace Consolidation\OutputFormatters\Formatters;
4 use Consolidation\OutputFormatters\Options\FormatterOptions;
5 use Consolidation\OutputFormatters\StructuredData\ListDataInterface;
6 use Consolidation\OutputFormatters\StructuredData\RenderCellInterface;
7 use Consolidation\OutputFormatters\Transformations\OverrideRestructureInterface;
8 use Symfony\Component\Console\Output\OutputInterface;
11 * Display the data in a simple list.
13 * This formatter prints a plain, unadorned list of data,
14 * with each data item appearing on a separate line. If you
15 * wish your list to contain headers, then use the table
16 * formatter, and wrap your data in an PropertyList.
18 class ListFormatter implements FormatterInterface, OverrideRestructureInterface, RenderDataInterface
23 public function write(OutputInterface $output, $data, FormatterOptions $options)
25 $output->writeln(implode("\n", $data));
31 public function overrideRestructure($structuredOutput, FormatterOptions $options)
33 // If the structured data implements ListDataInterface,
34 // then we will render whatever data its 'getListData'
36 if ($structuredOutput instanceof ListDataInterface) {
37 return $this->renderData($structuredOutput, $structuredOutput->getListData($options), $options);
44 public function renderData($originalData, $restructuredData, FormatterOptions $options)
46 if ($originalData instanceof RenderCellInterface) {
47 return $this->renderEachCell($originalData, $restructuredData, $options);
49 return $restructuredData;
52 protected function renderEachCell($originalData, $restructuredData, FormatterOptions $options)
54 foreach ($restructuredData as $key => $cellData) {
55 $restructuredData[$key] = $originalData->renderCell($key, $cellData, $options, $restructuredData);
57 return $restructuredData;