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