Repository navigation
Expand file tree
/
Copy pathJSONParser.php
More file actions
53 lines (49 loc) · 1.58 KB
/
Copy pathJSONParser.php
File metadata and controls
53 lines (49 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace Faisalman\SimpleExcel\Parser;
use Faisalman\SimpleExcel\Enums\SimpleExcelException;
use Faisalman\SimpleExcel\Spreadsheet\Cell;
use Faisalman\SimpleExcel\Spreadsheet\Workbook;
use Faisalman\SimpleExcel\Spreadsheet\Worksheet;
/**
* SimpleExcel class for parsing JSON table
*
* @author Faisal Salman
* @package Faisalman\SimpleExcel
*/
class JSONParser extends BaseParser
{
/**
* Defines valid file extension
*
* @access protected
* @var string
*/
protected $file_extension = 'json';
/**
* Load the string to be parsed
*
* @param string $str String with JSON format
* @param array $options Options
* @throws Exception If JSON format is invalid (or too deep)
*/
public function loadString ($str, $options = NULL) {
$this->workbook = new Workbook();
if (($workbook = json_decode(utf8_encode($str), false, 5)) === NULL) {
throw new \Exception('Invalid JSON format: '.$str, SimpleExcelException::MALFORMED_JSON);
} else {
foreach ($workbook as $worksheet) {
$sheet = new Worksheet();
$rows = array();
foreach ($worksheet as $record) {
$row = array();
foreach ($record as $cell) {
array_push($row, new Cell($cell));
}
array_push($rows, $row);
}
$sheet->setRecords($rows);
$this->workbook->insertWorksheet($sheet);
}
}
}
}