Completed
Push — master ( 707b28...82d13a )
by Andrew
02:12
created

Csv::reKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace CsvParser;
4
5
class Csv
6
{
7
    protected $data;
8
9 17
    public function __construct($array)
10
    {
11 17
        $this->data = $array;
12 17
    }
13
14 14
    public function getData()
15
    {
16 14
        return $this->data;
17
    }
18
19 1
    public function getRowCount()
20
    {
21 1
        return count($this->data);
22
    }
23
24
    public function first()
25
    {
26
        return current($this->data);
27
    }
28
29
    public function getKeys()
30
    {
31
        return array_keys($this->first());
32
    }
33
34
    /**
35
     * ->reKey allows you to reorganise the keys if they may be out of order as when we write to csv we do it in the order given
36
     * e.g. if you build up an array, adding new columns/keys as you go down the multi levels, so long as you push them into the keys passed here or leave blank and use the first row
37
     */
38
    public function reKey($keys = null)
39
    {
40
        if (! $keys) {
41
            $keys = $this->getKeys();
42
        }
43
        $template = array_map(function () { return ''; }, array_flip($keys));
44
        $this->mapRows(function ($row) use ($template) {
45
            return array_replace($template, $row);
46
        });
47
    }
48
49 2
    public function appendRow($row)
50
    {
51 2
        array_unshift($this->data, $row);
52 2
    }
53
54 2
    public function prependRow($row)
55
    {
56 2
        $this->data[] = $row;
57 2
    }
58
59 2
    public function columnExists($column)
60
    {
61 2
        return isset($this->data[0][$column]);
62
    }
63
64 1
    public function mapColumn($column, $callback)
65
    {
66 1
        if ( ! $this->columnExists($column)) {
67
            throw new Exception('Column does not exist');
68
        }
69 1
        foreach ($this->data as $i => $row) {
70 1
            $this->data[$i][$column] = $callback($row[$column]);
71
        }
72 1
    }
73
74 1
    public function mapRows($callback)
75
    {
76 1
        $this->data = array_map($callback, $this->data);
77 1
    }
78
79 3
    public function filterRows($callback)
80
    {
81 3
        $this->data = array_filter($this->data, $callback);
82 3
    }
83
84
    public function addColumn($column, $default='')
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $default is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
85
    {
86
        // TODO
87
    }
88
89
    public function removeColumn($column)
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
    {
91
        // TODO
92
    }
93
94
    public function removeRowByIndex($index)
0 ignored issues
show
Unused Code introduced by
The parameter $index is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
95
    {
96
        // TODO
97
    }
98
99
    public function removeRow($col, $val)
0 ignored issues
show
Unused Code introduced by
The parameter $col is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $val is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
    {
101
        // TODO
102
    }
103
104
    public function removeRows($rows)
0 ignored issues
show
Unused Code introduced by
The parameter $rows is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
105
    {
106
        // TODO
107
    }
108
109
    public function reorderColumn($col, $index)
0 ignored issues
show
Unused Code introduced by
The parameter $col is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $index is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
110
    {
111
        // TODO
112
    }
113
114
    public function reorderColumns($rows)
0 ignored issues
show
Unused Code introduced by
The parameter $rows is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
115
    {
116
        // TODO
117
    }
118
119
    public function reorderRow($col, $val, $index)
0 ignored issues
show
Unused Code introduced by
The parameter $col is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $val is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $index is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
120
    {
121
        // TODO
122
    }
123
124
    public function reorderRows($rows)
0 ignored issues
show
Unused Code introduced by
The parameter $rows is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
125
    {
126
        // TODO
127
    }
128
129
    public function reorderRowsByColumn($column, $type='asc')
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
130
    {
131
        // TODO
132
    }
133
134
    public function reorderRowsByColumns($columns)
0 ignored issues
show
Unused Code introduced by
The parameter $columns is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
135
    {
136
        // TODO
137
    }
138
139 1
    public function removeDuplicates($column)
140
    {
141 1
        $index = array();
142
        $this->filterRows(function ($row) use ($column, &$index) {
143 1
            if (in_array($row[$column], $index)) {
144 1
                return false;
145
            }
146 1
            $index[] = $row[$column];
147 1
            return true;
148 1
        });
149 1
    }
150
151
    public function removeBlanks($column)
152
    {
153 1
        $this->filterRows(function ($row) use ($column) {
154 1
            if ( ! isset($row[$column]) || trim($row[$column])==='') {
155 1
                return false;
156
            }
157 1
            return true;
158 1
        });
159 1
    }
160
}
161