1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author: Viskov Sergey |
4
|
|
|
* @date : 8/5/15 |
5
|
|
|
* @time : 1:34 PM |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace LTDBeget\dns; |
9
|
|
|
|
10
|
|
|
use Exception; |
11
|
|
|
use LTDBeget\stringstream\StringStream; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class SyntaxErrorException |
15
|
|
|
* |
16
|
|
|
* @package LTDBeget\dns |
17
|
|
|
*/ |
18
|
|
|
class SyntaxErrorException extends \RuntimeException |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
private $error_line; |
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $unexpected_char; |
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $messageTemplate = "Parse error: syntax error, unexpected '%s' on line %d."; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* SyntaxErrorException constructor. |
35
|
|
|
* |
36
|
|
|
* @param StringStream $stream |
37
|
|
|
* @param int $code |
38
|
|
|
* @param Exception|null $previous |
39
|
|
|
*/ |
40
|
1 |
|
public function __construct(StringStream $stream, int $code = 0, Exception $previous = NULL) |
41
|
|
|
{ |
42
|
1 |
|
if ($stream->isEnd()) { |
43
|
|
|
$stream->end(); |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
$this->unexpected_char = $stream->current(); |
47
|
1 |
|
$this->error_line = $this->getParseErrorLineNumber($stream); |
48
|
1 |
|
$message = sprintf($this->messageTemplate, $this->unexpected_char, $this->error_line); |
49
|
1 |
|
parent::__construct($message, $code, $previous); |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @internal |
54
|
|
|
* @param StringStream $stream |
55
|
|
|
* @return int |
56
|
|
|
*/ |
57
|
1 |
|
private function getParseErrorLineNumber(StringStream $stream) : int |
58
|
|
|
{ |
59
|
1 |
|
$parse_error_char_position = $stream->position(); |
60
|
1 |
|
$plain_data = $this->getFullString($stream); |
61
|
1 |
|
$exploded_by_lines = explode("\n", $plain_data); |
62
|
1 |
|
foreach ($exploded_by_lines as $key => $line) { |
63
|
1 |
|
$line_length = strlen($line) + 1; |
64
|
1 |
|
$parse_error_char_position -= $line_length; |
65
|
1 |
|
if ($parse_error_char_position < 0) { |
66
|
1 |
|
return $key + 1; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return 1; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param StringStream $stream |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
1 |
|
private function getFullString(StringStream $stream) : string |
78
|
|
|
{ |
79
|
1 |
|
$stream->start(); |
80
|
1 |
|
$string = ''; |
81
|
|
|
do { |
82
|
1 |
|
$string .= $stream->current(); |
83
|
1 |
|
$stream->next(); |
84
|
1 |
|
} while (!$stream->isEnd()); |
85
|
|
|
|
86
|
1 |
|
return $string; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return int |
91
|
|
|
*/ |
92
|
|
|
public function getErrorLine() |
93
|
|
|
{ |
94
|
|
|
return $this->error_line; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function getUnexpectedChar() |
101
|
|
|
{ |
102
|
|
|
return $this->unexpected_char; |
103
|
|
|
} |
104
|
|
|
} |