[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Configurator/Helpers/ -> AVTHelper.php (source)

   1  <?php
   2  
   3  /**
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2022 The s9e authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Configurator\Helpers;
   9  
  10  use DOMAttr;
  11  use RuntimeException;
  12  
  13  abstract class AVTHelper
  14  {
  15      /**
  16      * Parse an attribute value template
  17      *
  18      * @link https://www.w3.org/TR/1999/REC-xslt-19991116#dt-attribute-value-template
  19      *
  20      * @param  string $attrValue Attribute value
  21      * @return array             Array of tokens
  22      */
  23  	public static function parse($attrValue)
  24      {
  25          preg_match_all('((*MARK:literal)(?:[^{]|\\{\\{)++|(*MARK:expression)\\{(?:[^}"\']|"[^"]*+"|\'[^\']*+\')++\\}|(*MARK:junk).++)s', $attrValue, $matches);
  26  
  27          $tokens  = [];
  28          foreach ($matches[0] as $i => $str)
  29          {
  30              if ($matches['MARK'][$i] === 'expression')
  31              {
  32                  $tokens[] = ['expression', substr($str, 1, -1)];
  33              }
  34              else
  35              {
  36                  $tokens[] = ['literal', strtr($str, ['{{' => '{', '}}' => '}'])];
  37              }
  38          }
  39  
  40          return $tokens;
  41      }
  42  
  43      /**
  44      * Replace the value of an attribute via the provided callback
  45      *
  46      * The callback will receive an array containing the type and value of each token in the AVT.
  47      * Its return value should use the same format
  48      *
  49      * @param  DOMAttr  $attribute
  50      * @param  callable $callback
  51      * @return void
  52      */
  53  	public static function replace(DOMAttr $attribute, callable $callback)
  54      {
  55          $tokens = self::parse($attribute->value);
  56          foreach ($tokens as $k => $token)
  57          {
  58              $tokens[$k] = $callback($token);
  59          }
  60  
  61          $attribute->value = htmlspecialchars(self::serialize($tokens), ENT_NOQUOTES, 'UTF-8');
  62      }
  63  
  64      /**
  65      * Serialize an array of AVT tokens back into an attribute value
  66      *
  67      * @param  array  $tokens
  68      * @return string
  69      */
  70  	public static function serialize(array $tokens)
  71      {
  72          $attrValue = '';
  73          foreach ($tokens as $token)
  74          {
  75              if ($token[0] === 'literal')
  76              {
  77                  $attrValue .= preg_replace('([{}])', '$0$0', $token[1]);
  78              }
  79              elseif ($token[0] === 'expression')
  80              {
  81                  $attrValue .= '{' . $token[1] . '}';
  82              }
  83              else
  84              {
  85                  throw new RuntimeException('Unknown token type');
  86              }
  87          }
  88  
  89          return $attrValue;
  90      }
  91  
  92      /**
  93      * Transform given attribute value template into an XSL fragment
  94      *
  95      * @param  string $attrValue
  96      * @return string
  97      */
  98  	public static function toXSL($attrValue)
  99      {
 100          $xsl = '';
 101          foreach (self::parse($attrValue) as list($type, $content))
 102          {
 103              if ($type === 'expression')
 104              {
 105                  $xsl .= '<xsl:value-of select="' . htmlspecialchars($content, ENT_COMPAT, 'UTF-8') . '"/>';
 106              }
 107              elseif (trim($content) !== $content)
 108              {
 109                  $xsl .= '<xsl:text>' . htmlspecialchars($content, ENT_NOQUOTES, 'UTF-8') . '</xsl:text>';
 110              }
 111              else
 112              {
 113                  $xsl .= htmlspecialchars($content, ENT_NOQUOTES, 'UTF-8');
 114              }
 115          }
 116  
 117          return $xsl;
 118      }
 119  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1