1 | <?php |
||
7 | abstract class AbstractSectionParser implements SectionParser |
||
8 | { |
||
9 | private const |
||
10 | COMMENT_CHARACTER = '#'; |
||
11 | |||
12 | protected ?string |
||
|
|||
13 | $currentFilePath; |
||
14 | |||
15 | protected ?int |
||
16 | $currentLineNumber; |
||
17 | |||
18 | 280 | public function __construct() |
|
19 | { |
||
20 | 280 | $this->currentFilePath = null; |
|
21 | 280 | $this->currentLineNumber = null; |
|
22 | 280 | } |
|
23 | |||
24 | 280 | public function setCurrentFile(string $filePath): void |
|
25 | { |
||
26 | 280 | $this->currentFilePath = $filePath; |
|
27 | 280 | } |
|
28 | |||
29 | 274 | protected function isACommentLine(string $line): bool |
|
30 | { |
||
31 | 274 | return strpos(trim($line), self::COMMENT_CHARACTER) === 0; |
|
32 | } |
||
33 | |||
34 | 274 | final public function parse(string $line, int $lineNumber): void |
|
35 | { |
||
36 | 274 | $this->currentLineNumber = $lineNumber; |
|
37 | |||
38 | 274 | if($this->isACommentLine($line)) |
|
39 | { |
||
40 | 211 | return; |
|
41 | } |
||
42 | |||
43 | 274 | $this->parseLine($line); |
|
44 | 253 | } |
|
45 | |||
46 | abstract protected function parseLine(string $line): void; |
||
47 | |||
48 | 28 | protected function triggerError(string $message, string $title = 'Syntax error'): void |
|
49 | { |
||
50 | 28 | throw new \RuntimeException(sprintf( |
|
51 | 28 | '%s in %s line %d : %s', |
|
52 | $title, |
||
53 | 28 | $this->currentFilePath, |
|
54 | 28 | $this->currentLineNumber, |
|
55 | $message |
||
56 | )); |
||
57 | } |
||
58 | |||
59 | 247 | public function postParse(): void |
|
60 | { |
||
61 | 247 | } |
|
63 |