Overview

Namespaces

  • Yep
    • Tokenizer

Classes

  • Yep\Tokenizer\PhpTokenizer
  • Yep\Tokenizer\TokenIterator
  • Yep\Tokenizer\Tokenizer

Interfaces

  • Yep\Tokenizer\ITokenizer

Exceptions

  • Yep\Tokenizer\UnexpectedTokenException
  • Overview
  • Namespace
  • Class
  1: <?php
  2: namespace Yep\Tokenizer;
  3: 
  4: class TokenIterator implements \Iterator, \SeekableIterator {
  5:     /** @var int */
  6:     protected $position = -1;
  7: 
  8:     /** @var array */
  9:     protected $tokens = [];
 10: 
 11:     /**
 12:      * TokenIterator constructor
 13:      *
 14:      * @param array $tokens
 15:      */
 16:     public function __construct(array $tokens) {
 17:         $this->tokens = $tokens;
 18:     }
 19: 
 20:     // ↓ Iterator ↓
 21:     /**
 22:      * Returns current token
 23:      *
 24:      * @return array|null
 25:      */
 26:     public function current() {
 27:         if (isset($this->tokens[$this->position])) {
 28:             return $this->tokens[$this->position];
 29:         }
 30: 
 31:         return null;
 32:     }
 33: 
 34:     /**
 35:      * Moves to next position
 36:      */
 37:     public function next() {
 38:         ++$this->position;
 39:     }
 40: 
 41:     /**
 42:      * Returns current position
 43:      *
 44:      * @return int
 45:      */
 46:     public function key() {
 47:         return $this->position;
 48:     }
 49: 
 50:     /**
 51:      * Returns true, if current position is valid.
 52:      *
 53:      * @return bool
 54:      */
 55:     public function valid() {
 56:         return isset($this->tokens[$this->position]);
 57:     }
 58: 
 59:     /**
 60:      * Moves to begin
 61:      */
 62:     public function rewind() {
 63:         $this->position = -1;
 64:     }
 65: 
 66:     /**
 67:      * Moves to previous position
 68:      */
 69:     public function prev() {
 70:         --$this->position;
 71:     }
 72: 
 73:     /**
 74:      * Moves to next or previous position and returns token, if is valid.
 75:      *
 76:      * @param bool $back
 77:      * @return array|null
 78:      */
 79:     public function step($back = false) {
 80:         if ($back) {
 81:             $this->prev();
 82:         }
 83: 
 84:         if (!$back) {
 85:             $this->next();
 86:         }
 87: 
 88:         if ($this->valid()) {
 89:             return $this->current();
 90:         }
 91: 
 92:         return null;
 93:     }
 94: 
 95:     /**
 96:      * Moves to specific position
 97:      *
 98:      * @param int $position
 99:      */
100:     public function seek($position) {
101:         $this->position = $position;
102:     }
103: 
104:     // ↑ Iterator ↑
105: 
106:     /**
107:      * Checks the current token
108:      *
109:      * @return bool
110:      */
111:     public function currentIs() {
112:         $token = $this->current();
113: 
114:         if ($token === null) {
115:             return false;
116:         }
117: 
118:         $tokens = func_get_args();
119: 
120:         return
121:             in_array($token[ITokenizer::TYPE], $tokens, true)
122:             || in_array($token[ITokenizer::VALUE], $tokens, true);
123:     }
124: 
125:     /**
126:      * Returns current token value
127:      *
128:      * @return string|null
129:      */
130:     public function currentValue() {
131:         if (isset($this->tokens[$this->position][ITokenizer::VALUE])) {
132:             return $this->tokens[$this->position][ITokenizer::VALUE];
133:         }
134: 
135:         return null;
136:     }
137: 
138:     /**
139:      * Magic method which find and returns tokens
140:      *
141:      * @param array $tokens
142:      * @param bool  $expected
143:      * @param bool  $only_first
144:      * @param bool  $join
145:      * @param bool  $prev
146:      * @return array|null|string
147:      */
148:     protected function doSomethingMagic(array $tokens = [], $expected = true, $only_first = false, $join = false, $prev = false) {
149:         $found = [];
150: 
151:         while ($token = $this->step($prev)) {
152:             if (!empty($tokens) && (in_array($token[ITokenizer::TYPE], $tokens, true) || in_array($token[ITokenizer::VALUE], $tokens, true)) !== $expected) {
153:                 if ($only_first) {
154:                     continue;
155:                 }
156: 
157:                 $this->step(!$prev);
158:                 break;
159:             }
160: 
161:             $found[] = $token;
162: 
163:             if ($only_first) {
164:                 break;
165:             }
166:         }
167: 
168:         if ($prev) {
169:             $found = array_reverse($found);
170:         }
171: 
172:         if (!$join) {
173:             return $only_first ? array_shift($found) : $found;
174:         }
175: 
176:         $values = array_column($found, ITokenizer::VALUE);
177: 
178:         if (!$values) {
179:             return null;
180:         }
181: 
182:         return implode('', $values);
183:     }
184: 
185:     public function nextOne() {
186:         return $this->doSomethingMagic(func_get_args(), true, true); // expected, only_first
187:     }
188: 
189:     public function prevOne() {
190:         return $this->doSomethingMagic(func_get_args(), true, true, false, true); // expected, only_first, prev
191:     }
192: 
193:     public function nextOneNot() {
194:         return $this->doSomethingMagic(func_get_args(), false, true); // expected, only_first
195:     }
196: 
197:     public function prevOneNot() {
198:         return $this->doSomethingMagic(func_get_args(), false, true, false, true); // expected, only_first, prev
199:     }
200: 
201:     public function nextOneValue() {
202:         return $this->doSomethingMagic(func_get_args(), true, true, true); // expected, only_first, join
203:     }
204: 
205:     public function prevOneValue() {
206:         return $this->doSomethingMagic(func_get_args(), true, true, true, true); // expected, only_first, join, prev
207:     }
208: 
209:     public function nextAll() {
210:         return $this->doSomethingMagic(func_get_args()); // expected
211:     }
212: 
213:     public function prevAll() {
214:         return $this->doSomethingMagic(func_get_args(), true, false, false, true); // expected, prev
215:     }
216: 
217:     public function nextUntil() {
218:         return $this->doSomethingMagic(func_get_args(), false); // not expected
219:     }
220: 
221:     public function prevUntil() {
222:         return $this->doSomethingMagic(func_get_args(), false, false, false, true); // not expected, prev
223:     }
224: 
225:     public function joinNext() {
226:         return $this->doSomethingMagic(func_get_args(), true, false, true); // expected, join
227:     }
228: 
229:     public function joinPrev() {
230:         return $this->doSomethingMagic(func_get_args(), true, false, true, true); // expected, join, prev
231:     }
232: 
233:     public function joinNextUntil() {
234:         return $this->doSomethingMagic(func_get_args(), false, false, true); // not expected, join
235:     }
236: 
237:     public function joinPrevUntil() {
238:         return $this->doSomethingMagic(func_get_args(), false, false, true, true); // not expected, join, prev
239:     }
240: }
241: 
API documentation generated by ApiGen