Repository navigation
Expand file tree
/
Copy pathBaseParser.php
More file actions
79 lines (68 loc) · 2.12 KB
/
Copy pathBaseParser.php
File metadata and controls
79 lines (68 loc) · 2.12 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
namespace Faisalman\SimpleExcel\Parser;
use Faisalman\SimpleExcel\Enums\SimpleExcelException;
use Faisalman\SimpleExcel\Spreadsheet\Workbook;
/**
* SimpleExcel class for parsing HTML table
*
* @author Faisal Salman
* @package Faisalman\SimpleExcel
*/
abstract class BaseParser implements IParser
{
/**
* Holds the workbook instance
*
* @access protected
* @var Workbook
*/
protected $workbook;
/**
* Defines valid file extension
*
* @access protected
* @var string
*/
protected $file_extension = '';
/**
* @param Workbook reference to workbook
*/
public function __construct(&$workbook) {
$this->workbook = &$workbook;
}
/**
* Check whether file exists, valid, and readable
*
* @param string $file_path Path to file
* @return bool
* @throws Exception If file being loaded doesn't exist
* @throws Exception If file extension doesn't match
* @throws Exception If error reading the file
*/
protected function checkFile($file_path) {
// file exists?
if (!file_exists($file_path)) {
throw new \Exception('File '.$file_path.' doesn\'t exist', SimpleExcelException::FILE_NOT_FOUND);
// extension valid?
} else if (($handle = fopen($file_path, 'r')) === FALSE) {
throw new \Exception('Error reading the file in'.$file_path, SimpleExcelException::ERROR_READING_FILE);
fclose($handle);
} else {
return TRUE;
}
}
/**
* Load the file to be parsed
*
* @param string $file_path Path to file
* @param array $options Options
* @throws Exception If file being loaded doesn't exist
* @throws Exception If file extension doesn't match
* @throws Exception If error reading the file
*/
public function loadFile ($file_path, $options = NULL) {
if ($this->checkFile($file_path)) {
$this->loadString(file_get_contents($file_path), $options);
}
}
}