1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Funivan\PhpTokenizer; |
6
|
|
|
|
7
|
|
|
use Funivan\PhpTokenizer\Exception\Exception; |
8
|
|
|
use Funivan\PhpTokenizer\Exception\InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* |
12
|
|
|
* Value is 2 type variable. It can be string or null |
13
|
|
|
* When you set value is automatically cast to string |
14
|
|
|
* |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
class Token { |
18
|
|
|
|
19
|
|
|
const INVALID_TYPE = -1; |
20
|
|
|
|
21
|
|
|
const INVALID_LINE = -1; |
22
|
|
|
|
23
|
|
|
const INVALID_VALUE = null; |
24
|
|
|
|
25
|
|
|
const INVALID_INDEX = -1; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
protected $type = self::INVALID_TYPE; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string|null |
34
|
|
|
*/ |
35
|
|
|
protected $value; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var int |
39
|
|
|
*/ |
40
|
|
|
protected $line = self::INVALID_LINE; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Indicate position in current collection |
44
|
|
|
* |
45
|
|
|
* @var int |
46
|
|
|
*/ |
47
|
|
|
protected $index = self::INVALID_INDEX; |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* You need to provide at least 3 elements |
52
|
|
|
* |
53
|
|
|
* @param array $data |
54
|
|
|
* @throws Exception |
55
|
|
|
*/ |
56
|
537 |
|
public function __construct(array $data = []) { |
57
|
537 |
|
if (!empty($data)) { |
58
|
486 |
|
$this->setData($data); |
59
|
|
|
} |
60
|
528 |
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
6 |
|
public function __toString() { |
67
|
6 |
|
return $this->assemble(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
9 |
|
public function assemble() : string { |
75
|
9 |
|
return $this->value !== null ? (string) $this->value : ''; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param array $data |
81
|
|
|
* @return $this |
82
|
|
|
* @throws Exception |
83
|
|
|
*/ |
84
|
486 |
|
protected function setData(array $data) : self { |
85
|
486 |
|
if (!array_key_exists(0, $data)) { |
86
|
3 |
|
throw new InvalidArgumentException('Please provide type of token'); |
87
|
|
|
} |
88
|
|
|
|
89
|
483 |
|
$this->setType((int) $data[0]); |
90
|
|
|
|
91
|
483 |
|
if (!isset($data[1])) { |
92
|
3 |
|
throw new InvalidArgumentException('Please provide value of token'); |
93
|
|
|
} |
94
|
|
|
|
95
|
480 |
|
$this->setValue($data[1]); |
96
|
|
|
|
97
|
480 |
|
if (!array_key_exists(2, $data)) { |
98
|
3 |
|
throw new InvalidArgumentException('Please provide line of token'); |
99
|
|
|
} |
100
|
|
|
|
101
|
477 |
|
$this->setLine($data[2]); |
102
|
|
|
|
103
|
477 |
|
if (array_key_exists(3, $data)) { |
104
|
3 |
|
$this->setIndex($data[3]); |
105
|
|
|
} |
106
|
|
|
|
107
|
477 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
9 |
|
public function getData() : array { |
115
|
9 |
|
return [$this->getType(), $this->getValue(), $this->getLine(), $this->getIndex()]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param int $type |
121
|
|
|
* @return $this |
122
|
|
|
*/ |
123
|
483 |
|
public function setType(int $type) : self { |
124
|
483 |
|
$this->type = $type; |
125
|
483 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return int |
131
|
|
|
*/ |
132
|
297 |
|
public function getType() : int { |
133
|
297 |
|
return $this->type; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
6 |
|
public function getTypeName() : string { |
141
|
6 |
|
return token_name($this->getType()); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return string|null |
147
|
|
|
*/ |
148
|
495 |
|
public function getValue() { |
149
|
495 |
|
return $this->value; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param string|int $value |
155
|
|
|
* @throws InvalidArgumentException |
156
|
|
|
* @return $this |
157
|
|
|
*/ |
158
|
498 |
|
public function setValue($value) : self { |
159
|
498 |
|
if (!is_string($value) and !is_numeric($value)) { |
160
|
3 |
|
throw new InvalidArgumentException('You can set only string. Given: ' . gettype($value)); |
161
|
|
|
} |
162
|
498 |
|
$this->value = (string) $value; |
163
|
498 |
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return int |
169
|
|
|
*/ |
170
|
453 |
|
public function getLine() : int { |
171
|
453 |
|
return $this->line; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param int $line |
177
|
|
|
* @return $this |
178
|
|
|
*/ |
179
|
480 |
|
public function setLine(int $line) : self { |
180
|
480 |
|
$this->line = $line; |
181
|
480 |
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return bool |
187
|
|
|
*/ |
188
|
468 |
|
public function isValid() : bool { |
189
|
468 |
|
return $this->getValue() !== null; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Remove all data from token so this token become invalid |
195
|
|
|
* |
196
|
|
|
* @return $this |
197
|
|
|
*/ |
198
|
90 |
|
public function remove() : self { |
199
|
90 |
|
$this->type = static::INVALID_TYPE; |
200
|
90 |
|
$this->value = static::INVALID_VALUE; |
201
|
90 |
|
$this->line = static::INVALID_LINE; |
202
|
90 |
|
$this->index = static::INVALID_INDEX; |
203
|
90 |
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Add part to the end of value |
209
|
|
|
* |
210
|
|
|
* @param string $part |
211
|
|
|
* @return $this |
212
|
|
|
* @throws Exception |
213
|
|
|
*/ |
214
|
33 |
|
public function appendToValue($part) : self { |
215
|
|
|
|
216
|
33 |
|
if (!is_string($part) and !is_numeric($part)) { |
217
|
|
|
throw new InvalidArgumentException('You can append only string to value'); |
218
|
|
|
} |
219
|
|
|
|
220
|
33 |
|
$this->value = $this->value . $part; |
221
|
|
|
|
222
|
33 |
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Add part to the begin of value |
228
|
|
|
* |
229
|
|
|
* @param string $part |
230
|
|
|
* @return $this |
231
|
|
|
* @throws Exception |
232
|
|
|
*/ |
233
|
36 |
|
public function prependToValue($part) : self { |
234
|
|
|
|
235
|
36 |
|
if (!is_string($part) and !is_numeric($part)) { |
236
|
6 |
|
throw new InvalidArgumentException('You can prepend only string to value'); |
237
|
|
|
} |
238
|
|
|
|
239
|
33 |
|
$this->value = $part . $this->value; |
240
|
|
|
|
241
|
33 |
|
return $this; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @return null|int |
247
|
|
|
*/ |
248
|
225 |
|
public function getIndex() { |
249
|
225 |
|
return $this->index; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @param int $index |
255
|
|
|
* @return $this |
256
|
|
|
*/ |
257
|
489 |
|
public function setIndex(int $index) : self { |
258
|
489 |
|
$this->index = $index; |
259
|
489 |
|
return $this; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
|
263
|
9 |
|
public function equal(Token $token) : bool { |
264
|
|
|
return ( |
265
|
9 |
|
$this->value === $token->getValue() |
266
|
|
|
and |
267
|
9 |
|
$this->type === $token->getType() |
268
|
|
|
); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
} |