ArrayTable   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 99
c 0
b 0
f 0
wmc 16
lcom 1
cbo 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A getHeaders() 0 11 3
A getItems() 0 3 1
A getCells() 0 17 4
A selectColumns() 0 8 3
A setHeader() 0 4 1
A setFilter() 0 4 1
1
<?php
2
namespace rtens\domin\delivery\web\renderers\tables\types;
3
4
use rtens\domin\delivery\web\renderers\tables\Table;
5
6
class ArrayTable implements Table {
7
8
    /** @var array */
9
    private $array;
10
11
    /** @var string[] */
12
    private $columns = [];
13
14
    private $headers = [];
15
16
    private $filters = [];
17
18
    public function __construct(array $array) {
19
        $this->array = $array;
20
21
        foreach ($this->array as $item) {
22
            foreach ($item as $key => $value) {
23
                $this->columns[$key] = $key;
24
            }
25
        }
26
    }
27
28
    /**
29
     * @return string[] Header captions
30
     */
31
    public function getHeaders() {
32
        $headers = [];
33
        foreach ($this->columns as $column) {
34
            if (isset($this->headers[$column])) {
35
                $headers[] = $this->headers[$column];
36
            } else {
37
                $headers[] = ucfirst($column);
38
            }
39
        }
40
        return $headers;
41
    }
42
43
    /**
44
     * @return mixed[][] Rows containing the cells
45
     */
46
    public function getItems() {
47
        return $this->array;
48
    }
49
50
    /**
51
     * @param $item
52
     * @return mixed[] The cells of an item
53
     */
54
    public function getCells($item) {
55
        $row = [];
56
        foreach ($this->columns as $column) {
57
            if (array_key_exists($column, $item)) {
58
                $cell = $item[$column];
59
60
                if (isset($this->filters[$column])) {
61
                    $cell = call_user_func($this->filters[$column], $cell);
62
                }
63
64
                $row[] = $cell;
65
            } else {
66
                $row[] = '';
67
            }
68
        }
69
        return $row;
70
    }
71
72
    /**
73
     * @param $columns
74
     * @return static
75
     */
76
    public function selectColumns($columns) {
77
        foreach ($this->columns as $column) {
78
            if (!in_array($column, $columns)) {
79
                unset($this->columns[$column]);
80
            }
81
        }
82
        return $this;
83
    }
84
85
    /**
86
     * @param string $column
87
     * @param string $header
88
     * @return static
89
     */
90
    public function setHeader($column, $header) {
91
        $this->headers[$column] = $header;
92
        return $this;
93
    }
94
95
    /**
96
     * @param string $column
97
     * @param callable $filter
98
     * @return static
99
     */
100
    public function setFilter($column, callable $filter) {
101
        $this->filters[$column] = $filter;
102
        return $this;
103
    }
104
}