ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
BaseParser.php
Go to the documentation of this file.
1 <?php
2 
3 namespace SimpleExcel\Parser;
4 
6 
13 abstract class BaseParser implements IParser
14 {
21  protected $table_arr;
22 
29  protected $file_extension = '';
30 
34  public function __construct($file_url = NULL) {
35  if(isset($file_url)) {
36  $this->loadFile($file_url);
37  }
38  }
39 
49  public function getCell($row_num, $col_num, $val_only = true) {
50  // check whether the cell exists
51  if (!$this->isCellExists($row_num, $col_num)) {
52  throw new \Exception('Cell '.$row_num.','.$col_num.' doesn\'t exist', SimpleExcelException::CELL_NOT_FOUND);
53  }
54  return $this->table_arr[$row_num-1][$col_num-1];
55  }
56 
65  public function getColumn($col_num, $val_only = TRUE) {
66  $col_arr = array();
67 
68  if(!$this->isColumnExists($col_num)){
69  throw new \Exception('Column '.$col_num.' doesn\'t exist', SimpleExcelException::COLUMN_NOT_FOUND);
70  }
71 
72  // get the specified column within every row
73  foreach($this->table_arr as $row){
74  array_push($col_arr, $row[$col_num-1]);
75  }
76 
77  // return the array
78  return $col_arr;
79  }
80 
88  public function getField($val_only = TRUE) {
89  if(!$this->isFieldExists()){
90  throw new \Exception('Field is not set', SimpleExcelException::FIELD_NOT_FOUND);
91  }
92 
93  // return the array
94  return $this->table_arr;
95  }
96 
105  public function getRow($row_num, $val_only = TRUE) {
106  if(!$this->isRowExists($row_num)){
107  throw new \Exception('Row '.$row_num.' doesn\'t exist', SimpleExcelException::ROW_NOT_FOUND);
108  }
109 
110  // return the array
111  return $this->table_arr[$row_num-1];
112  }
113 
121  public function isCellExists($row_num, $col_num){
122  return $this->isRowExists($row_num) && $this->isColumnExists($col_num);
123  }
124 
131  public function isColumnExists($col_num){
132  $exist = false;
133  foreach($this->table_arr as $row){
134  if(array_key_exists($col_num-1, $row)){
135  $exist = true;
136  }
137  }
138  return $exist;
139  }
140 
147  public function isRowExists($row_num){
148  return array_key_exists($row_num-1, $this->table_arr);
149  }
150 
156  public function isFieldExists(){
157  return isset($this->table_arr);
158  }
159 
169  public function isFileReady($file_path) {
170 
171  // file exists?
172  if (!file_exists($file_path)) {
173 
174  throw new \Exception('File '.$file_path.' doesn\'t exist', SimpleExcelException::FILE_NOT_FOUND);
175 
176  // extension valid?
177  } else if (strtoupper(pathinfo($file_path, PATHINFO_EXTENSION))!= strtoupper($this->file_extension)){
178 
179  throw new \Exception('File extension '.$file_extension.' doesn\'t match with '.$this->file_extension, SimpleExcelException::FILE_EXTENSION_MISMATCH);
180 
181  // file readable?
182  } else if (($handle = fopen($file_path, 'r')) === FALSE) {
183 
184  throw new \Exception('Error reading the file in'.$file_path, SimpleExcelException::ERROR_READING_FILE);
185  fclose($handle);
186 
187  // okay then
188  } else {
189 
190  return TRUE;
191  }
192  }
193 }