1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of YaEtl. |
5
|
|
|
* (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl |
6
|
|
|
* This source file is licensed under the MIT license which you will |
7
|
|
|
* find in the LICENSE file or at https://opensource.org/licenses/MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace fab2s\YaEtl\Loaders\File; |
11
|
|
|
|
12
|
|
|
use fab2s\NodalFlow\NodalFlowException; |
13
|
|
|
use fab2s\NodalFlow\YaEtlException; |
14
|
|
|
use fab2s\YaEtl\Extractors\File\FileExtractorAbstract; |
15
|
|
|
use fab2s\YaEtl\Traits\CsvHandlerTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class CsvExtractor |
19
|
|
|
*/ |
20
|
|
|
class CsvExtractor extends FileExtractorAbstract |
21
|
|
|
{ |
22
|
|
|
use CsvHandlerTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* CsvLoader constructor. |
26
|
|
|
* |
27
|
|
|
* @param resource|string $input |
28
|
|
|
* @param string|null $delimiter |
29
|
|
|
* @param string|null $enclosure |
30
|
|
|
* @param string|null $escape |
31
|
|
|
* |
32
|
|
|
* @throws NodalFlowException |
33
|
|
|
* @throws YaEtlException |
34
|
|
|
*/ |
35
|
|
|
public function __construct($input, $delimiter = null, $enclosure = null, $escape = null) |
36
|
|
|
{ |
37
|
|
|
parent::__construct($input); |
38
|
|
|
$this->initCsvOptions($delimiter, $enclosure, $escape); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param mixed $param |
43
|
|
|
* |
44
|
|
|
* @return \Generator |
45
|
|
|
*/ |
46
|
|
|
public function getTraversable($param = null) |
47
|
|
|
{ |
48
|
|
|
if (!$this->extract($param) || false === ($firstRecord = $this->getFirstRecord())) { |
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$firstRecord = str_getcsv($firstRecord, $this->delimiter, $this->enclosure, $this->escape); |
53
|
|
|
if ($this->useHeader) { |
54
|
|
|
$this->header = $firstRecord; |
55
|
|
|
} else { |
56
|
|
|
yield $firstRecord; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
while (false !== ($record = fgetcsv($this->handle, 0, $this->delimiter, $this->enclosure, $this->escape))) { |
60
|
|
|
if ($this->useHeader) { |
61
|
|
|
$record = array_combine($this->header, $record); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
yield $record; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->releaseHandle(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string|false |
72
|
|
|
*/ |
73
|
|
|
protected function getFirstRecord() |
74
|
|
|
{ |
75
|
|
|
while (false !== ($line = fgets($this->handle))) { |
|
|
|
|
76
|
|
|
if ($line = $this->trimBom(trim($line))) { |
77
|
|
|
// obey excel sep |
78
|
|
|
if (strpos($line, 'sep=') === 0) { |
79
|
|
|
$this->useSep = true; |
80
|
|
|
$this->delimiter = $line[4]; |
81
|
|
|
continue; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $line; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param array $record |
93
|
|
|
* |
94
|
|
|
* @return bool|int |
95
|
|
|
*/ |
96
|
|
|
protected function writeCsvLine(array $record) |
97
|
|
|
{ |
98
|
|
|
return fputcsv($this->handle, $record, $this->delimiter, $this->enclosure, $this->escape); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|