Completed
Pull Request — master (#69)
by Greg
01:48
created

UnstructuredListData::processFieldAliases()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 4
nop 1
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