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