1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NwLaravel\Iterators; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Library Result Csv |
7
|
|
|
*/ |
8
|
|
|
class IteratorFileCsv extends AbstractIteratorFile implements IteratorInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array|null |
12
|
|
|
*/ |
13
|
|
|
protected $headers; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $delimiter; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $enclosure; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string|null |
27
|
|
|
*/ |
28
|
|
|
protected $escape; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $defaults = array(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $replace = array(); |
39
|
|
|
|
40
|
|
|
const DELIMITER_DEFAULT = ';'; |
41
|
|
|
const ENCLOSURE_DEFAULT = '"'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Abre o arquivo para leitura e define a variaveis |
45
|
|
|
* |
46
|
|
|
* @param string $fileName File Name |
47
|
|
|
* @param string $headers Headers [optional] |
48
|
|
|
* @param string $delimiter Separator Fields [optional] |
49
|
|
|
* @param string $enclosure Enclosure Fields [optional] |
50
|
|
|
* @param string $escape String de Escape [optional] |
51
|
|
|
* |
52
|
|
|
* @throws \RuntimeException |
53
|
|
|
*/ |
54
|
4 |
|
public function __construct( |
55
|
|
|
$fileName, |
56
|
|
|
array $headers = array(), |
57
|
|
|
$delimiter = null, |
58
|
|
|
$enclosure = null, |
59
|
|
|
$escape = null |
60
|
|
|
) { |
61
|
4 |
|
parent::__construct($fileName); |
62
|
|
|
|
63
|
4 |
|
$this->setHeaders($headers); |
64
|
4 |
|
$this->delimiter = (string) !is_null($delimiter) ? $delimiter : self::DELIMITER_DEFAULT; |
65
|
4 |
|
$this->enclosure = $enclosure ?: self::ENCLOSURE_DEFAULT; |
66
|
4 |
|
$this->escape = $escape ? $escape : null; |
67
|
4 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Line to array |
71
|
|
|
* |
72
|
|
|
* @return array|null |
73
|
|
|
*/ |
74
|
1 |
|
private function rowArray() |
75
|
|
|
{ |
76
|
1 |
|
$row = null; |
77
|
1 |
|
$line = $this->getLine(); |
78
|
|
|
|
79
|
1 |
|
if ($line !== false && !is_null($line)) { |
80
|
1 |
|
$row = str_getcsv($line, $this->delimiter, $this->enclosure, $this->escape); |
81
|
|
|
$row = array_map("trim", $row); |
82
|
1 |
|
} |
83
|
|
|
|
84
|
|
|
return $row; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Make Row Current |
89
|
|
|
* |
90
|
1 |
|
* @return array|bool |
91
|
|
|
*/ |
92
|
1 |
|
public function makeCurrent() |
93
|
|
|
{ |
94
|
|
|
$row = $this->rowArray(); |
95
|
1 |
|
if (is_null($row)) { |
96
|
1 |
|
return false; |
97
|
|
|
} |
98
|
1 |
|
|
99
|
1 |
|
$validateRow = array_filter($row, function ($value) { |
100
|
|
|
return ($value === '0') ? true : !empty($value); |
101
|
|
|
}); |
102
|
1 |
|
|
103
|
1 |
|
if (!count($validateRow)) { |
104
|
|
|
return (array) $validateRow; // Vazio |
105
|
|
|
} |
106
|
1 |
|
|
107
|
1 |
|
$headers = $this->getHeaders(); |
108
|
|
|
$count_headers = count($headers); |
109
|
1 |
|
|
110
|
|
|
// Falta valores, Existe mais Headers |
111
|
1 |
|
if ($count_headers > count($row)) { |
112
|
|
|
$row = array_pad($row, $count_headers, ""); |
113
|
|
|
|
114
|
1 |
|
} else { |
115
|
1 |
|
// Sobrando valores, Existe mais valores |
116
|
|
|
$row = array_slice($row, 0, $count_headers); |
117
|
1 |
|
} |
118
|
|
|
|
119
|
|
|
$row = array_combine($headers, $row); |
120
|
|
|
$row = array_merge($this->defaults, $row, $this->replace); |
121
|
|
|
|
122
|
|
|
return $row; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
1 |
|
* Conta as linhas pulando o cabeçalho, se existir |
127
|
|
|
* |
128
|
1 |
|
* @return integer |
129
|
1 |
|
*/ |
130
|
1 |
|
public function count() |
131
|
1 |
|
{ |
132
|
|
|
if (is_null($this->count)) { |
133
|
|
|
$this->count = parent::count(); |
134
|
|
|
$this->count -= 1; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $this->count; |
138
|
2 |
|
} |
139
|
|
|
|
140
|
2 |
|
/** |
141
|
|
|
* Rewind na segunda linha |
142
|
2 |
|
* |
143
|
2 |
|
* @see FileIterator::rewind() |
144
|
2 |
|
* @return void |
145
|
2 |
|
*/ |
146
|
|
|
public function rewind() |
147
|
2 |
|
{ |
148
|
|
|
parent::rewind(); |
149
|
|
|
$this->key = 1; |
150
|
|
|
$this->next(); // Pular Cabeçalho |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Retorna o headers tendo como chave a posicao das colunas da planilha |
155
|
2 |
|
* |
156
|
|
|
* @return array |
157
|
2 |
|
*/ |
158
|
2 |
|
public function getHeaders() |
159
|
2 |
|
{ |
160
|
|
|
if (is_null($this->headers)) { |
161
|
2 |
|
$tell = ftell($this->fileHandle); |
162
|
2 |
|
fseek($this->fileHandle, 0); |
163
|
2 |
|
$row = $this->rowArray(); |
164
|
2 |
|
$headers = array(); |
165
|
|
|
|
166
|
2 |
|
if (is_array($row)) { |
167
|
|
|
$headers = array_map(function ($title) { |
168
|
2 |
|
return strtolower(str_slug($title, '_')); |
169
|
2 |
|
}, $row); |
170
|
|
|
} |
171
|
2 |
|
|
172
|
|
|
fseek($this->fileHandle, $tell); |
173
|
|
|
$this->headers = $headers; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $this->headers; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Set Headers |
181
|
4 |
|
* |
182
|
|
|
* @param array $headers Headers |
183
|
4 |
|
* |
184
|
4 |
|
* @return IteratorFileCsv |
185
|
|
|
*/ |
186
|
|
|
public function setHeaders(array $headers) |
187
|
|
|
{ |
188
|
|
|
$this->headers = count($headers) ? $headers : null; |
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Set Fields Defaults |
194
|
1 |
|
* |
195
|
|
|
* @param array $defaults Defaults |
196
|
1 |
|
* |
197
|
1 |
|
* @return void |
198
|
|
|
*/ |
199
|
|
|
public function setDefaults(array $defaults) |
200
|
|
|
{ |
201
|
|
|
$this->defaults = $defaults; |
202
|
|
|
return $this; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Set Fields Replace |
207
|
1 |
|
* |
208
|
|
|
* @param array $replace Replaces |
209
|
1 |
|
* |
210
|
1 |
|
* @return void |
211
|
|
|
*/ |
212
|
|
|
public function setReplace(array $replace) |
213
|
|
|
{ |
214
|
|
|
$this->replace = $replace; |
215
|
|
|
return $this; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|