-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReader.php
More file actions
333 lines (299 loc) · 6.88 KB
/
Copy pathReader.php
File metadata and controls
333 lines (299 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
namespace VectorJSON;
class Reader implements \Iterator{
private $file;
private $gz;
private $resource;
private $alias;
private $use_main_array=true;
/**
* @var int
*/
private $key=-1;
private $current;
private $assoc=false;
private $depth=512;
private $flags=0;
private $offset=0;
private function __construct($resource){
$this->resource=$resource;
}
public function __destruct(){
$this->close();
}
/**
* Establece una lista de alias para cambiar los nombres de columnas a la vez que filtra el registro devuelto por {@see Reader::current()} y {@see Reader::readChunk()}
* @param array|null $alias Si es null, no se aplican los alias, y todas las columnas originales son devueltas.<br>
* Las llaves son los números de columna original, y los valores serán los nuevos nombres en el resultado.
* Ejemplo: <code>
* array(
* 0=>'Nombre',
* 1=>'Apellido',
* 3=>0,
* 4=>4,
* 6=>null
* )
* </code>
* En este caso:<ul>
* <li>Las columnas 2, 5 y las mayores a 6 se excluyen.</li>
* <li>Las columnas 0 y 1 serán renombradas a 'Nombre' y 'Apellido', respectivamente.</li>
* <li>La columna 3 pasará a ser la columna 0 del resultado.</li>
* <li>La columna 4 y 6 conservarán su nombre/número.</li>
* </ul>
* El valor resultante será del mismo tipo que el original, es decir, el resultado seguirá siendo objet o array si el original lo era
*/
public function alias(?array $alias=null){
if($alias){
foreach($alias as $k=>&$v){
if(is_null($v)) $v=$k;
}
}
$this->alias=$alias;
}
public function getAlias(){
return $this->alias;
}
/**
* @return bool
*/
public function isUseMainArray(){
return $this->use_main_array;
}
/**
* @param bool $use_main_array
*/
public function setUseMainArray(bool $use_main_array){
$this->use_main_array=$use_main_array;
}
/**
* Ver parámetro de {@see json_decode()}
* @return bool
*/
public function isAssoc(){
return $this->assoc;
}
/**
* Ver parámetro de {@see json_decode()}
* @return int
*/
public function getDepth(){
return $this->depth;
}
/**
* Ver parámetro de {@see json_decode()}
* @return int
*/
public function getFlags(){
return $this->flags;
}
/**
* Ver parámetro de {@see json_decode()}
* @param bool $assoc
*/
public function setAssoc(bool $assoc){
$this->assoc=$assoc;
}
/**
* Ver parámetro de {@see json_decode()}
* @param int $depth
*/
public function setDepth(int $depth){
$this->depth=$depth;
}
/**
* Ver parámetro de {@see json_decode()}
* @param int $flags
*/
public function setFlags(int $flags){
$this->flags=$flags;
}
public function offset(int $offset=0){
$this->offset=max(0, $offset);
}
/**
* @return int
*/
public function getOffset(){
return $this->offset;
}
public function reopen(){
if($this->ready()) return false;
if($this->file){
if($this->gz){
if($resource=gzopen($this->file, 'rb')){
$this->resource=$resource;
return true;
}
}
else{
if($resource=fopen($this->file, 'r')){
$this->resource=$resource;
return true;
}
}
}
return false;
}
/**
* @param $path
* @return Reader|null
*/
public static function open($path){
if(!($resource=fopen($path, 'r'))) return null;
$t=new self($resource);
$t->file=$path;
$t->gz=false;
return $t;
}
/**
* @param $path
* @return Reader|null
*/
public static function open_gz($path){
if(!($resource=gzopen($path, 'rb'))) return null;
$t=new self($resource);
$t->file=$path;
$t->gz=true;
return $t;
}
/**
* ##ADVERTENCIA
* Este recurso no se cierra automáticamenta al destruir el objeto, ni llamando a {@see Reader::close()}
* @param resource $resource
* @return Reader|null
*/
public static function open_stream($resource){
if(!is_resource($resource) || get_resource_type($resource)!='stream' || !stream_get_meta_data($resource)) return null;
$t=new self($resource);
return $t;
}
public function metadata(){
return stream_get_meta_data($this->resource);
}
public function ready(){
return is_resource($this->resource) && get_resource_type($this->resource)=='stream';
}
public function close(){
$closed=($this->file && $this->ready()?fclose($this->resource):false);
$this->resource=null;
$this->key=-1;
$this->current=null;
return $closed;
}
/**
* Obtiene el registro actual, pero no utiliza los alias de columna (ver {@see CSVIterator::alias()})
* @return array|null
*/
public function current_original(){
$current=json_decode($this->current, $this->assoc, $this->depth, $this->flags);
return $current;
}
/**
* Omite la cantidad de registros indicada
* @param int $cant
* @return int Cantidad de registros omitidos. Si llega al final, este valor será diferente a la indicada
*/
public function skip(int $cant){
$i=0;
while($this->valid() && $i<$cant){
$this->next();
++$i;
}
return $i;
}
/**
* Obtiene un conjunto de lineas del CSV
* @param int $size Tamaño máximo del conjunto
* @return array Matriz de datos
* @see CSVIterator::alias()
*/
public function readChunk(int $size){
$res=[];
for($i=0; $i<$size; ++$i){
if(!$this->valid()) break;
$res[$this->key()]=$this->current();
$this->next();
}
return $res;
}
/**
* @return mixed
*/
public function current(){
$current=$this->current_original();
if($this->use_main_array && is_object($current)) $current=get_object_vars($current);
if(is_array($this->alias)){
$current=self::parseAlias($this->alias, $current);
}
return $current;
}
public function next(){
++$this->key;
$this->current=fgets($this->resource);
}
/**
* @return int
*/
public function key(){
return $this->key;
}
public function valid(){
return is_string($this->current) && strlen($this->current)>0;
}
protected function reset(?int $offset){
if(ftell($this->resource)!==0){
if(fseek($this->resource, 0)!==0){
$this->close();
if(!$this->reopen()) return;
}
}
$this->key=-1;
$this->current=null;
if(!is_null($offset)){
$this->next();
$this->skip($offset-$this->key);
}
}
protected function _reset($offset){
if($this->key==$offset) return;
if($this->key>$offset){
if(fseek($this->resource, 0)===0){
$this->key=-1;
}
else{
$this->close();
return;
}
}
if($this->key<0){
$this->current=null;
$this->next();
}
$this->skip($offset-$this->key);
}
public function rewind(){
$this->reset($this->offset??0);
}
/**
* @param array $alias
* @param array|object $value
* @return array|\stdClass
*/
public static function &parseAlias(array &$alias, &$value){
if(is_array($value)){
$new=[];
foreach($alias AS $k=>&$v){
if(isset($value[$k])) $new[$v]=&$value[$k];
}
return $new;
}
elseif(is_object($value)){
$new=new \stdClass();
foreach($alias AS $k=>&$v){
if(isset($value[$k])) $new->$v=&$value[$k];
}
return $new;
}
return $value;
}
}