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 \ArrayObject implements RestructureInterface, ListDataInterface |
18
|
|
|
{ |
19
|
|
|
protected $data; |
20
|
|
|
|
21
|
|
|
public function __construct($data) |
22
|
|
|
{ |
23
|
|
|
parent::__construct($data); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
abstract public function restructure(FormatterOptions $options); |
27
|
|
|
|
28
|
|
|
abstract public function getListData(FormatterOptions $options); |
29
|
|
|
|
30
|
|
|
protected function createTableTransformation($data, $options) |
31
|
|
|
{ |
32
|
|
|
$defaults = $this->defaultOptions(); |
33
|
|
|
$fieldLabels = $this->getReorderedFieldLabels($data, $options, $defaults); |
34
|
|
|
|
35
|
|
|
$tableTransformer = new TableTransformation($data, $fieldLabels, $options->get(FormatterOptions::ROW_LABELS, $defaults)); |
36
|
|
|
if ($options->get(FormatterOptions::LIST_ORIENTATION, $defaults)) { |
37
|
|
|
$tableTransformer->setLayout(TableTransformation::LIST_LAYOUT); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $tableTransformer; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function getReorderedFieldLabels($data, $options, $defaults) |
44
|
|
|
{ |
45
|
|
|
$reorderer = new ReorderFields(); |
46
|
|
|
$fieldLabels = $reorderer->reorder( |
47
|
|
|
$this->getFields($options, $defaults), |
48
|
|
|
$options->get(FormatterOptions::FIELD_LABELS, $defaults), |
49
|
|
|
$data |
50
|
|
|
); |
51
|
|
|
return $fieldLabels; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function getFields($options, $defaults) |
55
|
|
|
{ |
56
|
|
|
$fieldShortcut = $options->get(FormatterOptions::FIELD); |
57
|
|
|
if (!empty($fieldShortcut)) { |
58
|
|
|
return [$fieldShortcut]; |
59
|
|
|
} |
60
|
|
|
$result = $options->get(FormatterOptions::FIELDS, $defaults); |
61
|
|
|
if (!empty($result)) { |
62
|
|
|
return $result; |
63
|
|
|
} |
64
|
|
|
return $options->get(FormatterOptions::DEFAULT_FIELDS, $defaults); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* A structured list may provide its own set of default options. These |
69
|
|
|
* will be used in place of the command's default options (from the |
70
|
|
|
* annotations) in instances where the user does not provide the options |
71
|
|
|
* explicitly (on the commandline) or implicitly (via a configuration file). |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
protected function defaultOptions() |
76
|
|
|
{ |
77
|
|
|
return [ |
78
|
|
|
FormatterOptions::FIELDS => [], |
79
|
|
|
FormatterOptions::FIELD_LABELS => [], |
80
|
|
|
FormatterOptions::ROW_LABELS => [], |
81
|
|
|
FormatterOptions::DEFAULT_FIELDS => [], |
82
|
|
|
]; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|