Ejemplo n.º 1
0
 private static function tokenize($str)
 {
     // testing out whitespace removal/reduction
     $str = preg_replace('/\\s{2,}/', "\n", $str);
     $split = preg_split("/(\\{[^\\{\\}]++\\})/", $str, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
     $tokenFormatBeginLength = strlen(TokenType::T_FORMAT_BEGIN);
     $tokenFormatEndLength = strlen(TokenType::T_FORMAT_END);
     $tokens = [];
     foreach ($split as $value) {
         if ($value[0] === TokenType::T_FORMAT_BEGIN) {
             // regex does not currently verify min length
             $symbol = $value[$tokenFormatBeginLength];
             $type = TokenType::type($symbol);
             if ($type) {
                 if ($type === TokenType::T_CLOSE) {
                     $tokens[] = TokenType::T_CLOSE;
                 } else {
                     $tokenName = substr($value, $tokenFormatBeginLength + 1, -$tokenFormatEndLength);
                     // check for evaluation entries
                     if ($type === TokenType::T_VARIABLE || $type === TokenType::T_FILTER) {
                         $tokens[] = new EvalNameToken($type, $tokenName);
                     } else {
                         $tokens[] = new NameToken($type, $tokenName);
                     }
                 }
             } else {
                 $tokens[] = $value;
             }
         } else {
             $tokens[] = $value;
         }
     }
     return $tokens;
 }