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)) { |
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (false !== ($firstRecord = $this->getFirstRecord())) { |
|
|
|
|
53
|
|
|
/* @var array $firstRecord */ |
54
|
|
|
yield $this->bakeRecord($firstRecord); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
while (false !== ($record = fgetcsv($this->handle, 0, $this->delimiter, $this->enclosure, $this->escape))) { |
58
|
|
|
/* @var array $record */ |
59
|
|
|
if (isset($this->header)) { |
60
|
|
|
$record = array_combine($this->header, $record); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
yield $this->bakeRecord($record); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->releaseHandle(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param array $record |
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
protected function bakeRecord(array $record) |
75
|
|
|
{ |
76
|
|
|
return isset($this->header) ? array_combine($this->header, $record) : $record; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return string|false |
81
|
|
|
*/ |
82
|
|
|
protected function getFirstRecord() |
83
|
|
|
{ |
84
|
|
|
$useHeader = $this->useHeader; |
85
|
|
|
while (false !== ($line = fgets($this->handle))) { |
86
|
|
|
if ($line = trim($this->trimBom($line))) { |
87
|
|
|
// obey excel sep |
88
|
|
|
if (strpos($line, 'sep=') === 0) { |
89
|
|
|
$this->useSep = true; |
90
|
|
|
$this->delimiter = $line[4]; |
91
|
|
|
continue; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$record = str_getcsv($line, $this->delimiter, $this->enclosure, $this->escape); |
95
|
|
|
if ($useHeader) { |
96
|
|
|
$this->header = $record; |
97
|
|
|
$useHeader = false; |
98
|
|
|
continue; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $record; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param array $record |
110
|
|
|
* |
111
|
|
|
* @return bool|int |
112
|
|
|
*/ |
113
|
|
|
protected function writeCsvLine(array $record) |
114
|
|
|
{ |
115
|
|
|
return fputcsv($this->handle, $record, $this->delimiter, $this->enclosure, $this->escape); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|