Exemplo n.º 1
0
 /**
  * Non-regression test
  */
 public function testSuccessiveParsingWithFunctions()
 {
     $scope = new Scope();
     $scope->bindFunction('test', function ($str) {
         return strtoupper($str);
     });
     $twigParser = new TwigExecutor($scope->getFunctions());
     $this->assertEquals('BAR', $twigParser->parse('{{ test("bar") }}', $scope));
     $this->assertEquals('BAR', $twigParser->parse('{{ test("bar") }}', $scope));
 }
Exemplo n.º 2
0
 public function testBindFunction()
 {
     $scope = new Scope();
     $scope->bindFunction('foo', function () {
         return 'hello';
     });
     $function = $scope->getFunction('foo');
     $this->assertEquals('hello', $function());
     $functions = $scope->getFunctions();
     $this->assertCount(1, $functions);
     $this->assertEquals('hello', $functions['foo']());
 }
Exemplo n.º 3
0
 public function testWithFunctionMultipleParameters()
 {
     $global = new \stdClass();
     $global->value = 5;
     $scope = new Scope();
     $scope->bind('foo', ['0', '1']);
     $scope->bind('global', $global);
     $scope->bindFunction('blah', function ($parameters, $global) {
         $a = [];
         foreach ($parameters as $i) {
             $a['bimIndex' . $global . $i] = 'barValue' . $global . $i;
         }
         return $a;
     });
     $parser = new ForEachExecutor();
     $subScopes = $parser->execute('blah(foo, global.value) as bim => bar', $scope);
     $this->assertCount(2, $subScopes);
     foreach ($subScopes as $i => $subScope) {
         $this->assertInstanceOf('Xport\\Parser\\Scope', $subScope);
         $this->assertEquals('bimIndex' . $global->value . $i, $subScope->get('bim'));
         $this->assertEquals('barValue' . $global->value . $i, $subScope->get('bar'));
     }
 }
Exemplo n.º 4
0
 /**
  * Bind a function to a name.
  *
  * @param string   $name
  * @param callable $callable
  */
 public function bindFunction($name, $callable)
 {
     $this->scope->bindFunction($name, $callable);
 }