Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / consolidation / output-formatters / src / Formatters / TableFormatter.php
1 <?php
2 namespace Consolidation\OutputFormatters\Formatters;
3
4 use Symfony\Component\Console\Output\OutputInterface;
5 use Symfony\Component\Console\Helper\Table;
6 use Symfony\Component\Console\Helper\TableStyle;
7
8 use Consolidation\OutputFormatters\Validate\ValidDataTypesInterface;
9 use Consolidation\OutputFormatters\Options\FormatterOptions;
10 use Consolidation\OutputFormatters\Validate\ValidDataTypesTrait;
11 use Consolidation\OutputFormatters\StructuredData\TableDataInterface;
12 use Consolidation\OutputFormatters\Transformations\ReorderFields;
13 use Consolidation\OutputFormatters\Exception\IncompatibleDataException;
14 use Consolidation\OutputFormatters\Transformations\WordWrapper;
15 use Consolidation\OutputFormatters\Formatters\HumanReadableFormat;
16
17 /**
18  * Display a table of data with the Symfony Table class.
19  *
20  * This formatter takes data of either the RowsOfFields or
21  * PropertyList data type.  Tables can be rendered with the
22  * rows running either vertically (the normal orientation) or
23  * horizontally.  By default, associative lists will be displayed
24  * as two columns, with the key in the first column and the
25  * value in the second column.
26  */
27 class TableFormatter implements FormatterInterface, ValidDataTypesInterface, RenderDataInterface, MetadataFormatterInterface, HumanReadableFormat
28 {
29     use ValidDataTypesTrait;
30     use RenderTableDataTrait;
31     use MetadataFormatterTrait;
32
33     protected $fieldLabels;
34     protected $defaultFields;
35
36     public function __construct()
37     {
38     }
39
40     public function validDataTypes()
41     {
42         return
43             [
44                 new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\RowsOfFields'),
45                 new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\PropertyList')
46             ];
47     }
48
49     /**
50      * @inheritdoc
51      */
52     public function validate($structuredData)
53     {
54         // If the provided data was of class RowsOfFields
55         // or PropertyList, it will be converted into
56         // a TableTransformation object by the restructure call.
57         if (!$structuredData instanceof TableDataInterface) {
58             throw new IncompatibleDataException(
59                 $this,
60                 $structuredData,
61                 $this->validDataTypes()
62             );
63         }
64         return $structuredData;
65     }
66
67     /**
68      * @inheritdoc
69      */
70     public function write(OutputInterface $output, $tableTransformer, FormatterOptions $options)
71     {
72         $headers = [];
73         $defaults = [
74             FormatterOptions::TABLE_STYLE => 'consolidation',
75             FormatterOptions::INCLUDE_FIELD_LABELS => true,
76         ];
77
78         $table = new Table($output);
79
80         static::addCustomTableStyles($table);
81
82         $table->setStyle($options->get(FormatterOptions::TABLE_STYLE, $defaults));
83         $isList = $tableTransformer->isList();
84         $includeHeaders = $options->get(FormatterOptions::INCLUDE_FIELD_LABELS, $defaults);
85         $listDelimiter = $options->get(FormatterOptions::LIST_DELIMITER, $defaults);
86
87         $headers = $tableTransformer->getHeaders();
88         $data = $tableTransformer->getTableData($includeHeaders && $isList);
89
90         if ($listDelimiter) {
91             if (!empty($headers)) {
92                 array_splice($headers, 1, 0, ':');
93             }
94             $data = array_map(function ($item) {
95                 array_splice($item, 1, 0, ':');
96                 return $item;
97             }, $data);
98         }
99
100         if ($includeHeaders && !$isList) {
101             $table->setHeaders($headers);
102         }
103
104         // todo: $output->getFormatter();
105         $data = $this->wrap($headers, $data, $table->getStyle(), $options);
106         $table->setRows($data);
107         $table->render();
108     }
109
110     /**
111      * Wrap the table data
112      * @param array $data
113      * @param TableStyle $tableStyle
114      * @param FormatterOptions $options
115      * @return array
116      */
117     protected function wrap($headers, $data, TableStyle $tableStyle, FormatterOptions $options)
118     {
119         $wrapper = new WordWrapper($options->get(FormatterOptions::TERMINAL_WIDTH));
120         $wrapper->setPaddingFromStyle($tableStyle);
121         if (!empty($headers)) {
122             $headerLengths = array_map(function ($item) {
123                 return strlen($item);
124             }, $headers);
125             $wrapper->setMinimumWidths($headerLengths);
126         }
127         return $wrapper->wrap($data);
128     }
129
130     /**
131      * Add our custom table style(s) to the table.
132      */
133     protected static function addCustomTableStyles($table)
134     {
135         // The 'consolidation' style is the same as the 'symfony-style-guide'
136         // style, except it maintains the colored headers used in 'default'.
137         $consolidationStyle = new TableStyle();
138         $consolidationStyle
139             ->setHorizontalBorderChar('-')
140             ->setVerticalBorderChar(' ')
141             ->setCrossingChar(' ')
142         ;
143         $table->setStyleDefinition('consolidation', $consolidationStyle);
144     }
145 }