Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / consolidation / output-formatters / src / Formatters / ListFormatter.php
1 <?php
2 namespace Consolidation\OutputFormatters\Formatters;
3
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;
9
10 /**
11  * Display the data in a simple list.
12  *
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.
17  */
18 class ListFormatter implements FormatterInterface, OverrideRestructureInterface, RenderDataInterface
19 {
20     /**
21      * @inheritdoc
22      */
23     public function write(OutputInterface $output, $data, FormatterOptions $options)
24     {
25         $output->writeln(implode("\n", $data));
26     }
27
28     /**
29      * @inheritdoc
30      */
31     public function overrideRestructure($structuredOutput, FormatterOptions $options)
32     {
33         // If the structured data implements ListDataInterface,
34         // then we will render whatever data its 'getListData'
35         // method provides.
36         if ($structuredOutput instanceof ListDataInterface) {
37             return $this->renderData($structuredOutput, $structuredOutput->getListData($options), $options);
38         }
39     }
40
41     /**
42      * @inheritdoc
43      */
44     public function renderData($originalData, $restructuredData, FormatterOptions $options)
45     {
46         if ($originalData instanceof RenderCellInterface) {
47             return $this->renderEachCell($originalData, $restructuredData, $options);
48         }
49         return $restructuredData;
50     }
51
52     protected function renderEachCell($originalData, $restructuredData, FormatterOptions $options)
53     {
54         foreach ($restructuredData as $key => $cellData) {
55             $restructuredData[$key] = $originalData->renderCell($key, $cellData, $options, $restructuredData);
56         }
57         return $restructuredData;
58     }
59 }