示例#1
0
 /**
  * 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;
 }
示例#2
0
 public function testEscape()
 {
     $scope = new Scope();
     $scope->bind('foo', 'l\'baré');
     $twigExecutor = new TwigExecutor($scope->getFunctions());
     $this->assertEquals('l\'baré', $twigExecutor->parse('{{ foo }}', $scope));
 }
示例#3
0
 public function testToArray()
 {
     $scope = new Scope();
     $scope->bind('foo', 'bar');
     $array = $scope->toArray();
     $this->assertEquals(['foo' => 'bar'], $array);
 }
示例#4
0
 public function testWithMethodCall()
 {
     $scope = new Scope();
     $scope->bind('foo', new FixtureClass());
     $parser = new ForEachExecutor();
     $subScopes = $parser->execute('foo.getStuff() as bar', $scope);
     $this->assertCount(2, $subScopes);
     foreach ($subScopes as $i => $subScope) {
         $this->assertInstanceOf('Xport\\Parser\\Scope', $subScope);
         $this->assertEquals('barValue' . $i, $subScope->get('bar'));
     }
 }
示例#5
0
 /**
  * Bind a value to a name.
  *
  * @param string $name
  * @param mixed  $value
  */
 public function bind($name, $value)
 {
     $this->scope->bind($name, $value);
 }