Ejemplo n.º 1
0
 protected function parse_every($type = 'every', $opening_tag, $condition_contents, $exact_match, $template_contents, $content_vars, $index_in_group)
 {
     $tag = new PerchXMLTag($opening_tag);
     $positive = $condition_contents;
     $negative = '';
     // else condition
     if (strpos($condition_contents, 'perch:else') > 0) {
         $parts = preg_split('/<perch:else\\s*\\/>/', $condition_contents);
         if (is_array($parts) && count($parts) > 1) {
             $positive = $parts[0];
             $negative = $parts[1];
         }
     }
     if ($tag->count()) {
         $count = (int) $tag->count();
         $offset = 0;
         if ($count !== 0 && $index_in_group % $count == 0) {
             $template_contents = str_replace($exact_match, $positive, $template_contents);
         } else {
             $template_contents = str_replace($exact_match, $negative, $template_contents);
         }
     } elseif ($tag->nth_child()) {
         $nth_child = $tag->nth_child();
         $nths = array(0);
         if (is_numeric($nth_child)) {
             $nths[] = (int) $nth_child;
         } else {
             $multiplier = 0;
             $offset = 0;
             switch ($nth_child) {
                 case 'odd':
                     $multiplier = 2;
                     $offset = 1;
                     break;
                 case 'even':
                     $multiplier = 2;
                     $offset = 0;
                     break;
                 default:
                     $s = '/([\\+-]{0,1}[0-9]*)n([\\+-]{0,1}[0-9]+){0,1}/';
                     if (preg_match($s, $tag->nth_child(), $matches)) {
                         if (isset($matches[1]) && $matches[1] != '' && $matches[1] != '-') {
                             $multiplier = (int) $matches[1];
                         } else {
                             if ($matches[1] == '-') {
                                 $multiplier = -1;
                             } else {
                                 $multiplier = 1;
                             }
                         }
                         if (isset($matches[2])) {
                             $offset = (int) $matches[2];
                         } else {
                             $offset = 0;
                         }
                     }
                     break;
             }
             $n = 0;
             if ($multiplier > 0) {
                 while ($n < 1000 && max($nths) <= $index_in_group) {
                     $nths[] = $multiplier * $n + $offset;
                     $n++;
                 }
             } else {
                 while ($n < 1000) {
                     $nth = $multiplier * $n + $offset;
                     if ($nth > 0) {
                         $nths[] = $nth;
                     } else {
                         break;
                     }
                     $n++;
                 }
             }
         }
         if (PerchUtil::count($nths)) {
             if (in_array($index_in_group, $nths)) {
                 $template_contents = str_replace($exact_match, $positive, $template_contents);
             } else {
                 $template_contents = str_replace($exact_match, $negative, $template_contents);
             }
         } else {
             $template_contents = str_replace($exact_match, $negative, $template_contents);
         }
     } else {
         // No count or nth-child, so scrub it.
         $template_contents = str_replace($exact_match, $negative, $template_contents);
     }
     return $template_contents;
 }