1
|
|
|
<?php |
2
|
|
|
namespace Consolidation\OutputFormatters\StructuredData; |
3
|
|
|
|
4
|
|
|
use Consolidation\OutputFormatters\Options\FormatterOptions; |
5
|
|
|
use Consolidation\OutputFormatters\StructuredData\ListDataInterface; |
6
|
|
|
use Consolidation\OutputFormatters\Transformations\PropertyParser; |
7
|
|
|
use Consolidation\OutputFormatters\Transformations\ReorderFields; |
8
|
|
|
use Consolidation\OutputFormatters\Transformations\TableTransformation; |
9
|
|
|
use Consolidation\OutputFormatters\Transformations\PropertyListTableTransformation; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Holds an array where each element of the array is one |
13
|
|
|
* key : value pair. The keys must be unique, as is typically |
14
|
|
|
* the case for associative arrays. |
15
|
|
|
*/ |
16
|
|
|
class PropertyList extends AbstractStructuredList implements ConversionInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @inheritdoc |
20
|
|
|
*/ |
21
|
|
View Code Duplication |
public function convert(FormatterOptions $options) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
$defaults = $this->defaultOptions(); |
24
|
|
|
$fields = $this->getFields($options, $defaults); |
25
|
|
|
if (FieldProcessor::hasUnstructuredFieldAccess($fields)) { |
26
|
|
|
return new UnstructuredData($this->getArrayCopy()); |
27
|
|
|
} |
28
|
|
|
return $this; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Restructure this data for output by converting it into a table |
33
|
|
|
* transformation object. |
34
|
|
|
* |
35
|
|
|
* @param FormatterOptions $options Options that affect output formatting. |
36
|
|
|
* @return Consolidation\OutputFormatters\Transformations\TableTransformation |
37
|
|
|
*/ |
38
|
|
|
public function restructure(FormatterOptions $options) |
39
|
|
|
{ |
40
|
|
|
$data = [$this->getArrayCopy()]; |
41
|
|
|
$options->setConfigurationDefault('list-orientation', true); |
42
|
|
|
$tableTransformer = $this->createTableTransformation($data, $options); |
43
|
|
|
return $tableTransformer; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getListData(FormatterOptions $options) |
47
|
|
|
{ |
48
|
|
|
$data = $this->getArrayCopy(); |
49
|
|
|
|
50
|
|
|
$defaults = $this->defaultOptions(); |
51
|
|
|
$fieldLabels = $this->getReorderedFieldLabels([$data], $options, $defaults); |
52
|
|
|
|
53
|
|
|
$result = []; |
54
|
|
|
foreach ($fieldLabels as $id => $label) { |
55
|
|
|
$result[$id] = $data[$id]; |
56
|
|
|
} |
57
|
|
|
return $result; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function defaultOptions() |
61
|
|
|
{ |
62
|
|
|
return [ |
|
|
|
|
63
|
|
|
FormatterOptions::LIST_ORIENTATION => true, |
64
|
|
|
] + parent::defaultOptions(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function instantiateTableTransformation($data, $fieldLabels, $rowLabels) |
68
|
|
|
{ |
69
|
|
|
return new PropertyListTableTransformation($data, $fieldLabels, $rowLabels); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.