1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Funivan\PhpTokenizer; |
4
|
|
|
|
5
|
|
|
use Funivan\PhpTokenizer\Collection\BaseCollection; |
6
|
|
|
use Funivan\PhpTokenizer\Exception\Exception; |
7
|
|
|
use Funivan\PhpTokenizer\Query\Query; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Represent access and manipulation array of tokens |
11
|
|
|
* |
12
|
|
|
* @method \Funivan\PhpTokenizer\Token getLast(); |
13
|
|
|
* @method \Funivan\PhpTokenizer\Token current(); |
14
|
|
|
* @method \Funivan\PhpTokenizer\Token|null offsetGet($index); |
15
|
|
|
* @method \Funivan\PhpTokenizer\Token|null getFirst(); |
16
|
|
|
* @method \Funivan\PhpTokenizer\Token[] getItems(); |
17
|
|
|
* @method \Funivan\PhpTokenizer\Collection extractItems($offset, $length = null); |
18
|
|
|
* @method $this setItems($tokens) |
19
|
|
|
* |
20
|
|
|
* @package Funivan\PhpTokenizer |
21
|
|
|
*/ |
22
|
|
|
class Collection extends BaseCollection { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* You can use this constant for access class name |
26
|
|
|
*/ |
27
|
|
|
const N = __CLASS__; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $initialContentHash; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param array $items |
37
|
450 |
|
*/ |
38
|
450 |
|
public function __construct(array $items = []) { |
39
|
450 |
|
parent::__construct($items); |
40
|
450 |
|
$this->storeContentHash(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Extract each value from token |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
144 |
|
*/ |
49
|
144 |
|
public function __toString() { |
50
|
|
|
return $this->assemble(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* |
56
|
|
|
* @param string $string |
57
|
|
|
* @return Collection |
58
|
|
|
* @throws Exception |
59
|
435 |
|
*/ |
60
|
435 |
|
public static function createFromString($string) { |
61
|
435 |
|
$tokens = Helper::getTokensFromString($string); |
62
|
|
|
return new static($tokens); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return bool |
68
|
9 |
|
*/ |
69
|
9 |
|
public function isChanged() { |
70
|
|
|
return ($this->getContentHash() !== $this->initialContentHash); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return string |
76
|
450 |
|
*/ |
77
|
450 |
|
private function getContentHash() { |
78
|
|
|
return md5($this->assemble()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function assemble() { |
86
|
441 |
|
$string = ''; |
87
|
441 |
|
/** @var Token $token */ |
88
|
|
|
foreach ($this as $token) { |
89
|
|
|
if (!$token->isValid()) { |
90
|
|
|
continue; |
91
|
|
|
} |
92
|
|
|
$string .= $token->getValue(); |
93
|
|
|
} |
94
|
450 |
|
|
95
|
450 |
|
return $string; |
96
|
|
|
} |
97
|
450 |
|
|
98
|
435 |
|
|
99
|
66 |
|
/** |
100
|
|
|
* Remove all invalid tokens in collection |
101
|
435 |
|
* Refresh index. |
102
|
300 |
|
* |
103
|
|
|
* @return $this |
104
|
450 |
|
*/ |
105
|
|
|
public function refresh() { |
106
|
|
|
$string = $this->assemble(); |
107
|
|
|
$this->cleanCollection(); |
108
|
|
|
|
109
|
|
|
$tokens = Helper::getTokensFromString($string); |
110
|
|
|
$this->setItems($tokens); |
111
|
|
|
|
112
|
|
|
$this->rewind(); |
113
|
|
|
return $this; |
114
|
9 |
|
} |
115
|
9 |
|
|
116
|
9 |
|
|
117
|
|
|
/** |
118
|
9 |
|
* @param int $step |
119
|
9 |
|
* @return Token |
120
|
|
|
*/ |
121
|
9 |
|
public function getPrevious($step = 1) { |
122
|
9 |
|
$item = parent::getPrevious($step); |
123
|
|
|
if ($item === null) { |
124
|
|
|
$item = new Token(); |
125
|
|
|
} |
126
|
|
|
return $item; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
3 |
|
/** |
131
|
3 |
|
* @param int $step |
132
|
3 |
|
* @return Token |
133
|
3 |
|
*/ |
134
|
2 |
|
public function getNext($step = 1) { |
135
|
3 |
|
$item = parent::getNext($step); |
136
|
|
|
if ($item === null) { |
137
|
|
|
$item = new Token(); |
138
|
|
|
} |
139
|
|
|
return $item; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
|
143
|
6 |
|
/** |
144
|
6 |
|
* @codeCoverageIgnore |
145
|
6 |
|
* @deprecated |
146
|
6 |
|
* @see createFromString |
147
|
4 |
|
* @param string $string |
148
|
6 |
|
* @return Collection |
149
|
|
|
*/ |
150
|
|
|
public static function initFromString($string) { |
151
|
|
|
trigger_error(__CLASS__ . '::' . __METHOD__ . ' deprecated and will be removed in 0.1.3 Use ' . __CLASS__ . '::createFromString', E_USER_DEPRECATED); |
152
|
|
|
return self::createFromString($string); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Remove invalid tokens from collection |
158
|
|
|
* |
159
|
|
|
* @return $this |
160
|
|
|
*/ |
161
|
|
|
private function cleanCollection() { |
162
|
|
|
foreach ($this as $index => $token) { |
163
|
|
|
if ($token->isValid()) { |
164
|
|
|
continue; |
165
|
|
|
} |
166
|
|
|
unset($this->items[$index]); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
9 |
|
} |
171
|
9 |
|
|
172
|
9 |
|
|
173
|
9 |
|
/** |
174
|
|
|
* Remove all tokens in collection |
175
|
3 |
|
* |
176
|
6 |
|
* @return $this |
177
|
|
|
*/ |
178
|
9 |
|
|
179
|
|
|
public function remove() { |
180
|
|
|
foreach ($this as $token) { |
181
|
|
|
$token->remove(); |
182
|
|
|
} |
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
/** |
188
|
30 |
|
* @param Token $tokenStart |
189
|
30 |
|
* @param Token $tokenEnd |
190
|
30 |
|
* @return Collection |
191
|
20 |
|
*/ |
192
|
30 |
|
public function extractByTokens(Token $tokenStart, Token $tokenEnd) { |
193
|
|
|
|
194
|
|
|
$collection = new Collection(); |
195
|
|
|
$startIndex = $tokenStart->getIndex(); |
196
|
|
|
$endIndex = $tokenEnd->getIndex(); |
197
|
|
|
|
198
|
|
|
foreach ($this->getItems() as $token) { |
199
|
|
|
if ($token->getIndex() >= $startIndex and $token->getIndex() <= $endIndex) { |
200
|
|
|
$collection->append($token); |
201
|
177 |
|
} |
202
|
|
|
} |
203
|
177 |
|
|
204
|
177 |
|
|
205
|
177 |
|
return $collection; |
206
|
|
|
} |
207
|
177 |
|
|
208
|
177 |
|
|
209
|
177 |
|
/** |
210
|
118 |
|
* @param Query $query |
211
|
118 |
|
* @return Collection |
212
|
|
|
*/ |
213
|
|
|
public function find(Query $query) { |
214
|
177 |
|
$finder = new TokenFinder($this); |
215
|
|
|
return $finder->find($query); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return $this |
221
|
|
|
*/ |
222
|
15 |
|
public function storeContentHash() { |
223
|
15 |
|
$this->initialContentHash = $this->getContentHash(); |
224
|
15 |
|
return $this; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
} |
228
|
|
|
|