1: <?php
2:
3: namespace SimpleExcel\Parser;
4:
5: use SimpleExcel\Enums\SimpleExcelException;
6: use SimpleExcel\Spreadsheet\Workbook;
7:
8: /**
9: * SimpleExcel class for parsing HTML table
10: *
11: * @author Faisalman
12: * @package SimpleExcel
13: */
14: abstract class BaseParser implements IParser
15: {
16: /**
17: * Holds the workbook instance
18: *
19: * @access protected
20: * @var Workbook
21: */
22: protected $workbook;
23:
24: /**
25: * Defines valid file extension
26: *
27: * @access protected
28: * @var string
29: */
30: protected $file_extension = '';
31:
32: /**
33: * @param Workbook reference to workbook
34: */
35: public function __construct(&$workbook) {
36: $this->workbook = &$workbook;
37: }
38:
39: /**
40: * Check whether file exists, valid, and readable
41: *
42: * @param string $file_path Path to file
43: * @return bool
44: * @throws Exception If file being loaded doesn't exist
45: * @throws Exception If file extension doesn't match
46: * @throws Exception If error reading the file
47: */
48: protected function checkFile($file_path) {
49:
50: // file exists?
51: if (!file_exists($file_path)) {
52:
53: throw new \Exception('File '.$file_path.' doesn\'t exist', SimpleExcelException::FILE_NOT_FOUND);
54:
55: // extension valid?
56: } else if (strtoupper(pathinfo($file_path, PATHINFO_EXTENSION))!= strtoupper($this->file_extension)){
57:
58: throw new \Exception('File extension '.$file_extension.' doesn\'t match with '.$this->file_extension, SimpleExcelException::FILE_EXTENSION_MISMATCH);
59:
60: // file readable?
61: } else if (($handle = fopen($file_path, 'r')) === FALSE) {
62:
63: throw new \Exception('Error reading the file in'.$file_path, SimpleExcelException::ERROR_READING_FILE);
64: fclose($handle);
65: } else {
66: return TRUE;
67: }
68: }
69: }
70: