ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
JSONParser.php
Go to the documentation of this file.
1<?php
2
3namespace SimpleExcel\Parser;
4
6
13class JSONParser extends BaseParser implements IParser
14{
21 protected $file_extension = 'json';
22
28 public function loadFile($file_path) {
29
30 if (!$this->isFileReady($file_path)) {
31 return;
32 }
33
34 $handle = fopen($file_path, 'r');
35 $this->loadString($handle);
36 fclose($handle);
37 }
38
45 public function loadString($str){
46 $field = array();
47 if (($table = json_decode(utf8_encode($str), false, 4)) === NULL) {
48 throw new \Exception('Invalid JSON format: '.$str, SimpleExcelException::MALFORMED_JSON);
49 } else {
50 foreach ($table as $rows) {
51 $row = array();
52 foreach ($rows as $cell) {
53 array_push($row, $cell);
54 }
55 array_push($field, $row);
56 }
57 }
58 $this->table_arr = $field;
59 }
60}
isFileReady($file_path)
Check whether file exists, valid, and readable.
Definition: BaseParser.php:169
loadString($str)
Load the string to be parsed.
Definition: JSONParser.php:45
loadFile($file_path)
Load the JSON file to be parsed.
Definition: JSONParser.php:28
define parser interface
Definition: IParser.php:14