1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Htsl\ReadingBuffer; |
4
|
|
|
|
5
|
|
|
use Htsl\Helper\TGetter; |
6
|
|
|
|
7
|
|
|
//////////////////////////////////////////////////////////////// |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @property-read string $content Line content without indentation. |
11
|
|
|
* @property-read string $fullContent Full content of this line. |
12
|
|
|
* @property-read int $indentLevel Indent level of this line. |
13
|
|
|
* @property-read \Htsl\ReadingBuffer\Line $subIndentLine Subindent line. |
14
|
|
|
*/ |
15
|
|
|
class Line |
16
|
|
|
{ |
17
|
|
|
use TGetter; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Line content. |
21
|
|
|
* |
22
|
|
|
* @access private |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $content; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Whether this line is last line. |
30
|
|
|
* |
31
|
|
|
* @access private |
32
|
|
|
* |
33
|
|
|
* @var bool |
34
|
|
|
*/ |
35
|
|
|
private $isLast=false; |
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Constructor. |
39
|
|
|
* |
40
|
|
|
* @access public |
41
|
|
|
* |
42
|
|
|
* @param string | bool $content String content for normal line and false for last line. |
43
|
|
|
*/ |
44
|
|
|
public function __construct( /*string|bool*/$content ) |
45
|
|
|
{ |
46
|
|
|
false===$content and $this->isLast=true; |
|
|
|
|
47
|
|
|
|
48
|
|
|
$this->content= rtrim($content,"\n"); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Getting content without indentation. |
53
|
|
|
* |
54
|
|
|
* @access public |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public function getContent():string |
59
|
|
|
{ |
60
|
|
|
return ltrim($this->content,"\t"); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Getting full content. |
65
|
|
|
* |
66
|
|
|
* @access public |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function getFullContent():string |
71
|
|
|
{ |
72
|
|
|
return $this->content; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Getting a section of content, like call substr(). |
77
|
|
|
* |
78
|
|
|
* @access public |
79
|
|
|
* |
80
|
|
|
* @param int $start |
81
|
|
|
* @param int ...$lengths |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
|
|
|
|
85
|
|
|
public function slice( int$start=0, int...$lengths ):string |
86
|
|
|
{ |
87
|
|
|
return substr($this->getContent(),$start,...array_slice($lengths,0,1)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Getting single character from content. |
92
|
|
|
* |
93
|
|
|
* @access public |
94
|
|
|
* |
95
|
|
|
* @param int $offset |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function getChar( int$offset ):string |
100
|
|
|
{ |
101
|
|
|
return substr($this->getcontent(),$offset,1); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Matching a preg pattern and return whether matches. |
106
|
|
|
* |
107
|
|
|
* @access public |
108
|
|
|
* |
109
|
|
|
* @param string $pattern |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
public function pregMatch( string$pattern ):bool |
114
|
|
|
{ |
115
|
|
|
return !!preg_match($pattern,ltrim($this->content,"\t")); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Matching a preg pattern and return the all or one of groups of matchment. |
120
|
|
|
* |
121
|
|
|
* @access public |
122
|
|
|
* |
123
|
|
|
* @param string $pattern |
124
|
|
|
* @param int | string $match Group index or name |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function pregGet( string$pattern, /*int|string*/$match=0 ):string |
129
|
|
|
{ |
130
|
|
|
preg_match($pattern,ltrim($this->content,"\t"),$matches); |
131
|
|
|
return $matches[$match]??''; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Multiple matching a preg pattern and map result with a callback. |
136
|
|
|
* |
137
|
|
|
* @access public |
138
|
|
|
* |
139
|
|
|
* @param string $pattern |
140
|
|
|
* @param callable $callback |
141
|
|
|
* |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
|
|
public function pregMap( string$pattern, callable$callback ):array |
145
|
|
|
{ |
146
|
|
|
preg_match_all($pattern,ltrim($this->content,"\t"),$matches); |
147
|
|
|
return array_map($callback,...$matches); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Getting the indent level of this line, the number of starting tab characters. |
152
|
|
|
* |
153
|
|
|
* @access public |
154
|
|
|
* |
155
|
|
|
* @return int |
156
|
|
|
*/ |
157
|
|
|
public function getIndentLevel():int |
158
|
|
|
{ |
159
|
|
|
return strlen($this->content)-strlen(ltrim($this->content,"\t")); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Converting this object to string, returning content without indentation. |
164
|
|
|
* |
165
|
|
|
* @access public |
166
|
|
|
* |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
|
|
public function __toString():string |
170
|
|
|
{ |
171
|
|
|
return $this->getContent(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Wether this line is empty. |
176
|
|
|
* |
177
|
|
|
* @access public |
178
|
|
|
* |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
|
|
public function isEmpty():bool |
182
|
|
|
{ |
183
|
|
|
return !strlen($this->content); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Whether this line is last line. |
188
|
|
|
* |
189
|
|
|
* @access public |
190
|
|
|
* |
191
|
|
|
* @return bool |
192
|
|
|
*/ |
193
|
|
|
public function isLast():bool |
194
|
|
|
{ |
195
|
|
|
return $this->isLast; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Whether this line is last line. |
200
|
|
|
* |
201
|
|
|
* @access public |
202
|
|
|
* |
203
|
|
|
* @return bool |
204
|
|
|
*/ |
205
|
|
|
public function noMore():bool |
206
|
|
|
{ |
207
|
|
|
return $this->isLast; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Whether next line exists. |
212
|
|
|
* |
213
|
|
|
* @access public |
214
|
|
|
* |
215
|
|
|
* @return bool |
216
|
|
|
*/ |
217
|
|
|
public function hasMore():bool |
218
|
|
|
{ |
219
|
|
|
return !$this->isLast; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Getting line with sub level indent( tab space tab ). |
224
|
|
|
* |
225
|
|
|
* @access public |
226
|
|
|
* |
227
|
|
|
* @return \Htsl\ReadingBuffer\Line |
228
|
|
|
*/ |
229
|
|
|
public function getSubIndentLine():self |
230
|
|
|
{ |
231
|
|
|
return new static(ltrim($this->getContent(),' ')); |
232
|
|
|
} |
233
|
|
|
} |
|
|
|
|
234
|
|
|
|
This check looks for improperly formatted assignments.
Every assignment must have exactly one space before and one space after the equals operator.
To illustrate:
will have no issues, while
will report issues in lines 1 and 2.