1
|
|
|
<?php |
2
|
|
|
namespace Consolidation\OutputFormatters\StructuredData; |
3
|
|
|
|
4
|
|
|
use Consolidation\OutputFormatters\RestructureInterface; |
5
|
|
|
use Consolidation\OutputFormatters\Transformations\PropertyParser; |
6
|
|
|
use Consolidation\OutputFormatters\Transformations\ReorderFields; |
7
|
|
|
use Consolidation\OutputFormatters\Transformations\TableTransformation; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Holds an array where each element of the array is one row, |
11
|
|
|
* and each row contains an associative array where the keys |
12
|
|
|
* are the field names, and the values are the field data. |
13
|
|
|
* |
14
|
|
|
* It is presumed that every row contains the same keys. |
15
|
|
|
*/ |
16
|
|
|
class RowsOfFields extends \ArrayObject implements RestructureInterface |
17
|
|
|
{ |
18
|
|
|
protected $data; |
19
|
|
|
|
20
|
|
|
public function __construct($data) |
21
|
|
|
{ |
22
|
|
|
parent::__construct($data); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function restructure($configurationData, $options) |
26
|
|
|
{ |
27
|
|
|
$options = $this->interpretOptions($configurationData, $options); |
28
|
|
|
|
29
|
|
|
$reorderer = new ReorderFields(); |
30
|
|
|
$data = $this->getArrayCopy(); |
31
|
|
|
|
32
|
|
|
$fieldLabels = $reorderer->reorder($options['fields'], $options['field-labels'], $data); |
33
|
|
|
|
34
|
|
|
$tableTransformer = new TableTransformation($data, $fieldLabels); |
35
|
|
|
|
36
|
|
|
return $tableTransformer; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function interpretOptions($configurationData, $options) |
40
|
|
|
{ |
41
|
|
|
$configurationData += $this->defaultOptions(); |
42
|
|
|
|
43
|
|
|
$configurationData['field-labels'] = PropertyParser::parse($configurationData['field-labels']); |
44
|
|
|
$configurationData['default-fields'] = PropertyParser::parse($configurationData['default-fields']); |
45
|
|
|
|
46
|
|
|
return $options + $configurationData; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function defaultOptions() |
50
|
|
|
{ |
51
|
|
|
return [ |
52
|
|
|
'fields' => [], |
53
|
|
|
'field-labels' => [], |
54
|
|
|
'default-fields' => [], |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|