1: <?php
2:
3: namespace SimpleExcel\Writer;
4:
5: use SimpleExcel\Spreadsheet\Cell;
6:
7: /**
8: * SimpleExcel class for writing table as JSON
9: *
10: * @author Faisalman
11: * @package SimpleExcel
12: */
13: class JSONWriter extends BaseWriter
14: {
15: /**
16: * Defines content-type for HTTP header
17: *
18: * @access protected
19: * @var string
20: */
21: protected $content_type = 'application/json';
22:
23: /**
24: * Defines file extension to be used when saving file
25: *
26: * @access protected
27: * @var string
28: */
29: protected $file_extension = 'json';
30:
31: /**
32: * Get document content as string
33: *
34: * @param array $options Options
35: * @return string Content of document
36: */
37: public function toString ($options = NULL) {
38: $json = array();
39: $sheet = array();
40: foreach ($this->workbook->getWorksheets() as $i => $worksheet) {
41: foreach ($worksheet->getRecords() as $record) {
42: $row = array();
43: for ($i = 0; $i < count($record); $i++) {
44: $row[$i] = $record[$i]->value;
45: }
46: array_push($sheet, (object)$row);
47: }
48: array_push($json, $sheet);
49: }
50: return json_encode($json);
51: }
52: }