Completed
Pull Request — master (#11)
by Greg
02:06
created

TableTransformation   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 20
c 3
b 0
f 2
lcom 1
cbo 0
dl 0
loc 97
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A setLayout() 0 4 1
A getLayout() 0 4 1
A isList() 0 4 1
A transformRows() 0 8 2
A transformRow() 0 8 3
A getHeaders() 0 4 1
A getHeader() 0 7 2
A getData() 0 11 3
A getListData() 0 10 3
A getRowDataWithKey() 0 11 2
1
<?php
2
namespace Consolidation\OutputFormatters\Transformations;
3
4
class TableTransformation extends \ArrayObject
5
{
6
    protected $headers;
7
    protected $layout;
8
9
    const TABLE_LAYOUT = 'table';
10
    const LIST_LAYOUT = 'list';
11
12
    public function __construct($data, $fieldLabels)
13
    {
14
        $this->headers = $fieldLabels;
15
        $rows = static::transformRows($data, $fieldLabels);
16
        $this->layout = self::TABLE_LAYOUT;
17
        parent::__construct($rows);
18
    }
19
20
    public function setLayout($layout)
21
    {
22
        $this->layout = $layout;
23
    }
24
25
    public function getLayout()
26
    {
27
        return $this->layout;
28
    }
29
30
    public function isList()
31
    {
32
        return $this->layout == self::LIST_LAYOUT;
33
    }
34
35
    protected static function transformRows($data, $fieldLabels)
36
    {
37
        $rows = [];
38
        foreach ($data as $row) {
39
            $rows[] = static::transformRow($row, $fieldLabels);
40
        }
41
        return $rows;
42
    }
43
44
    protected static function transformRow($row, $fieldLabels)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        $result = [];
47
        foreach ($fieldLabels as $key => $label) {
48
            $result[$key] = array_key_exists($key, $row) ? $row[$key] : '';
49
        }
50
        return $result;
51
    }
52
53
    public function getHeaders()
54
    {
55
        return $this->headers;
56
    }
57
58
    public function getHeader($key)
59
    {
60
        if (array_key_exists($key, $this->headers)) {
61
            return $this->headers[$key];
62
        }
63
        return $key;
64
    }
65
66
    public function getData($includeRowKey = false)
67
    {
68
        $data = $this->getArrayCopy();
69
        if ($this->isList()) {
70
            $data = $this->getListData();
71
        }
72
        if ($includeRowKey) {
73
            $data = $this->getRowDataWithKey($data);
74
        }
75
        return $data;
76
    }
77
78
    protected function getListData()
79
    {
80
        $result = [];
81
        foreach ($this as $row) {
82
            foreach ($row as $key => $value) {
83
                $result[$key][] = $value;
84
            }
85
        }
86
        return $result;
87
    }
88
89
    protected function getRowDataWithKey($data)
90
    {
91
        $result = [];
92
        $i = 0;
93
        foreach ($data as $key => $row) {
94
            array_unshift($row, $this->getHeader($key));
95
            $i++;
96
            $result[$key] = $row;
97
        }
98
        return $result;
99
    }
100
}
101