Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
      • Traits
    • Reflection
    • Security
    • Tokenizer
    • Utils
  • Tracy
    • Bridges
      • Nette
  • none

Classes

  • ArrayHash
  • ArrayList
  • Arrays
  • Callback
  • DateTime
  • FileSystem
  • Finder
  • Html
  • Image
  • Json
  • ObjectHelpers
  • ObjectMixin
  • Paginator
  • Random
  • Reflection
  • SafeStream
  • Strings
  • TokenIterator
  • Tokenizer
  • Validators

Interfaces

  • IHtmlString

Exceptions

  • AssertionException
  • ImageException
  • JsonException
  • RegexpException
  • TokenizerException
  • UnknownImageFileException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Utils;
  9: 
 10: 
 11: /**
 12:  * Traversing helper.
 13:  * @deprecated use similar Nette\Tokenizer\Stream
 14:  */
 15: class TokenIterator
 16: {
 17:     /** @var array */
 18:     public $tokens;
 19: 
 20:     /** @var int */
 21:     public $position = -1;
 22: 
 23:     /** @var array */
 24:     public $ignored = [];
 25: 
 26: 
 27:     /**
 28:      * @param array[]
 29:      */
 30:     public function __construct(array $tokens)
 31:     {
 32:         $this->tokens = $tokens;
 33:     }
 34: 
 35: 
 36:     /**
 37:      * Returns current token.
 38:      * @return array|null
 39:      */
 40:     public function currentToken()
 41:     {
 42:         return isset($this->tokens[$this->position])
 43:             ? $this->tokens[$this->position]
 44:             : null;
 45:     }
 46: 
 47: 
 48:     /**
 49:      * Returns current token value.
 50:      * @return string|null
 51:      */
 52:     public function currentValue()
 53:     {
 54:         return isset($this->tokens[$this->position])
 55:             ? $this->tokens[$this->position][Tokenizer::VALUE]
 56:             : null;
 57:     }
 58: 
 59: 
 60:     /**
 61:      * Returns next token.
 62:      * @param  int|string  (optional) desired token type or value
 63:      * @return array|null
 64:      */
 65:     public function nextToken()
 66:     {
 67:         return $this->scan(func_get_args(), true, true); // onlyFirst, advance
 68:     }
 69: 
 70: 
 71:     /**
 72:      * Returns next token value.
 73:      * @param  int|string  (optional) desired token type or value
 74:      * @return string|null
 75:      */
 76:     public function nextValue()
 77:     {
 78:         return $this->scan(func_get_args(), true, true, true); // onlyFirst, advance, strings
 79:     }
 80: 
 81: 
 82:     /**
 83:      * Returns all next tokens.
 84:      * @param  int|string  (optional) desired token type or value
 85:      * @return array[]
 86:      */
 87:     public function nextAll()
 88:     {
 89:         return $this->scan(func_get_args(), false, true); // advance
 90:     }
 91: 
 92: 
 93:     /**
 94:      * Returns all next tokens until it sees a given token type or value.
 95:      * @param  int|string  token type or value to stop before
 96:      * @return array[]
 97:      */
 98:     public function nextUntil($arg)
 99:     {
100:         return $this->scan(func_get_args(), false, true, false, true); // advance, until
101:     }
102: 
103: 
104:     /**
105:      * Returns concatenation of all next token values.
106:      * @param  int|string  (optional) token type or value to be joined
107:      * @return string
108:      */
109:     public function joinAll()
110:     {
111:         return $this->scan(func_get_args(), false, true, true); // advance, strings
112:     }
113: 
114: 
115:     /**
116:      * Returns concatenation of all next tokens until it sees a given token type or value.
117:      * @param  int|string  token type or value to stop before
118:      * @return string
119:      */
120:     public function joinUntil($arg)
121:     {
122:         return $this->scan(func_get_args(), false, true, true, true); // advance, strings, until
123:     }
124: 
125: 
126:     /**
127:      * Checks the current token.
128:      * @param  int|string  token type or value
129:      * @return bool
130:      */
131:     public function isCurrent($arg)
132:     {
133:         if (!isset($this->tokens[$this->position])) {
134:             return false;
135:         }
136:         $args = func_get_args();
137:         $token = $this->tokens[$this->position];
138:         return in_array($token[Tokenizer::VALUE], $args, true)
139:             || (isset($token[Tokenizer::TYPE]) && in_array($token[Tokenizer::TYPE], $args, true));
140:     }
141: 
142: 
143:     /**
144:      * Checks the next token existence.
145:      * @param  int|string  (optional) token type or value
146:      * @return bool
147:      */
148:     public function isNext()
149:     {
150:         return (bool) $this->scan(func_get_args(), true, false); // onlyFirst
151:     }
152: 
153: 
154:     /**
155:      * Checks the previous token existence.
156:      * @param  int|string  (optional) token type or value
157:      * @return bool
158:      */
159:     public function isPrev()
160:     {
161:         return (bool) $this->scan(func_get_args(), true, false, false, false, true); // onlyFirst, prev
162:     }
163: 
164: 
165:     /**
166:      * @return static
167:      */
168:     public function reset()
169:     {
170:         $this->position = -1;
171:         return $this;
172:     }
173: 
174: 
175:     /**
176:      * Moves cursor to next token.
177:      */
178:     protected function next()
179:     {
180:         $this->position++;
181:     }
182: 
183: 
184:     /**
185:      * Looks for (first) (not) wanted tokens.
186:      * @param  array of desired token types or values
187:      * @param  bool
188:      * @param  bool
189:      * @param  bool
190:      * @param  bool
191:      * @param  bool
192:      * @return mixed
193:      */
194:     protected function scan($wanted, $onlyFirst, $advance, $strings = false, $until = false, $prev = false)
195:     {
196:         $res = $onlyFirst ? null : ($strings ? '' : []);
197:         $pos = $this->position + ($prev ? -1 : 1);
198:         do {
199:             if (!isset($this->tokens[$pos])) {
200:                 if (!$wanted && $advance && !$prev && $pos <= count($this->tokens)) {
201:                     $this->next();
202:                 }
203:                 return $res;
204:             }
205: 
206:             $token = $this->tokens[$pos];
207:             $type = isset($token[Tokenizer::TYPE]) ? $token[Tokenizer::TYPE] : null;
208:             if (!$wanted || (in_array($token[Tokenizer::VALUE], $wanted, true) || in_array($type, $wanted, true)) ^ $until) {
209:                 while ($advance && !$prev && $pos > $this->position) {
210:                     $this->next();
211:                 }
212: 
213:                 if ($onlyFirst) {
214:                     return $strings ? $token[Tokenizer::VALUE] : $token;
215:                 } elseif ($strings) {
216:                     $res .= $token[Tokenizer::VALUE];
217:                 } else {
218:                     $res[] = $token;
219:                 }
220: 
221:             } elseif ($until || !in_array($type, $this->ignored, true)) {
222:                 return $res;
223:             }
224:             $pos += $prev ? -1 : 1;
225:         } while (true);
226:     }
227: }
228: 
Nette 2.4-20180918 API API documentation generated by ApiGen 2.8.0