public function create($usage, $template) { $_this = $this; $config = $this->parse($usage); if (!$template instanceof Template) { $template = new Template($template); } $template->replaceTokens('#\\{(?:[A-Z]+[A-Z_0-9]*|@[-\\w]+)\\}#', function ($m) use($config, $_this) { $tokenId = \substr($m[0], 1, -1); if ($tokenId[0] === '@') { return array('expression', $tokenId); } if (isset($config['tokens'][$tokenId])) { return array('expression', '@' . $config['tokens'][$tokenId]); } if ($tokenId === $config['passthroughToken']) { return array('passthrough'); } if ($_this->isFilter($tokenId)) { throw new RuntimeException('Token {' . $tokenId . '} is ambiguous or undefined'); } return array('expression', '$' . $tokenId); }); $return = array('bbcode' => $config['bbcode'], 'bbcodeName' => $config['bbcodeName'], 'tag' => $config['tag']); $return['tag']->template = $template; return $return; }
/** * @testdox replaceTokens() resets isNormalized */ public function testReplaceTokensResetsIsNormalized() { $mock = $this->getMockBuilder('s9e\\TextFormatter\\Configurator\\TemplateNormalizer')->disableOriginalConstructor()->getMock(); $template = new Template('<br/>'); $template->normalize($mock); $this->assertTrue($template->isNormalized()); $template->replaceTokens('//', function () { }); $this->assertFalse($template->isNormalized()); }
/** * Create a BBCode and its underlying tag and template(s) based on its reference usage * * @param string $usage BBCode usage, e.g. [B]{TEXT}[/b] * @param string|Template $template BBCode's template * @return array An array containing three elements: 'bbcode', 'bbcodeName' * and 'tag' */ public function create($usage, $template) { // Parse the BBCode usage $config = $this->parse($usage); // Create a template object for manipulation if (!$template instanceof Template) { $template = new Template($template); } // Replace the passthrough token in the BBCode's template $template->replaceTokens('#\\{(?:[A-Z]+[A-Z_0-9]*|@[-\\w]+)\\}#', function ($m) use($config) { $tokenId = substr($m[0], 1, -1); // Acknowledge {@foo} as an XPath expression even outside of attribute value // templates if ($tokenId[0] === '@') { return ['expression', $tokenId]; } // Test whether this is a known token if (isset($config['tokens'][$tokenId])) { // Replace with the corresponding attribute return ['expression', '@' . $config['tokens'][$tokenId]]; } // Test whether the token is used as passthrough if ($tokenId === $config['passthroughToken']) { return ['passthrough']; } // Undefined token. If it's the name of a filter, consider it's an error if ($this->isFilter($tokenId)) { throw new RuntimeException('Token {' . $tokenId . '} is ambiguous or undefined'); } // Use the token's name as parameter name return ['expression', '$' . $tokenId]; }); // Prepare the return array $return = ['bbcode' => $config['bbcode'], 'bbcodeName' => $config['bbcodeName'], 'tag' => $config['tag']]; // Set the template for this BBCode's tag $return['tag']->template = $template; return $return; }