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