Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy

Classes

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

Interfaces

  • IHtmlString

Exceptions

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