Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / consolidation / output-formatters / src / StructuredData / AbstractStructuredList.php
1 <?php
2 namespace Consolidation\OutputFormatters\StructuredData;
3
4 use Consolidation\OutputFormatters\StructuredData\RestructureInterface;
5 use Consolidation\OutputFormatters\Options\FormatterOptions;
6 use Consolidation\OutputFormatters\StructuredData\ListDataInterface;
7 use Consolidation\OutputFormatters\Transformations\ReorderFields;
8 use Consolidation\OutputFormatters\Transformations\TableTransformation;
9
10 /**
11  * Holds an array where each element of the array is one row,
12  * and each row contains an associative array where the keys
13  * are the field names, and the values are the field data.
14  *
15  * It is presumed that every row contains the same keys.
16  */
17 abstract class AbstractStructuredList extends ListDataFromKeys implements RestructureInterface, RenderCellCollectionInterface
18 {
19     use RenderCellCollectionTrait;
20
21     public function __construct($data)
22     {
23         parent::__construct($data);
24     }
25
26     abstract public function restructure(FormatterOptions $options);
27
28     protected function createTableTransformation($data, $options)
29     {
30         $defaults = $this->defaultOptions();
31         $fieldLabels = $this->getReorderedFieldLabels($data, $options, $defaults);
32
33         $tableTransformer = $this->instantiateTableTransformation($data, $fieldLabels, $options->get(FormatterOptions::ROW_LABELS, $defaults));
34         if ($options->get(FormatterOptions::LIST_ORIENTATION, $defaults)) {
35             $tableTransformer->setLayout(TableTransformation::LIST_LAYOUT);
36         }
37
38         return $tableTransformer;
39     }
40
41     protected function instantiateTableTransformation($data, $fieldLabels, $rowLabels)
42     {
43         return new TableTransformation($data, $fieldLabels, $rowLabels);
44     }
45
46     protected function getReorderedFieldLabels($data, $options, $defaults)
47     {
48         $reorderer = new ReorderFields();
49         $fieldLabels = $reorderer->reorder(
50             $this->getFields($options, $defaults),
51             $options->get(FormatterOptions::FIELD_LABELS, $defaults),
52             $data
53         );
54         return $fieldLabels;
55     }
56
57     protected function getFields($options, $defaults)
58     {
59         $fieldShortcut = $options->get(FormatterOptions::FIELD);
60         if (!empty($fieldShortcut)) {
61             return [$fieldShortcut];
62         }
63         $result = $options->get(FormatterOptions::FIELDS, $defaults);
64         if (!empty($result)) {
65             return $result;
66         }
67         return $options->get(FormatterOptions::DEFAULT_FIELDS, $defaults);
68     }
69
70     /**
71      * A structured list may provide its own set of default options. These
72      * will be used in place of the command's default options (from the
73      * annotations) in instances where the user does not provide the options
74      * explicitly (on the commandline) or implicitly (via a configuration file).
75      *
76      * @return array
77      */
78     protected function defaultOptions()
79     {
80         return [
81             FormatterOptions::FIELDS => [],
82             FormatterOptions::FIELD_LABELS => [],
83             FormatterOptions::ROW_LABELS => [],
84             FormatterOptions::DEFAULT_FIELDS => [],
85         ];
86     }
87 }