/** * Execute the token/data parsing * * @param array $data Data to populate into the structure * @return \SalesforceEng\Breakout\Document instance */ public function execute(array $data) { $document = $this->document; while ($this->container->valid()) { $current = $this->container->current(); $this->container->next(); if (is_array($current)) { $tokenType = $current[0]->getToken(); switch ($tokenType) { case 'if': $ifBlock = new Block\BlockIf($data, $current, $document); $document = $ifBlock->execute(); break; case 'for': $forBlock = new Block\BlockFor($data, $current, $document); $document = $forBlock->execute(); break; case 'raw': $rawBlock = new Block\BlockRaw($data, $current, $document); $document = $rawBlock->execute(); } } else { $document = $this->handleVariable($data, $current, $document); } } return $document; }
/** * Test the "if" block with an "else" included */ public function testBlockIfWithElse() { // Use the tokenizer to build the tokens $t = new Tokenizer(); $document = 'this is a {% if bar %}inside{% else %}in else{% endif %} here'; $tokens = $t->execute($document); // Data will be found, goes into if $data = ['bar' => 'this is set!']; $block = new BlockIf($data, $tokens[0], new Document($document)); $this->assertEquals('this is a inside here', $block->execute()); // Data not found, goes into else $data = []; $block = new BlockIf($data, $tokens[0], new Document($document)); $this->assertEquals('this is a in else here', $block->execute()); }