1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Alexander Getmanskii <[email protected]> |
4
|
|
|
* @package GetSky\ParserExpressions |
5
|
|
|
*/ |
6
|
|
|
namespace GetSky\ParserExpressions; |
7
|
|
|
|
8
|
|
|
use Exception; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* It is a wrapper for parsing string. |
12
|
|
|
*/ |
13
|
|
|
class Context |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string Parsing string. |
17
|
|
|
*/ |
18
|
|
|
protected $string; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var int The number of the current position. |
22
|
|
|
*/ |
23
|
|
|
protected $cursor = 0; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var int The current depth of immersion in the rules tree. |
27
|
|
|
*/ |
28
|
|
|
protected $depth = 0; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ErrorCollectionInterface |
32
|
|
|
*/ |
33
|
|
|
protected $errors; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param ErrorCollectionInterface $errorCollection |
37
|
|
|
*/ |
38
|
2 |
|
public function __construct(ErrorCollectionInterface $errorCollection) |
39
|
|
|
{ |
40
|
2 |
|
$this->errors = $errorCollection; |
41
|
2 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Set new string and reset cursor. |
45
|
|
|
* |
46
|
|
|
* @param string $string |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
2 |
|
public function setString($string) |
50
|
|
|
{ |
51
|
2 |
|
$this->string = (string) $string; |
52
|
2 |
|
$this->cursor = 0; |
53
|
2 |
|
$this->depth = 0; |
54
|
2 |
|
$this->errors->clear(); |
55
|
2 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns the characters from the current position with the given $size. |
59
|
|
|
* |
60
|
|
|
* @param int $size |
61
|
|
|
* @return bool|string |
62
|
|
|
*/ |
63
|
3 |
|
public function value($size = 1) |
64
|
|
|
{ |
65
|
3 |
|
if ($this->cursor + $size > strlen($this->string)) { |
66
|
1 |
|
return false; |
67
|
|
|
} |
68
|
2 |
|
$value = substr($this->string, $this->cursor, $size); |
69
|
2 |
|
$this->cursor += $size; |
70
|
|
|
|
71
|
2 |
|
return $value; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns the current value of the cursor. |
76
|
|
|
* |
77
|
|
|
* @return int |
78
|
|
|
*/ |
79
|
3 |
|
public function getCursor() |
80
|
|
|
{ |
81
|
3 |
|
return $this->cursor; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Sets a new value for the cursor. |
86
|
|
|
* |
87
|
|
|
* @param int $position |
88
|
|
|
* @throws Exception |
89
|
|
|
*/ |
90
|
4 |
|
public function setCursor($position) |
91
|
|
|
{ |
92
|
4 |
|
if ($position < 0) { |
93
|
1 |
|
throw new Exception('The cursor can\'t be negative.'); |
94
|
|
|
} |
95
|
3 |
|
$this->cursor = $position; |
96
|
3 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Increase the depth by one. |
100
|
|
|
* |
101
|
|
|
* @throws Exception |
102
|
|
|
*/ |
103
|
7 |
|
public function increaseDepth() |
104
|
|
|
{ |
105
|
7 |
|
$this->depth++; |
106
|
7 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Decrease the depth by one. |
110
|
|
|
* |
111
|
|
|
* @throws Exception |
112
|
|
|
*/ |
113
|
7 |
|
public function decreaseDepth() |
114
|
|
|
{ |
115
|
7 |
|
if ($this->depth == 0) { |
116
|
|
|
throw new Exception('The depth can\'t be negative.'); |
117
|
|
|
} |
118
|
7 |
|
$this->depth--; |
119
|
7 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param RuleInterface $rule |
123
|
|
|
* @param int $index |
124
|
|
|
*/ |
125
|
|
|
public function error(RuleInterface $rule, $index) |
126
|
|
|
{ |
127
|
|
|
$this->errors->add($rule, $index, $this->depth); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|