/**
  * {@inheritdoc}
  * @throws ParsingException
  */
 public function build(Sheet $sheet, $yamlContent, Scope $scope)
 {
     // Init TwigExecutor with all user functions.
     $this->twigExecutor = new TwigExecutor($scope->getFunctions());
     // Table.
     $table = new Table();
     $sheet->addTable($table);
     if (isset($yamlContent) && isset($yamlContent['label'])) {
         $table->setLabel($this->twigExecutor->parse($yamlContent['label'], $scope));
     }
     // Columns.
     if (!isset($yamlContent) || !array_key_exists('columns', $yamlContent)) {
         throw new ParsingException("'content' of type 'VerticalTable' must contains 'columns'");
     }
     foreach ($yamlContent['columns'] as $yamlColumn) {
         $this->parseColumn($table, $yamlColumn, $scope);
     }
     // Lines.
     if (!isset($yamlContent) || !array_key_exists('lines', $yamlContent)) {
         throw new ParsingException("'content' of type 'VerticalTable' must contains 'lines'");
     }
     foreach ($yamlContent['lines'] as $yamlLine) {
         $this->parseLine($table, $yamlLine, $scope);
     }
 }
示例#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
 /**
  * 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;
 }
示例#4
0
 /**
  * Build a model.
  *
  * @param MappingReader $mappingReader
  * @throws ParsingException
  * @return Document
  */
 public function build(MappingReader $mappingReader)
 {
     // Init TwigExecutor with all user functions.
     $this->twigExecutor = new TwigExecutor($this->scope->getFunctions());
     $document = new Document();
     $this->parseRoot($document, $mappingReader->getMapping(), $this->scope);
     return $document;
 }
示例#5
0
 /**
  * @expectedException \InvalidArgumentException
  * @expectedExceptionMessage Unknown function 'foo'
  */
 public function testGetNotBoundFunction()
 {
     $scope = new Scope();
     $scope->getFunction('foo');
 }
示例#6
0
 /**
  * Parse a Twig expression.
  *
  * @param string $expression Twig expression
  * @param Scope  $scope
  *
  * @return string
  */
 public function parse($expression, Scope $scope)
 {
     return $this->twig->render($expression, $scope->toArray());
 }
示例#7
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'));
     }
 }