|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ilateral\SilverStripe\SlightlyBetterBulkLoader; |
|
4
|
|
|
|
|
5
|
|
|
use League\Csv\Reader; |
|
6
|
|
|
|
|
7
|
|
|
class CustomReader extends Reader |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Should this reader ignore column headers that are null when importing? |
|
11
|
|
|
* |
|
12
|
|
|
* Sometimes Excel adds null headers to the end of a CSV and this can result in the |
|
13
|
|
|
* CSV failing to parse. |
|
14
|
|
|
* |
|
15
|
|
|
* @var boolean |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $ignore_empty_headers = true; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Validates the array to be used by the fetchAssoc method |
|
21
|
|
|
* |
|
22
|
|
|
* @param array $keys |
|
23
|
|
|
* |
|
24
|
|
|
* @throws InvalidArgumentException If the submitted array fails the assertion |
|
25
|
|
|
* |
|
26
|
|
|
* @return array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected function validateKeys(array $keys) |
|
29
|
|
|
{ |
|
30
|
|
|
if ($this->getIgnoreEmptyHeaders()) { |
|
31
|
|
|
$last = $this->findLastHeaderPos($keys); |
|
32
|
|
|
$new_keys = []; |
|
33
|
|
|
|
|
34
|
|
|
for ($i = 0; $i < count($keys); $i++) { |
|
|
|
|
|
|
35
|
|
|
if ($i <= $last) { |
|
36
|
|
|
$new_keys[] = $keys[$i]; |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$keys = $new_keys; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return parent::validateKeys($keys); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Attempt to find the position of the last legitimate |
|
48
|
|
|
* header column (a column that is not null and is the last |
|
49
|
|
|
* item in the list that is not null). |
|
50
|
|
|
* |
|
51
|
|
|
* @param array $keys |
|
52
|
|
|
* |
|
53
|
|
|
* @return int |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function findLastHeaderPos(array $keys) |
|
56
|
|
|
{ |
|
57
|
|
|
$last = 0; |
|
58
|
|
|
$count = count($keys); |
|
59
|
|
|
|
|
60
|
|
|
for ($i = 0; $i < $count; $i++) { |
|
61
|
|
|
$curr = trim($keys[$i]); |
|
62
|
|
|
$j = $i; |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
// If the current field isn't empty, skip |
|
65
|
|
|
if (!empty($curr)) { |
|
66
|
|
|
$last = $i; |
|
67
|
|
|
continue; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// If the column is empty, the previous one isn't |
|
71
|
|
|
// and the next few are or don't exist, assume |
|
72
|
|
|
// we have hit the last item. Set and break. |
|
73
|
|
|
if (empty($curr) && isset($keys[$i-1])) { |
|
74
|
|
|
$temp = 0; |
|
75
|
|
|
for ($j = $i; $j < $count; $j++) { |
|
76
|
|
|
if (!empty($keys[$j])) { |
|
77
|
|
|
$temp = $keys[$j]; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if ($temp > 0) { |
|
82
|
|
|
$last = $i; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $last; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Get the ignore_empty_headers param |
|
92
|
|
|
* |
|
93
|
|
|
* @return boolean |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getIgnoreEmptyHeaders() |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->ignore_empty_headers; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Set the ignore_empty_headers param |
|
102
|
|
|
* |
|
103
|
|
|
* @param boolean $ignore_empty_headers Ignore header keys that are blank? |
|
104
|
|
|
* |
|
105
|
|
|
* @return self |
|
106
|
|
|
*/ |
|
107
|
|
|
public function setIgnoreEmptyHeaders(boolean $ignore_empty_headers) |
|
108
|
|
|
{ |
|
109
|
|
|
$this->ignore_empty_headers = $ignore_empty_headers; |
|
|
|
|
|
|
110
|
|
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: