Beispiel #1
0
 protected function tokenWithEachToCode(Token $token)
 {
     $expr = self::parseExpression($token->getProp('select'));
     $arr_id = '$' . $this->varialize($expr) . '_items';
     $each_token = new Token('each', 'double', array('select' => '`' . $arr_id . '`', 'as' => $token->getProp('as'), 'key' => $token->getProp('key'), 'check_traversable' => 'false'));
     if ($separator = $this->findSeparator($token)) {
         $each_token->addChild($separator);
     }
     $code = "<?php\n";
     $code .= $arr_id . ' = ' . $expr . ";\n";
     $code .= "if (" . $arr_id . " && (is_array(" . $arr_id . ") || " . $arr_id . " instanceof Traversable)) {\n";
     $code .= $this->getAdderPlaceholderCode($arr_id);
     $code .= "if (count(" . $arr_id . ")) {\n";
     $code .= "?>";
     $items = array();
     foreach ($token->children as $child) {
         if ($child->name == 'item') {
             $items[] = $child;
         }
     }
     usort($items, function ($a, $b) {
         $ta = $a->getProp('test') ? 1 : 0;
         $tb = $b->getProp('test') ? 1 : 0;
         return $tb - $ta;
     });
     $all_subroot = true;
     $target_token = $each_token;
     foreach ($items as $num => $item) {
         $test = $item->getProp('test');
         $item_subroot = $item->getProp('subroot');
         if (!$item_subroot || $item_subroot == 'false') {
             $all_subroot = false;
         }
         if (!$test) {
             $test = 'true';
         }
         $cond_token = new Token($num == 0 ? 'if' : 'elseif', 'double', array('test' => $test));
         foreach ($item->getChildren() as $item_child) {
             $cond_token->addChild($item_child);
         }
         $target_token->addChild($cond_token);
         $target_token = $cond_token;
     }
     if ($all_subroot) {
         $each_token->setProp('subroot', 'true');
     }
     $in_items = false;
     $each_added = false;
     foreach ($token->children as $child) {
         if ($child->name == 'item' && !$in_items) {
             $in_items = true;
         }
         if (!$in_items) {
             $code .= $this->getTokenCode($child, $token);
             continue;
         }
         if (!$each_added) {
             $code .= $this->getTokenCode($each_token, $token);
             $each_added = true;
         }
         if ($child->name == 'item' || $child->isEmpty()) {
             continue;
         }
         $in_items = false;
         $code .= $this->getTokenCode($child, $token);
     }
     $code .= "<?php\n}\n}\n?>";
     return $code;
 }