AbstractListData::getFields()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.3222
c 0
b 0
f 0
cc 5
nc 5
nop 2
1
<?php
2
namespace Consolidation\OutputFormatters\StructuredData;
3
4
use Consolidation\OutputFormatters\Options\FormatterOptions;
5
use Consolidation\OutputFormatters\Transformations\ReorderFields;
6
7
/**
8
 * Base class for all list data types.
9
 */
10
class AbstractListData extends \ArrayObject implements ListDataInterface
11
{
12
    public function __construct($data)
13
    {
14
        parent::__construct($data);
15
    }
16
17
    public function getListData(FormatterOptions $options)
18
    {
19
        return array_keys($this->getArrayCopy());
20
    }
21
22
    protected function getReorderedFieldLabels($data, $options, $defaults)
23
    {
24
        $reorderer = new ReorderFields();
25
        $fieldLabels = $reorderer->reorder(
26
            $this->getFields($options, $defaults),
27
            $options->get(FormatterOptions::FIELD_LABELS, $defaults),
28
            $data
29
        );
30
        return $fieldLabels;
31
    }
32
33
    protected function getFields($options, $defaults)
34
    {
35
        $fieldShortcut = $options->get(FormatterOptions::FIELD);
36
        if (!empty($fieldShortcut)) {
37
            return [$fieldShortcut];
38
        }
39
        $result = $options->get(FormatterOptions::FIELDS);
40
        if (!empty($result)) {
41
            return $result;
42
        }
43
        $isHumanReadable = $options->get(FormatterOptions::HUMAN_READABLE);
44
        if ($isHumanReadable) {
45
            $result = $options->get(FormatterOptions::DEFAULT_TABLE_FIELDS);
46
            if (!empty($result)) {
47
                return $result;
48
            }
49
        }
50
        return $options->get(FormatterOptions::DEFAULT_FIELDS, $defaults);
51
    }
52
53
    /**
54
     * A structured list may provide its own set of default options. These
55
     * will be used in place of the command's default options (from the
56
     * annotations) in instances where the user does not provide the options
57
     * explicitly (on the commandline) or implicitly (via a configuration file).
58
     *
59
     * @return array
60
     */
61
    protected function defaultOptions()
62
    {
63
        return [
64
            FormatterOptions::FIELDS => [],
65
            FormatterOptions::FIELD_LABELS => [],
66
        ];
67
    }
68
}
69