Completed
Pull Request — master (#69)
by Greg
05:13
created

UnstructuredListData   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 29
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A restructure() 0 8 1
A processFieldAliases() 0 12 3
1
<?php
2
namespace Consolidation\OutputFormatters\StructuredData;
3
4
use Consolidation\OutputFormatters\Options\FormatterOptions;
5
use Consolidation\OutputFormatters\StructuredData\RestructureInterface;
6
use Consolidation\OutputFormatters\Transformations\UnstructuredDataTransformation;
7
8
/**
9
 * Represents aribtrary unstructured array data where the
10
 * data to display in --list format comes from the array keys.
11
 *
12
 * Unstructured list data can have variable keys in every rown (unlike
13
 * RowsOfFields, which expects uniform rows), and the data elements may
14
 * themselves be deep arrays.
15
 */
16
class UnstructuredListData extends AbstractListData implements RestructureInterface
17
{
18
    public function __construct($data)
19
    {
20
        parent::__construct($data);
21
    }
22
23
    public function restructure(FormatterOptions $options)
24
    {
25
        $data = $this->getArrayCopy();
26
        $defaults = $this->defaultOptions();
27
        $fields = $this->getFields($options, $defaults);
28
29
        return new UnstructuredDataTransformation($data, $this->processFieldAliases($fields));
30
    }
31
32
    protected function processFieldAliases($fields)
33
    {
34
        if (!is_array($fields)) {
35
            $fields = array_filter(explode(',', $fields));
36
        }
37
        $transformed_fields = [];
38
        foreach ($fields as $field) {
39
            list($machine_name,$label) = explode(' as ', $field) + [$field, preg_replace('#.*\.#', '', $field)];
40
            $transformed_fields[$machine_name] = $label;
41
        }
42
        return $transformed_fields;
43
    }
44
}
45