1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* MIT License |
5
|
|
|
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace SprykerEco\Zed\Econda\Business\Exporter\Writer\File\Adapter; |
9
|
|
|
|
10
|
|
|
use SplFileObject; |
11
|
|
|
use SprykerEco\Zed\Econda\Business\Exporter\Exception\FileWriterException; |
12
|
|
|
|
13
|
|
|
class CsvAdapter implements AdapterInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $folderPath; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $fileName; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $delimiter; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $enclosure; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $escape; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var \SplFileObject|null |
42
|
|
|
*/ |
43
|
|
|
protected $csvFile; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $directory |
47
|
|
|
* @param string $delimiter |
48
|
|
|
* @param string $enclosure |
49
|
|
|
* @param string $escape |
50
|
|
|
*/ |
51
|
|
|
public function __construct($directory, $delimiter = ',', $enclosure = '"', $escape = '\\') |
52
|
|
|
{ |
53
|
|
|
$this->folderPath = $directory; |
54
|
|
|
|
55
|
|
|
$this->delimiter = $delimiter; |
56
|
|
|
$this->enclosure = $enclosure; |
57
|
|
|
$this->escape = $escape; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array $data |
62
|
|
|
* @param string $type |
63
|
|
|
* |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
public function write(array $data, $type = ''): bool |
67
|
|
|
{ |
68
|
|
|
$result = false; |
69
|
|
|
$csvFile = $this->getCsvFile($data); |
70
|
|
|
foreach ($data as $key => $row) { |
71
|
|
|
$result = $csvFile->fputcsv($row, $this->delimiter) !== false ? true : false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $result; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $folderPath |
79
|
|
|
* |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
|
|
public function setFolderPath($folderPath) |
83
|
|
|
{ |
84
|
|
|
$this->folderPath = $folderPath; |
85
|
|
|
$this->csvFile = null; |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $fileName |
92
|
|
|
* |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
|
|
public function setFileName($fileName) |
96
|
|
|
{ |
97
|
|
|
$this->fileName = $fileName; |
98
|
|
|
$this->csvFile = null; |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param \SplFileObject $csvFile |
105
|
|
|
* @param array $dataRow |
106
|
|
|
* |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
|
|
protected function initializeHeaderColumns(SplFileObject $csvFile, array $dataRow): void |
110
|
|
|
{ |
111
|
|
|
$csvFile->fputcsv(array_keys($dataRow), $this->delimiter); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param array $data |
116
|
|
|
* |
117
|
|
|
* @return \SplFileObject |
118
|
|
|
*/ |
119
|
|
|
protected function getCsvFile(array $data): SplFileObject |
120
|
|
|
{ |
121
|
|
|
if (!$this->csvFile) { |
122
|
|
|
$this->csvFile = new SplFileObject($this->getAbsolutePath(), 'w'); |
123
|
|
|
$this->csvFile->setCsvControl($this->delimiter, $this->enclosure, $this->escape); |
124
|
|
|
|
125
|
|
|
$this->initializeHeaderColumns($this->csvFile, current($data)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $this->csvFile; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @throws \SprykerEco\Zed\Econda\Business\Exporter\Exception\FileWriterException |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
protected function getAbsolutePath(): string |
137
|
|
|
{ |
138
|
|
|
if (!$this->folderPath) { |
139
|
|
|
throw new FileWriterException('Path to export file to not set properly'); |
140
|
|
|
} |
141
|
|
|
if (!$this->fileName) { |
142
|
|
|
throw new FileWriterException('File name to export to not set properly'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$absolutePath = $this->folderPath . DIRECTORY_SEPARATOR . $this->fileName; |
146
|
|
|
|
147
|
|
|
return $absolutePath; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|