|
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\Collection extractItems($offset, $length = null); |
|
17
|
|
|
* @method $this setItems($tokens) |
|
18
|
|
|
* |
|
19
|
|
|
* @package Funivan\PhpTokenizer |
|
20
|
|
|
*/ |
|
21
|
|
|
class Collection extends BaseCollection { |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* You can use this constant for access class name |
|
25
|
|
|
*/ |
|
26
|
|
|
const N = __CLASS__; |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Extract each value from token |
|
31
|
|
|
* |
|
32
|
|
|
* @return string |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __toString() { |
|
35
|
|
|
return $this->assemble(); |
|
36
|
|
|
} |
|
37
|
450 |
|
|
|
38
|
450 |
|
|
|
39
|
450 |
|
/** |
|
40
|
450 |
|
* |
|
41
|
|
|
* @param string $string |
|
42
|
|
|
* @return Collection |
|
43
|
|
|
* @throws Exception |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function createFromString($string) { |
|
46
|
|
|
$tokens = Helper::getTokensFromString($string); |
|
47
|
|
|
return new static($tokens); |
|
48
|
144 |
|
} |
|
49
|
144 |
|
|
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Remove all invalid tokens in collection |
|
57
|
|
|
* Refresh index. |
|
58
|
|
|
* |
|
59
|
435 |
|
* @return $this |
|
60
|
435 |
|
*/ |
|
61
|
435 |
|
public function refresh() { |
|
62
|
|
|
$string = $this->assemble(); |
|
63
|
|
|
$this->cleanCollection(); |
|
64
|
|
|
|
|
65
|
|
|
$tokens = Helper::getTokensFromString($string); |
|
66
|
|
|
$this->setItems($tokens); |
|
67
|
|
|
|
|
68
|
9 |
|
$this->rewind(); |
|
69
|
9 |
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param int $step |
|
75
|
|
|
* @return Token |
|
76
|
450 |
|
*/ |
|
77
|
450 |
|
public function getPrevious($step = 1) { |
|
78
|
|
|
$item = parent::getPrevious($step); |
|
79
|
|
|
if ($item === null) { |
|
80
|
|
|
$item = new Token(); |
|
81
|
|
|
} |
|
82
|
|
|
return $item; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
441 |
|
/** |
|
87
|
441 |
|
* @param int $step |
|
88
|
|
|
* @return Token |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getNext($step = 1) { |
|
91
|
|
|
$item = parent::getNext($step); |
|
92
|
|
|
if ($item === null) { |
|
93
|
|
|
$item = new Token(); |
|
94
|
450 |
|
} |
|
95
|
450 |
|
return $item; |
|
96
|
|
|
} |
|
97
|
450 |
|
|
|
98
|
435 |
|
|
|
99
|
66 |
|
/** |
|
100
|
|
|
* @codeCoverageIgnore |
|
101
|
435 |
|
* @deprecated |
|
102
|
300 |
|
* @see createFromString |
|
103
|
|
|
* @param string $string |
|
104
|
450 |
|
* @return Collection |
|
105
|
|
|
*/ |
|
106
|
|
|
public static function initFromString($string) { |
|
107
|
|
|
trigger_error(__CLASS__ . '::' . __METHOD__ . ' deprecated and will be removed in 0.1.3 Use ' . __CLASS__ . '::createFromString', E_USER_DEPRECATED); |
|
108
|
|
|
return self::createFromString($string); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Remove invalid tokens from collection |
|
114
|
9 |
|
* |
|
115
|
9 |
|
* @return $this |
|
116
|
9 |
|
*/ |
|
117
|
|
|
private function cleanCollection() { |
|
118
|
9 |
|
foreach ($this as $index => $token) { |
|
119
|
9 |
|
if ($token->isValid()) { |
|
120
|
|
|
continue; |
|
121
|
9 |
|
} |
|
122
|
9 |
|
unset($this->items[$index]); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
3 |
|
* @param Token $tokenStart |
|
131
|
3 |
|
* @param Token $tokenEnd |
|
132
|
3 |
|
* @return Collection |
|
133
|
3 |
|
*/ |
|
134
|
2 |
|
public function extractByTokens(Token $tokenStart, Token $tokenEnd) { |
|
135
|
3 |
|
|
|
136
|
|
|
$collection = new Collection(); |
|
137
|
|
|
$startIndex = $tokenStart->getIndex(); |
|
138
|
|
|
$endIndex = $tokenEnd->getIndex(); |
|
139
|
|
|
|
|
140
|
|
|
foreach ($this->getTokens() as $token) { |
|
141
|
|
|
if ($token->getIndex() >= $startIndex and $token->getIndex() <= $endIndex) { |
|
142
|
|
|
$collection->append($token); |
|
143
|
6 |
|
} |
|
144
|
6 |
|
} |
|
145
|
6 |
|
|
|
146
|
6 |
|
|
|
147
|
4 |
|
return $collection; |
|
148
|
6 |
|
} |
|
149
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param Query $query |
|
153
|
|
|
* @return Collection |
|
154
|
|
|
*/ |
|
155
|
|
|
public function find(Query $query) { |
|
156
|
|
|
$finder = new TokenFinder($this); |
|
157
|
|
|
return $finder->find($query); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
|
|
161
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
|