示例#1
0
 /**
  * Convert a preg-style replacement to a template
  *
  * @param  string  $template    Original template
  * @param  integer $passthrough Index of the passthrough capture
  * @return string               Modified template
  */
 protected function convertTemplate($template, $passthrough)
 {
     // Replace numeric references in the template with the value of the corresponding attribute
     // values or passthrough
     $template = TemplateHelper::replaceTokens($template, $this->referencesRegexp, function ($m, $node) use($passthrough) {
         $key = (int) trim($m[0], '\\${}');
         if ($key === 0) {
             // $0 copies the whole textContent
             return ['expression', '.'];
         }
         if ($key === $passthrough && $node instanceof DOMText) {
             // Passthrough capture, does not include start/end tags
             return ['passthrough'];
         }
         if (isset($this->captures[$key]['name'])) {
             // Normal capture, replaced by the equivalent expression
             return ['expression', '@' . $this->captures[$key]['name']];
         }
         // Non-existent captures are simply ignored, similarly to preg_replace()
         return ['literal', ''];
     });
     // Unescape backslashes and special characters in the template
     $template = TemplateHelper::replaceTokens($template, '(\\\\+[0-9${\\\\])', function ($m) {
         return ['literal', stripslashes($m[0])];
     });
     return $template;
 }
示例#2
0
 protected function convertTemplate($template, $passthrough)
 {
     $_this = $this;
     $template = TemplateHelper::replaceTokens($template, $this->referencesRegexp, function ($m, $node) use($passthrough, $_this) {
         $key = (int) \trim($m[0], '\\${}');
         if ($key === 0) {
             return array('expression', '.');
         }
         if ($key === $passthrough && $node instanceof DOMText) {
             return array('passthrough');
         }
         if (isset($_this->captures[$key]['name'])) {
             return array('expression', '@' . $_this->captures[$key]['name']);
         }
         return array('literal', '');
     });
     $template = TemplateHelper::replaceTokens($template, '(\\\\+[0-9${\\\\])', function ($m) {
         return array('literal', \stripslashes($m[0]));
     });
     return $template;
 }
 /**
  * @testdox replaceTokens() tests
  * @dataProvider replaceTokensTests
  */
 public function testReplaceTokens($template, $regexp, $fn, $expected)
 {
     if ($expected instanceof Exception) {
         $this->setExpectedException(get_class($expected), $expected->getMessage());
     }
     $this->assertSame($expected, TemplateHelper::replaceTokens($template, $regexp, $fn, $expected));
 }
示例#4
0
 /**
  * Replace parts of this template that match given regexp
  *
  * @param  string   $regexp Regexp for matching parts that need replacement
  * @param  callback $fn     Callback used to get the replacement
  * @return void
  */
 public function replaceTokens($regexp, $fn)
 {
     $this->forensics = null;
     $this->template = TemplateHelper::replaceTokens($this->template, $regexp, $fn);
     $this->isNormalized = false;
 }