Mark V
1: <?php
2: namespace MOC\V\Component\Document\Component\Bridge\Repository\PhpExcel;
3:
4: use MOC\V\Component\Document\Component\Bridge\Repository\PhpExcel;
5: use MOC\V\Component\Document\Component\Exception\Repository\TypeFileException;
6: use MOC\V\Component\Document\Component\Parameter\Repository\FileParameter;
7:
8: /**
9: * Class File
10: *
11: * @package MOC\V\Component\Document\Component\Bridge\Repository\PhpExcel
12: */
13: abstract class File extends Config
14: {
15:
16: /**
17: * @param FileParameter $Location
18: * @param \PHPExcel_Cell_IValueBinder $ValueBinder
19: *
20: * @return PhpExcel
21: */
22: public function newFile(FileParameter $Location, \PHPExcel_Cell_IValueBinder $ValueBinder = null)
23: {
24:
25: $this->setFileParameter($Location);
26: $this->setConfiguration($ValueBinder);
27: $this->Source = new \PHPExcel();
28: return $this;
29: }
30:
31: /**
32: * @param FileParameter $Location
33: * @param \PHPExcel_Cell_IValueBinder $ValueBinder
34: *
35: * @return PhpExcel
36: * @throws TypeFileException
37: * @throws \PHPExcel_Reader_Exception
38: */
39: public function loadFile(FileParameter $Location, \PHPExcel_Cell_IValueBinder $ValueBinder = null)
40: {
41:
42: $this->setFileParameter($Location);
43: $this->setConfiguration($ValueBinder);
44:
45: $Info = $Location->getFileInfo();
46: $ReaderType = $this->getReaderType($Info);
47:
48: if ($ReaderType) {
49: /** @var \PHPExcel_Reader_IReader|\PHPExcel_Reader_CSV $Reader */
50: $Reader = \PHPExcel_IOFactory::createReader($ReaderType);
51: /**
52: * Find CSV Delimiter
53: */
54: if ('CSV' == $ReaderType) {
55: $Result = $this->getDelimiterType();
56: if ($Result) {
57: $Reader->setDelimiter($Result);
58: }
59: }
60: $this->Source = $Reader->load($Location->getFile());
61: } else {
62: throw new TypeFileException('No Reader for '.$Info->getExtension().' available!');
63: }
64: return $this;
65: }
66:
67: /**
68: * @param \SplFileInfo $Info
69: *
70: * @return string
71: */
72: private function getReaderType(\SplFileInfo $Info)
73: {
74:
75: $ReaderList = array(
76: 'Excel2007' => array(
77: 'xlsx',
78: 'xlsm',
79: 'xltx',
80: 'xltm'
81: ),
82: 'Excel5' => array(
83: 'xls',
84: 'xlt'
85: ),
86: 'OOCalc' => array(
87: 'ods',
88: 'ots'
89: ),
90: 'SYLK' => array(
91: 'slk'
92: ),
93: 'Excel2003XML' => array(
94: 'xml'
95: ),
96: 'Gnumeric' => array(
97: 'gnumeric'
98: ),
99: 'HTML' => array(
100: 'htm',
101: 'html'
102: ),
103: 'CSV' => array(
104: 'txt',
105: 'csv'
106: )
107: );
108:
109: $ReaderType = null;
110: $Extension = $Info->getExtension();
111: array_walk($ReaderList, function ($TypeList, $Reader) use (&$ReaderType, $Extension) {
112:
113: if (in_array($Extension, $TypeList)) {
114: $ReaderType = $Reader;
115: }
116: });
117: return $ReaderType;
118: }
119:
120: /**
121: * @return bool|string
122: */
123: private function getDelimiterType()
124: {
125:
126: $Delimiter = array(
127: ',',
128: ';',
129: "\t"
130: );
131: $Result = array();
132: $Content = file($this->getFileParameter());
133: for ($Line = 0; $Line < 5; $Line++) {
134: if (isset( $Content[$Line] )) {
135: foreach ($Delimiter as $Char) {
136: $Result[$Char][$Line] = substr_count($Content[$Line], $Char);
137: }
138: }
139: }
140: array_walk($Result, function ($Count, $Delimiter) use (&$Result) {
141:
142: if (0 == array_sum($Count)) {
143: $Result[$Delimiter] = false;
144: } else {
145: $Count = array_unique($Count);
146: if (1 == count($Count)) {
147: $Result[$Delimiter] = true;
148: } else {
149: $Result[$Delimiter] = false;
150: }
151: }
152: });
153: $Result = array_filter($Result);
154: if (1 == count($Result)) {
155: return key($Result);
156: } else {
157: return false;
158: }
159: }
160:
161: /**
162: * @param null|FileParameter $Location
163: *
164: * @return PhpExcel
165: * @throws TypeFileException
166: * @throws \PHPExcel_Reader_Exception
167: */
168: public function saveFile(FileParameter $Location = null)
169: {
170:
171: if (null === $Location) {
172: $Info = $this->getFileParameter()->getFileInfo();
173: } else {
174: $Info = $Location->getFileInfo();
175: }
176:
177: $WriterType = $this->getWriterType($Info);
178:
179: if (null === $Location) {
180: $Location = $this->getFileParameter();
181: } else {
182: $Location = $Location->getFile();
183: }
184:
185: if ($WriterType) {
186: $Writer = \PHPExcel_IOFactory::createWriter($this->Source, $WriterType);
187: $Writer->save($Location);
188: } else {
189: // @codeCoverageIgnoreStart
190: throw new TypeFileException('No Writer for '.$Info->getExtension().' available!');
191: // @codeCoverageIgnoreEnd
192: }
193:
194: return $this;
195: }
196:
197: /**
198: * @param \SplFileInfo $Info
199: *
200: * @return null|string
201: */
202: private function getWriterType(\SplFileInfo $Info)
203: {
204:
205: $WriterList = array(
206: 'Excel2007' => array(
207: 'xlsx',
208: 'xlsm',
209: 'xltx',
210: 'xltm'
211: ),
212: 'Excel5' => array(
213: 'xls',
214: 'xlt'
215: ),
216: 'HTML' => array(
217: 'htm',
218: 'html'
219: ),
220: 'CSV' => array(
221: 'csv'
222: )
223: );
224:
225: $WriterType = null;
226: $Extension = $Info->getExtension();
227: array_walk($WriterList, function ($TypeList, $Writer) use (&$WriterType, $Extension) {
228:
229: if (in_array($Extension, $TypeList)) {
230: $WriterType = $Writer;
231: }
232: });
233: return $WriterType;
234: }
235: }
236: