/** * Executes a foreach expression. * * The expression has the form: 'foo as bar', where foo is an array and bar the value. * * @param string $expression foreach expression * @param scope $scope * * @throws ParsingException * @return Scope[] */ public function execute($expression, Scope $scope) { $metaModel = new MetaModel(); $metaModel->addContainer($scope); $subScopes = []; $parseResult = $this->forEachParser->parse($expression); if (is_array($parseResult['array'])) { $functionName = $scope->getFunction($parseResult['array']['functionName']); $array = call_user_func_array($functionName, array_map(function ($parameter) use($metaModel) { return $metaModel->run($parameter); }, $parseResult['array']['parameters'])); } else { $array = $metaModel->run($parseResult['array']); } foreach ($array as $key => $value) { // New sub-scope $subScope = new Scope($scope); $subScope->bind($parseResult['value'], $value); if (isset($parseResult['key'])) { $subScope->bind($parseResult['key'], $key); } $subScopes[] = $subScope; } return $subScopes; }
/** * @expectedException \Xport\Parser\ParsingException */ public function testInvalidString12() { $parser = new ForEachParser(); $parser->parse('foo as bar(bam => bim)'); }