Exemplo n.º 1
0
 /**
  * @param $segmentText
  * @throws \RuntimeException
  * @throws \Jig\JigException
  */
 public function processForeachStart($segmentText)
 {
     //find the variable and replace it with new version
     $pattern = '/foreach\\s+(\\$?\\w+[^\\s\\=]*)\\s/u';
     $matchCount = preg_match($pattern, $segmentText, $matches, PREG_OFFSET_CAPTURE);
     if ($matchCount === 0) {
         throw new JigException("Could not extract variable to foreach over from [{$segmentText}].");
     }
     $foreachItem = $matches[1][0];
     $varPosition = $matches[1][1];
     $segmentText = str_replace('foreach', 'foreach (', $segmentText);
     $segment = new CodeTemplateSegment($foreachItem);
     $replace = $segment->getCodeString($this->parsedTemplate, CodeTemplateSegment::NO_FILTER | CodeTemplateSegment::NO_PHP | CodeTemplateSegment::NO_OUTPUT);
     $segmentText = str_replace($foreachItem, $replace, $segmentText);
     $this->addCode($segmentText . '){ ');
     $dependentVariablesPosition = $varPosition + strlen($foreachItem);
     $pattern = '/\\s+(\\$\\w+)\\s?/u';
     $matchCount = preg_match_all($pattern, $segmentText, $matches, PREG_PATTERN_ORDER, $dependentVariablesPosition);
     if ($matchCount === 0) {
         throw new \RuntimeException("Failed to parse foreach correctly.");
     }
     foreach ($matches[1] as $variableName) {
         $this->parsedTemplate->addLocalVariable($variableName);
     }
 }