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\Traits\CsvHandlerTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class CsvLoader |
18
|
|
|
*/ |
19
|
|
|
class CsvLoader extends FileLoaderAbstract |
20
|
|
|
{ |
21
|
|
|
use CsvHandlerTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var bool |
25
|
|
|
*/ |
26
|
|
|
protected $isFirstLine = true; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* CsvLoader constructor. |
30
|
|
|
* |
31
|
|
|
* @param string $destination |
32
|
|
|
* @param string $delimiter |
33
|
|
|
* @param string $enclosure |
34
|
|
|
* @param string $escape |
35
|
|
|
* |
36
|
|
|
* @throws NodalFlowException |
37
|
|
|
* @throws YaEtlException |
38
|
|
|
*/ |
39
|
|
|
public function __construct($destination, $delimiter = ',', $enclosure = '"', $escape = '"') |
40
|
|
|
{ |
41
|
|
|
parent::__construct($destination); |
42
|
|
|
$this->delimiter = $delimiter; |
43
|
|
|
$this->enclosure = $enclosure; |
44
|
|
|
$this->escape = $escape; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Execute this Node |
49
|
|
|
* |
50
|
|
|
* @param array $param |
51
|
|
|
*/ |
52
|
|
|
public function exec($param) |
53
|
|
|
{ |
54
|
|
|
$this->handleFirstLine($param) |
55
|
|
|
->writeCsvLine($param); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
|
|
public function writeBom() |
62
|
|
|
{ |
63
|
|
|
if ($this->useBom && ($bom = $this->prependBom(''))) { |
64
|
|
|
fwrite($this->handle, $bom); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public function writeSep() |
74
|
|
|
{ |
75
|
|
|
if ($this->useSep) { |
76
|
|
|
fwrite($this->handle, "sep=$this->delimiter" . PHP_EOL); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param array $param |
84
|
|
|
* |
85
|
|
|
* @return $this |
86
|
|
|
*/ |
87
|
|
|
public function writeHeader(array $param) |
88
|
|
|
{ |
89
|
|
|
if ($this->useHeader) { |
90
|
|
|
if (!isset($this->header)) { |
91
|
|
|
$this->header = array_keys($param); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->writeCsvLine($this->header); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param array $record |
102
|
|
|
* |
103
|
|
|
* @return bool|int |
104
|
|
|
*/ |
105
|
|
|
public function writeCsvLine(array $record) |
106
|
|
|
{ |
107
|
|
|
return fputcsv($this->handle, $record, $this->delimiter, $this->enclosure, $this->escape); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array $param |
112
|
|
|
* |
113
|
|
|
* @return $this |
114
|
|
|
*/ |
115
|
|
|
protected function handleFirstLine($param) |
116
|
|
|
{ |
117
|
|
|
if ($this->isFirstLine) { |
118
|
|
|
$this->writeBom() |
119
|
|
|
->writeSep() |
120
|
|
|
->writeHeader($param); |
121
|
|
|
$this->isFirstLine = false; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|