1 | <?php |
||
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() |
||
62 | |||
63 | /** |
||
64 | * @return Format |
||
65 | */ |
||
66 | 9 | public function getFormat() |
|
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() |
|
99 | |||
100 | 6 | public function isWritable() |
|
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() |
||
147 | |||
148 | 4 | public function getHash() |
|
156 | |||
157 | 6 | public function writer() |
|
158 | { |
||
159 | 6 | if (!$this->writer) { |
|
169 | |||
170 | 6 | private function formatToWriter(Format $format, \SplFileInfo $file) |
|
190 | |||
191 | 9 | public function info() |
|
205 | |||
206 | } |
||
207 |