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