|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NwLaravel\Iterators; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Abstract interação de arquivos, implementa o iterator para percorrer as linhas do arquivo |
|
7
|
|
|
*/ |
|
8
|
|
|
abstract class AbstractIteratorFile implements \Iterator, \Countable |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $fileName; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var resource |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $fileHandle; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var mixed |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $current; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var int |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $key; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var int |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $count; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Make Row Current |
|
37
|
|
|
* |
|
38
|
|
|
* @return mixed |
|
39
|
|
|
*/ |
|
40
|
|
|
abstract protected function makeCurrent(); |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Abre o arquivo para leitura |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $fileName Path |
|
46
|
|
|
* |
|
47
|
|
|
* @throws \RuntimeException |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct($fileName) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->fileName = (string) $fileName; |
|
52
|
|
|
|
|
53
|
|
|
if (!file_exists($fileName) || !$this->fileHandle = fopen($fileName, 'r')) { |
|
54
|
|
|
throw new \RuntimeException('Couldn\'t open file "' . $fileName . '"'); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Contagem de linhas |
|
60
|
|
|
* |
|
61
|
|
|
* @return int |
|
62
|
|
|
*/ |
|
63
|
|
|
public function count() |
|
64
|
|
|
{ |
|
65
|
|
|
if (is_null($this->count)) { |
|
66
|
|
|
$this->count = 0; |
|
67
|
|
|
|
|
68
|
|
|
// Salva a posição do ponteiro de leitura |
|
69
|
|
|
$tell = ftell($this->fileHandle); |
|
70
|
|
|
rewind($this->fileHandle); |
|
71
|
|
|
|
|
72
|
|
|
while (!feof($this->fileHandle) && false !== fgets($this->fileHandle)) { |
|
73
|
|
|
$this->count += 1; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// Restabelece a posição do ponteiro |
|
77
|
|
|
fseek($this->fileHandle, $tell); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $this->count; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Inicia a leitura do arquivo do inicio |
|
85
|
|
|
* |
|
86
|
|
|
* @return void |
|
87
|
|
|
*/ |
|
88
|
|
|
public function rewind() |
|
89
|
|
|
{ |
|
90
|
|
|
rewind($this->fileHandle); |
|
91
|
|
|
$this->key = -1; |
|
92
|
|
|
$this->next(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Retorna o indice atual |
|
97
|
|
|
* |
|
98
|
|
|
* @return int |
|
99
|
|
|
*/ |
|
100
|
|
|
public function key() |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->key; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Valida se havera proxima linha |
|
107
|
|
|
* |
|
108
|
|
|
* @return bool |
|
109
|
|
|
*/ |
|
110
|
|
|
public function valid() |
|
111
|
|
|
{ |
|
112
|
|
|
return ($this->current !== false && !is_null($this->current)); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Retorna a linha atual |
|
117
|
|
|
* |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
|
|
public function current() |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->current; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Busca a proxima linha |
|
127
|
|
|
* |
|
128
|
|
|
* @Make return void |
|
129
|
|
|
*/ |
|
130
|
|
|
public function next() |
|
131
|
|
|
{ |
|
132
|
|
|
$this->current = $this->makeCurrent(); |
|
133
|
|
|
$this->key++; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Le alinha no arquivo, formata o encodig caso seja necessario |
|
138
|
|
|
* |
|
139
|
|
|
* @return string |
|
140
|
|
|
*/ |
|
141
|
|
|
protected function getLine() |
|
142
|
|
|
{ |
|
143
|
|
|
$encoding = 'UTF-8'; |
|
144
|
|
|
$line = false; |
|
145
|
|
|
|
|
146
|
|
|
if (! feof($this->fileHandle)) { |
|
147
|
|
|
$line = fgets($this->fileHandle); |
|
148
|
|
|
|
|
149
|
|
|
if ($line !== false) { |
|
150
|
|
|
$line = trim($line); |
|
151
|
|
|
if (mb_detect_encoding($line, $encoding, true) != $encoding) { |
|
152
|
|
|
$line = utf8_encode($line); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return $line; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Ao destruir fecha o arquivo |
|
162
|
|
|
* |
|
163
|
|
|
* @return void |
|
164
|
|
|
*/ |
|
165
|
|
|
public function __destruct() |
|
166
|
|
|
{ |
|
167
|
|
|
if (is_resource($this->fileHandle)) { |
|
168
|
|
|
fclose($this->fileHandle); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|