Repository navigation
Expand file tree
/
Copy pathJSONParser.php
More file actions
61 lines (54 loc) · 1.52 KB
/
Copy pathJSONParser.php
File metadata and controls
61 lines (54 loc) · 1.52 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
54
55
56
57
58
59
60
61
<?php
namespace SimpleExcel\Parser;
use SimpleExcel\Exception\SimpleExcelException;
/**
* SimpleExcel class for parsing JSON table
*
* @author Faisalman
* @package SimpleExcel
*/
class JSONParser extends BaseParser implements IParser
{
/**
* Defines valid file extension
*
* @access protected
* @var string
*/
protected $file_extension = 'json';
/**
* Load the JSON file to be parsed
*
* @param string $file_path Path to JSON file
*/
public function loadFile($file_path) {
if (!$this->isFileReady($file_path)) {
return;
}
$handle = fopen($file_path, 'r');
$contents = fread($handle, filesize($file_path));
$this->loadString($contents);
fclose($handle);
}
/**
* Load the string to be parsed
*
* @param string $str String with JSON format
* @throws Exception If JSON format is invalid (or too deep)
*/
public function loadString($str){
$field = array();
if (($table = json_decode(utf8_encode($str), false, 4)) === NULL) {
throw new \Exception('Invalid JSON format: '.$str, SimpleExcelException::MALFORMED_JSON);
} else {
foreach ($table as $rows) {
$row = array();
foreach ($rows as $cell) {
array_push($row, $cell);
}
array_push($field, $row);
}
}
$this->table_arr = $field;
}
}