1
|
|
|
<?php |
2
|
|
|
namespace Consolidation\OutputFormatters\StructuredData; |
3
|
|
|
|
4
|
|
|
use Consolidation\OutputFormatters\RestructureInterface; |
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
|
|
|
|
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
|
|
|
class RowsOfFields extends \ArrayObject implements RestructureInterface, ListDataInterface |
18
|
|
|
{ |
19
|
|
|
protected $data; |
20
|
|
|
|
21
|
7 |
|
public function __construct($data) |
22
|
|
|
{ |
23
|
7 |
|
parent::__construct($data); |
24
|
7 |
|
} |
25
|
|
|
|
26
|
3 |
|
public function restructure($configurationData, $options) |
27
|
|
|
{ |
28
|
3 |
|
$data = $this->getArrayCopy(); |
29
|
3 |
|
return $this->createTableTransformation($data, $configurationData, $options); |
30
|
|
|
} |
31
|
|
|
|
32
|
1 |
|
public function getListData() |
33
|
|
|
{ |
34
|
1 |
|
return array_keys($this->getArrayCopy()); |
35
|
|
|
} |
36
|
|
|
|
37
|
5 |
|
protected function createTableTransformation($data, $configurationData, $options) |
38
|
|
|
{ |
39
|
5 |
|
$options = $this->interpretOptions($configurationData, $options); |
40
|
|
|
|
41
|
5 |
|
$reorderer = new ReorderFields(); |
42
|
5 |
|
$fieldLabels = $reorderer->reorder($options['fields'], $options['field-labels'], $data); |
43
|
|
|
|
44
|
5 |
|
$tableTransformer = new TableTransformation($data, $fieldLabels); |
45
|
|
|
|
46
|
5 |
|
return $tableTransformer; |
47
|
|
|
} |
48
|
|
|
|
49
|
5 |
|
protected function interpretOptions($configurationData, $options) |
50
|
|
|
{ |
51
|
5 |
|
$configurationData += $this->defaultOptions(); |
52
|
|
|
|
53
|
5 |
|
$configurationData['field-labels'] = PropertyParser::parse($configurationData['field-labels']); |
54
|
5 |
|
$configurationData['default-fields'] = PropertyParser::parse($configurationData['default-fields']); |
55
|
|
|
|
56
|
5 |
|
return $options + $configurationData; |
57
|
|
|
} |
58
|
|
|
|
59
|
5 |
|
protected function defaultOptions() |
60
|
|
|
{ |
61
|
|
|
return [ |
62
|
5 |
|
'fields' => [], |
63
|
5 |
|
'field-labels' => [], |
64
|
5 |
|
'default-fields' => [], |
65
|
5 |
|
]; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|