1
|
|
|
<?php |
2
|
|
|
namespace Mathielen\ImportEngine\Storage; |
3
|
|
|
|
4
|
|
|
use Ddeboer\DataImport\Reader\ArrayReader; |
5
|
|
|
use Ddeboer\DataImport\Reader\CsvReader; |
6
|
|
|
use Mathielen\DataImport\Reader\ExcelReader; |
7
|
|
|
use Ddeboer\DataImport\Reader\ReaderInterface; |
8
|
|
|
use Mathielen\DataImport\Writer\CsvWriter; |
9
|
|
|
use Mathielen\DataImport\Writer\ExcelWriter; |
10
|
|
|
use Mathielen\DataImport\Writer\XmlWriter; |
11
|
|
|
use Ddeboer\DataImport\Writer\WriterInterface; |
12
|
|
|
use Mathielen\DataImport\Reader\XmlReader; |
13
|
|
|
use Mathielen\ImportEngine\Storage\Format\CompressedFormat; |
14
|
|
|
use Mathielen\ImportEngine\Storage\Format\Format; |
15
|
|
|
use Mathielen\ImportEngine\Storage\Format\CsvFormat; |
16
|
|
|
use Mathielen\ImportEngine\Storage\Format\ExcelFormat; |
17
|
|
|
use Mathielen\ImportEngine\Storage\Format\JsonFormat; |
18
|
|
|
use Mathielen\ImportEngine\Storage\Format\XmlFormat; |
19
|
|
|
use Mathielen\ImportEngine\Exception\InvalidConfigurationException; |
20
|
|
|
|
21
|
|
|
class LocalFileStorage implements StorageFormatInterface, RecognizableStorageInterface |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \SplFileInfo |
26
|
|
|
*/ |
27
|
|
|
private $file; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Format |
31
|
|
|
*/ |
32
|
|
|
private $format; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ReaderInterface |
36
|
|
|
*/ |
37
|
|
|
private $reader; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var WriterInterface |
41
|
|
|
*/ |
42
|
|
|
private $writer; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var StorageInfo |
46
|
|
|
*/ |
47
|
|
|
private $info; |
48
|
|
|
|
49
|
13 |
|
public function __construct(\SplFileInfo $file, Format $format) |
50
|
|
|
{ |
51
|
13 |
|
$this->file = $file; |
52
|
13 |
|
$this->format = $format; |
53
|
13 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return \SplFileInfo |
57
|
|
|
*/ |
58
|
|
|
public function getFile() |
59
|
|
|
{ |
60
|
|
|
return $this->file; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return Format |
65
|
|
|
*/ |
66
|
9 |
|
public function getFormat() |
67
|
|
|
{ |
68
|
9 |
|
return $this->format; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
public function getAvailableFormats() |
72
|
|
|
{ |
73
|
|
|
return array( |
74
|
1 |
|
'csv', |
75
|
|
|
'excel', |
76
|
|
|
'xml', |
77
|
|
|
'json', |
78
|
|
|
'zlib' |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
11 |
|
public function reader() |
83
|
|
|
{ |
84
|
11 |
|
if (!$this->reader) { |
85
|
11 |
|
if (!$this->isReadable()) { |
86
|
|
|
throw new InvalidConfigurationException('Cannot read from file '.$this->file); |
87
|
|
|
} |
88
|
|
|
|
89
|
11 |
|
$this->reader = $this->formatToReader($this->format, $this->file); |
90
|
|
|
} |
91
|
|
|
|
92
|
11 |
|
return $this->reader; |
93
|
|
|
} |
94
|
|
|
|
95
|
11 |
|
public function isReadable() |
96
|
|
|
{ |
97
|
11 |
|
return $this->file->isReadable() && $this->file->getSize() > 0; |
98
|
|
|
} |
99
|
|
|
|
100
|
6 |
|
public function isWritable() |
101
|
|
|
{ |
102
|
6 |
|
return is_writable(dirname($this->file)); |
103
|
|
|
} |
104
|
|
|
|
105
|
11 |
|
private function formatToReader(Format $format, \SplFileInfo $file) |
106
|
|
|
{ |
107
|
11 |
|
if ($format instanceof CsvFormat) { |
108
|
6 |
|
$reader = new CsvReader($file->openFile(), $format->getDelimiter(), $format->getEnclosure(), $format->getEscape()); |
109
|
6 |
|
$reader->setStrict(false); |
110
|
6 |
|
if ($format->isHeaderInFirstRow()) { |
111
|
6 |
|
$reader->setHeaderRowNumber(0, CsvReader::DUPLICATE_HEADERS_MERGE); |
112
|
6 |
|
$reader->setColumnHeaders(array_map('trim', $reader->getColumnHeaders())); //TODO some header-collaborator? |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
} elseif ($format instanceof ExcelFormat) { |
116
|
4 |
|
$headerRowNumber = $format->isHeaderInFirstRow()?0:null; |
117
|
4 |
|
$reader = new ExcelReader($file->openFile(), $headerRowNumber, $format->getActivesheet()); |
118
|
4 |
|
if ($format->isHeaderInFirstRow()) { |
119
|
4 |
|
$reader->setColumnHeaders(array_map('trim', $reader->getColumnHeaders())); //TODO some header-collaborator? |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
} elseif ($format instanceof JsonFormat) { |
123
|
|
|
$array = json_decode(file_get_contents($file), true); |
124
|
|
|
$reader = new ArrayReader($array); |
125
|
|
|
|
126
|
|
|
} elseif ($format instanceof XmlFormat) { |
127
|
1 |
|
$reader = new XmlReader($file->openFile(), $format->getXpath()); |
128
|
|
|
|
129
|
1 |
|
} elseif ($format instanceof CompressedFormat && $format->getSubFormat()) { |
130
|
1 |
|
$reader = $this->formatToReader($format->getSubFormat(), $format->getInsideStream($file)); |
131
|
|
|
|
132
|
|
|
} else { |
133
|
|
|
throw new InvalidConfigurationException("Cannot build reader. Unknown format: ".$format); |
134
|
|
|
} |
135
|
|
|
|
136
|
11 |
|
return $reader; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function getFields() |
140
|
|
|
{ |
141
|
|
|
if (!$this->isReadable()) { |
142
|
|
|
throw new InvalidConfigurationException("Cannot read from file ".$this->file); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $this->reader()->getFields(); |
146
|
|
|
} |
147
|
|
|
|
148
|
4 |
|
public function getHash() |
149
|
|
|
{ |
150
|
4 |
|
if (!$this->isReadable()) { |
151
|
|
|
throw new InvalidConfigurationException("Cannot read from file ".$this->file); |
152
|
|
|
} |
153
|
|
|
|
154
|
4 |
|
return md5_file($this->file->getRealPath()); |
155
|
|
|
} |
156
|
|
|
|
157
|
6 |
|
public function writer() |
158
|
|
|
{ |
159
|
6 |
|
if (!$this->writer) { |
160
|
6 |
|
if (!$this->isWritable()) { |
161
|
|
|
throw new InvalidConfigurationException('Cannot write to file '.$this->file); |
162
|
|
|
} |
163
|
|
|
|
164
|
6 |
|
$this->writer = $this->formatToWriter($this->format, $this->file); |
165
|
|
|
} |
166
|
|
|
|
167
|
6 |
|
return $this->writer; |
168
|
|
|
} |
169
|
|
|
|
170
|
6 |
|
private function formatToWriter(Format $format, \SplFileInfo $file) |
171
|
|
|
{ |
172
|
6 |
|
if ($format instanceof CsvFormat) { |
173
|
3 |
|
$writer = new CsvWriter($format->getDelimiter(), $format->getEnclosure(), fopen($file, 'a'), false, $format->isHeaderInFirstRow()); |
174
|
|
|
|
175
|
|
|
} elseif ($format instanceof ExcelFormat) { |
176
|
2 |
|
$writer = new ExcelWriter($file->openFile('a'), $format->getActivesheet(), $format->getExceltype(), $format->isHeaderInFirstRow()); |
177
|
|
|
|
178
|
|
|
} elseif ($format instanceof XmlFormat) { |
179
|
1 |
|
$writer = new XMLWriter($file->openFile('a')); |
180
|
|
|
|
181
|
|
|
} elseif ($format instanceof CompressedFormat) { |
182
|
|
|
throw new \LogicException("Not implemented!"); |
183
|
|
|
|
184
|
|
|
} else { |
185
|
|
|
throw new InvalidConfigurationException("Cannot build writer. Unknown format: ".$format); |
186
|
|
|
} |
187
|
|
|
|
188
|
6 |
|
return $writer; |
189
|
|
|
} |
190
|
|
|
|
191
|
9 |
|
public function info() |
192
|
|
|
{ |
193
|
9 |
|
if (!isset($this->info)) { |
194
|
9 |
|
$this->info = new StorageInfo(array( |
195
|
9 |
|
'name' => $this->file->getFilename(), |
196
|
9 |
|
'hash' => $this->isReadable()?$this->getHash():null, |
197
|
9 |
|
'format' => $this->getFormat(), |
198
|
9 |
|
'size' => $this->isReadable()?$this->file->getSize():0, |
199
|
9 |
|
'count' => $this->isReadable()?count($this->reader()):0 |
200
|
|
|
)); |
201
|
|
|
} |
202
|
|
|
|
203
|
9 |
|
return $this->info; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
} |
207
|
|
|
|