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
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Utils
  • none
  • Tracy
    • Bridges
      • Nette

Classes

  • ArrayHash
  • ArrayList
  • Arrays
  • Callback
  • DateTime
  • FileSystem
  • Finder
  • Html
  • Image
  • Json
  • LimitedScope
  • MimeTypeDetector
  • ObjectMixin
  • Paginator
  • Random
  • Strings
  • TokenIterator
  • Tokenizer
  • Validators

Interfaces

  • IHtmlString

Exceptions

  • AssertionException
  • ImageException
  • JsonException
  • RegexpException
  • TokenizerException
  • UnknownImageFileException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  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:  */
 14: class TokenIterator
 15: {
 16:     /** @var array */
 17:     public $tokens;
 18: 
 19:     /** @var int */
 20:     public $position = -1;
 21: 
 22:     /** @var array */
 23:     public $ignored = array();
 24: 
 25: 
 26:     /**
 27:      * @param array[]
 28:      */
 29:     public function __construct(array $tokens)
 30:     {
 31:         $this->tokens = $tokens;
 32:     }
 33: 
 34: 
 35:     /**
 36:      * Returns current token.
 37:      * @return array|NULL
 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:      * Returns current token value.
 49:      * @return string|NULL
 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:      * Returns next token.
 61:      * @param  int|string  (optional) desired token type or value
 62:      * @return array|NULL
 63:      */
 64:     public function nextToken()
 65:     {
 66:         return $this->scan(func_get_args(), TRUE, TRUE); // onlyFirst, advance
 67:     }
 68: 
 69: 
 70:     /**
 71:      * Returns next token value.
 72:      * @param  int|string  (optional) desired token type or value
 73:      * @return string|NULL
 74:      */
 75:     public function nextValue()
 76:     {
 77:         return $this->scan(func_get_args(), TRUE, TRUE, TRUE); // onlyFirst, advance, strings
 78:     }
 79: 
 80: 
 81:     /**
 82:      * Returns all next tokens.
 83:      * @param  int|string  (optional) desired token type or value
 84:      * @return array[]
 85:      */
 86:     public function nextAll()
 87:     {
 88:         return $this->scan(func_get_args(), FALSE, TRUE); // advance
 89:     }
 90: 
 91: 
 92:     /**
 93:      * Returns all next tokens until it sees a given token type or value.
 94:      * @param  int|string  token type or value to stop before
 95:      * @return array[]
 96:      */
 97:     public function nextUntil($arg)
 98:     {
 99:         return $this->scan(func_get_args(), FALSE, TRUE, FALSE, TRUE); // advance, until
100:     }
101: 
102: 
103:     /**
104:      * Returns concatenation of all next token values.
105:      * @param  int|string  (optional) token type or value to be joined
106:      * @return string
107:      */
108:     public function joinAll()
109:     {
110:         return $this->scan(func_get_args(), FALSE, TRUE, TRUE); // advance, strings
111:     }
112: 
113: 
114:     /**
115:      * Returns concatenation of all next tokens until it sees a given token type or value.
116:      * @param  int|string  token type or value to stop before
117:      * @return string
118:      */
119:     public function joinUntil($arg)
120:     {
121:         return $this->scan(func_get_args(), FALSE, TRUE, TRUE, TRUE); // advance, strings, until
122:     }
123: 
124: 
125:     /**
126:      * Checks the current token.
127:      * @param  int|string  token type or value
128:      * @return bool
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:      * Checks the next token existence.
144:      * @param  int|string  (optional) token type or value
145:      * @return bool
146:      */
147:     public function isNext()
148:     {
149:         return (bool) $this->scan(func_get_args(), TRUE, FALSE); // onlyFirst
150:     }
151: 
152: 
153:     /**
154:      * Checks the previous token existence.
155:      * @param  int|string  (optional) token type or value
156:      * @return bool
157:      */
158:     public function isPrev()
159:     {
160:         return (bool) $this->scan(func_get_args(), TRUE, FALSE, FALSE, FALSE, TRUE); // onlyFirst, prev
161:     }
162: 
163: 
164:     /**
165:      * @return self
166:      */
167:     public function reset()
168:     {
169:         $this->position = -1;
170:         return $this;
171:     }
172: 
173: 
174:     /**
175:      * Moves cursor to next token.
176:      */
177:     protected function next()
178:     {
179:         $this->position++;
180:     }
181: 
182: 
183:     /**
184:      * Looks for (first) (not) wanted tokens.
185:      * @param  array of desired token types or values
186:      * @param  bool
187:      * @param  bool
188:      * @param  bool
189:      * @param  bool
190:      * @param  bool
191:      * @return mixed
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: 
Nette 2.3-20161221 API API documentation generated by ApiGen 2.8.0