1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Utils;
9:
10:
11: 12: 13:
14: class TokenIterator
15: {
16:
17: public $tokens;
18:
19:
20: public $position = -1;
21:
22:
23: public $ignored = array();
24:
25:
26: 27: 28:
29: public function __construct(array $tokens)
30: {
31: $this->tokens = $tokens;
32: }
33:
34:
35: 36: 37: 38:
39: public function currentToken()
40: {
41: return isset($this->tokens[$this->position])
42: ? $this->tokens[$this->position]
43: : NULL;
44: }
45:
46:
47: 48: 49: 50:
51: public function currentValue()
52: {
53: return isset($this->tokens[$this->position])
54: ? $this->tokens[$this->position][Tokenizer::VALUE]
55: : NULL;
56: }
57:
58:
59: 60: 61: 62: 63:
64: public function nextToken()
65: {
66: return $this->scan(func_get_args(), TRUE, TRUE);
67: }
68:
69:
70: 71: 72: 73: 74:
75: public function nextValue()
76: {
77: return $this->scan(func_get_args(), TRUE, TRUE, TRUE);
78: }
79:
80:
81: 82: 83: 84: 85:
86: public function nextAll()
87: {
88: return $this->scan(func_get_args(), FALSE, TRUE);
89: }
90:
91:
92: 93: 94: 95: 96:
97: public function nextUntil($arg)
98: {
99: return $this->scan(func_get_args(), FALSE, TRUE, FALSE, TRUE);
100: }
101:
102:
103: 104: 105: 106: 107:
108: public function joinAll()
109: {
110: return $this->scan(func_get_args(), FALSE, TRUE, TRUE);
111: }
112:
113:
114: 115: 116: 117: 118:
119: public function joinUntil($arg)
120: {
121: return $this->scan(func_get_args(), FALSE, TRUE, TRUE, TRUE);
122: }
123:
124:
125: 126: 127: 128: 129:
130: public function isCurrent($arg)
131: {
132: if (!isset($this->tokens[$this->position])) {
133: return FALSE;
134: }
135: $args = func_get_args();
136: $token = $this->tokens[$this->position];
137: return in_array($token[Tokenizer::VALUE], $args, TRUE)
138: || (isset($token[Tokenizer::TYPE]) && in_array($token[Tokenizer::TYPE], $args, TRUE));
139: }
140:
141:
142: 143: 144: 145: 146:
147: public function isNext()
148: {
149: return (bool) $this->scan(func_get_args(), TRUE, FALSE);
150: }
151:
152:
153: 154: 155: 156: 157:
158: public function isPrev()
159: {
160: return (bool) $this->scan(func_get_args(), TRUE, FALSE, FALSE, FALSE, TRUE);
161: }
162:
163:
164: 165: 166:
167: public function reset()
168: {
169: $this->position = -1;
170: return $this;
171: }
172:
173:
174: 175: 176:
177: protected function next()
178: {
179: $this->position++;
180: }
181:
182:
183: 184: 185: 186: 187: 188: 189: 190: 191: 192:
193: protected function scan($wanted, $onlyFirst, $advance, $strings = FALSE, $until = FALSE, $prev = FALSE)
194: {
195: $res = $onlyFirst ? NULL : ($strings ? '' : array());
196: $pos = $this->position + ($prev ? -1 : 1);
197: do {
198: if (!isset($this->tokens[$pos])) {
199: if (!$wanted && $advance && !$prev && $pos <= count($this->tokens)) {
200: $this->next();
201: }
202: return $res;
203: }
204:
205: $token = $this->tokens[$pos];
206: $type = isset($token[Tokenizer::TYPE]) ? $token[Tokenizer::TYPE] : NULL;
207: if (!$wanted || (in_array($token[Tokenizer::VALUE], $wanted, TRUE) || in_array($type, $wanted, TRUE)) ^ $until) {
208: while ($advance && !$prev && $pos > $this->position) {
209: $this->next();
210: }
211:
212: if ($onlyFirst) {
213: return $strings ? $token[Tokenizer::VALUE] : $token;
214: } elseif ($strings) {
215: $res .= $token[Tokenizer::VALUE];
216: } else {
217: $res[] = $token;
218: }
219:
220: } elseif ($until || !in_array($type, $this->ignored, TRUE)) {
221: return $res;
222: }
223: $pos += $prev ? -1 : 1;
224: } while (TRUE);
225: }
226:
227: }
228: