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\YaEtl\Traits\CsvHandlerTrait; |
14
|
|
|
use fab2s\YaEtl\YaEtlException; |
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 resource|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, string $delimiter = ',', string $enclosure = '"', string $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 = null) |
53
|
|
|
{ |
54
|
|
|
$this->handleFirstLine($param) |
55
|
|
|
->writeCsvLine($param); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return static |
60
|
|
|
*/ |
61
|
|
|
public function writeSep(): self |
62
|
|
|
{ |
63
|
|
|
if ($this->useSep) { |
64
|
|
|
fwrite($this->handle, "sep=$this->delimiter" . PHP_EOL); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param array $param |
72
|
|
|
* |
73
|
|
|
* @return static |
74
|
|
|
*/ |
75
|
|
|
public function writeHeader(array $param): self |
76
|
|
|
{ |
77
|
|
|
if ($this->useHeader) { |
78
|
|
|
if (!isset($this->header)) { |
79
|
|
|
$this->header = array_keys($param); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->writeCsvLine($this->header); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param array $record |
90
|
|
|
* |
91
|
|
|
* @return bool|int |
92
|
|
|
*/ |
93
|
|
|
public function writeCsvLine(array $record) |
94
|
|
|
{ |
95
|
|
|
return fputcsv($this->handle, $record, $this->delimiter, $this->enclosure, $this->escape); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param array $param |
100
|
|
|
* |
101
|
|
|
* @return static |
102
|
|
|
*/ |
103
|
|
|
protected function handleFirstLine($param): self |
104
|
|
|
{ |
105
|
|
|
if ($this->isFirstLine) { |
106
|
|
|
$this->writeBom() |
107
|
|
|
->writeSep() |
108
|
|
|
->writeHeader($param); |
109
|
|
|
$this->isFirstLine = false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|