1: <?php
2: /**
3: * Simple Excel
4: *
5: * A PHP library with simplistic approach
6: * Easily parse/convert/write between Microsoft Excel XML/CSV/TSV/HTML/JSON/etc formats
7: *
8: * Copyright (c) 2011-2013 Faisalman <fyzlman@gmail.com>
9: *
10: * Permission is hereby granted, free of charge, to any person obtaining a copy
11: * of this software and associated documentation files (the "Software"), to deal
12: * in the Software without restriction, including without limitation the rights
13: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14: * copies of the Software, and to permit persons to whom the Software is
15: * furnished to do so, subject to the following conditions:
16: *
17: * The above copyright notice and this permission notice shall be included in
18: * all copies or substantial portions of the Software.
19: *
20: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26: * THE SOFTWARE.
27: *
28: * @author Faisalman
29: * @copyright 2011-2013 (c) Faisalman
30: * @license http://www.opensource.org/licenses/mit-license
31: * @link http://github.com/faisalman/simple-excel-php
32: * @package SimpleExcel
33: * @version 0.4.0-alpha
34: */
35:
36: namespace SimpleExcel;
37:
38: use SimpleExcel\Enums\SimpleExcelException;
39: use SimpleExcel\Spreadsheet\Workbook;
40: use SimpleExcel\Spreadsheet\Worksheet;
41:
42: if (!class_exists('Composer\\Autoload\\ClassLoader', false)){
43: // autoload all interfaces & classes
44: spl_autoload_register(function($class_name){
45: if($class_name != 'SimpleExcel') require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, substr($class_name, strlen('SimpleExcel\\'))).'.php');
46: });
47: }
48:
49: /**
50: * SimpleExcel main class
51: *
52: * @author Faisalman
53: * @package SimpleExcel
54: */
55: class SimpleExcel
56: {
57: /**
58: * @var IParser
59: */
60: protected $parser;
61:
62: /**
63: * @var string
64: */
65: protected $parserType;
66:
67: /**
68: * @var array
69: */
70: protected $validParserTypes;
71:
72: /**
73: * @var array
74: */
75: protected $validWriterTypes;
76:
77: /**
78: * @var IWriter
79: */
80: protected $writer;
81:
82: /**
83: * @var string
84: */
85: protected $writerType;
86:
87: /**
88: * @var Workbook
89: */
90: public $workbook;
91:
92: /**
93: * SimpleExcel constructor method
94: *
95: * @param string $filetype Set the filetype of the file
96: */
97: public function __construct ($filetype = NULL) {
98: $this->workbook = new Workbook();
99: $this->validParserTypes = array('XML', 'CSV', 'TSV', 'HTML', 'JSON');
100: $this->validWriterTypes = array('XML', 'CSV', 'TSV', 'HTML', 'JSON');
101: if (isset($filetype)) {
102: $this->setParserType($filetype);
103: $this->setWriterType($filetype);
104: }
105: }
106:
107: /**
108: * Export data as file
109: *
110: * @param string $target Where to write the file
111: * @param string $filetype Type of the file to be written
112: * @param string $options Options
113: * @throws Exception If filetype is not supported
114: * @throws Exception If error writing file
115: */
116: public function exportFile ($target, $fileType, $options = NULL) {
117: $this->setWriterType($fileType);
118: $this->writer->exportFile($target, $options);
119: }
120:
121: /**
122: * Get specified worksheet
123: *
124: * @param int $index Worksheet index
125: * @return Worksheet
126: * @throws Exception If worksheet with specified index is not found
127: */
128: public function getWorksheet ($index = 1) {
129: return $this->workbook->getWorksheet($index);
130: }
131:
132: /**
133: * Get all worksheets
134: *
135: * @return array
136: */
137: public function getWorksheets () {
138: return $this->workbook->getWorksheets();
139: }
140:
141: /**
142: * Insert a worksheet
143: *
144: * @param Worksheet $worksheet Worksheet to be inserted
145: */
146: public function insertWorksheet (Worksheet $worksheet) {
147: $this->workbook->insertWorksheet($worksheet);
148: }
149:
150: /**
151: * Load file to parser
152: *
153: * @param string $filepath Path to file
154: * @param string $filetype Set the filetype of the file which will be parsed
155: * @param string $options Options
156: * @throws Exception If filetype is not supported
157: * @throws Exception If file being loaded doesn't exist
158: * @throws Exception If file extension doesn't match
159: * @throws Exception If error reading the file
160: */
161: public function loadFile ($filePath, $fileType, $options = NULL) {
162: $this->setParserType($fileType);
163: $this->parser->loadFile($filePath, $options);
164: }
165:
166: /**
167: * Load string to parser
168: *
169: * @param string $filepath Path to file
170: * @param string $filetype Set the filetype of the file which will be parsed
171: * @throws Exception If filetype is not supported
172: */
173: public function loadString ($string, $fileType) {
174: $this->setParserType($fileType);
175: $this->parser->loadString($string);
176: }
177:
178: /**
179: * Remove a worksheet
180: *
181: * @param int $index Worksheet index to be removed
182: */
183: public function removeWorksheet ($index) {
184: $this->workbook->removeWorksheet($index);
185: }
186:
187: /**
188: * Construct a SimpleExcel Parser
189: *
190: * @param string $filetype Set the filetype of the file which will be parsed (XML/CSV/TSV/HTML/JSON)
191: * @throws Exception If filetype is not supported
192: */
193: protected function setParserType($filetype){
194: $filetype = strtoupper($filetype);
195: if ($filetype != $this->parserType) {
196: if(!in_array($filetype, $this->validParserTypes)){
197: throw new \Exception('Filetype '.$filetype.' is not supported', SimpleExcelException::FILETYPE_NOT_SUPPORTED);
198: }
199: $parser_class = 'SimpleExcel\\Parser\\'.$filetype.'Parser';
200: $this->parser = new $parser_class($this->workbook);
201: $this->parserType = $filetype;
202: }
203: }
204:
205: /**
206: * Construct a SimpleExcel Writer
207: *
208: * @param string $filetype Set the filetype of the file which will be written
209: * @throws Exception If filetype is not supported
210: */
211: protected function setWriterType ($filetype) {
212: $filetype = strtoupper($filetype);
213: if ($filetype != $this->writerType) {
214: if(!in_array($filetype, $this->validWriterTypes)) {
215: throw new \Exception('Filetype '.$filetype.' is not supported', SimpleExcelException::FILETYPE_NOT_SUPPORTED);
216: }
217: $writer_class = 'SimpleExcel\\Writer\\'.$filetype.'Writer';
218: $this->writer = new $writer_class($this->workbook);
219: $this->writerType = $filetype;
220: }
221: }
222:
223: /**
224: * Get data as string
225: *
226: * @param string $filetype Document format for the string to be returned
227: * @param string $options Options
228: * @return string
229: * @throws Exception If filetype is not supported
230: */
231: public function toString ($filetype, $options = NULL) {
232: $this->setWriterType($filetype);
233: return $this->writer->toString($options);
234: }
235: }
236: