[ Index ]

PHP Cross Reference of phpBB-3.3.2-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Parser/ -> FilterProcessing.php (source)

   1  <?php
   2  
   3  /**
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2020 The s9e authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Parser;
   9  
  10  use s9e\TextFormatter\Parser;
  11  
  12  class FilterProcessing
  13  {
  14      /**
  15      * Execute all the attribute preprocessors of given tag
  16      *
  17      * @private
  18      *
  19      * @param  Tag   $tag       Source tag
  20      * @param  array $tagConfig Tag's config
  21      * @return void
  22      */
  23  	public static function executeAttributePreprocessors(Tag $tag, array $tagConfig)
  24      {
  25          if (empty($tagConfig['attributePreprocessors']))
  26          {
  27              return;
  28          }
  29  
  30          foreach ($tagConfig['attributePreprocessors'] as list($attrName, $regexp, $map))
  31          {
  32              if ($tag->hasAttribute($attrName))
  33              {
  34                  self::executeAttributePreprocessor($tag, $attrName, $regexp, $map);
  35              }
  36          }
  37      }
  38  
  39      /**
  40      * Filter the attributes of given tag
  41      *
  42      * @private
  43      *
  44      * @param  Tag    $tag            Tag being checked
  45      * @param  array  $tagConfig      Tag's config
  46      * @param  array  $registeredVars Array of registered vars for use in attribute filters
  47      * @param  Logger $logger         This parser's Logger instance
  48      * @return void
  49      */
  50  	public static function filterAttributes(Tag $tag, array $tagConfig, array $registeredVars, Logger $logger)
  51      {
  52          $attributes = [];
  53          foreach ($tagConfig['attributes'] as $attrName => $attrConfig)
  54          {
  55              $attrValue = false;
  56              if ($tag->hasAttribute($attrName))
  57              {
  58                  $vars = [
  59                      'attrName'       => $attrName,
  60                      'attrValue'      => $tag->getAttribute($attrName),
  61                      'logger'         => $logger,
  62                      'registeredVars' => $registeredVars
  63                  ];
  64                  $attrValue = self::executeAttributeFilterChain($attrConfig['filterChain'], $vars);
  65              }
  66  
  67              if ($attrValue !== false)
  68              {
  69                  $attributes[$attrName] = $attrValue;
  70              }
  71              elseif (isset($attrConfig['defaultValue']))
  72              {
  73                  $attributes[$attrName] = $attrConfig['defaultValue'];
  74              }
  75              elseif (!empty($attrConfig['required']))
  76              {
  77                  $tag->invalidate();
  78              }
  79          }
  80          $tag->setAttributes($attributes);
  81      }
  82  
  83      /**
  84      * Execute a tag's filterChain
  85      *
  86      * @private
  87      *
  88      * @param  Tag    $tag        Tag to filter
  89      * @param  Parser $parser     Parser
  90      * @param  array  $tagsConfig Tags' config
  91      * @param  Tag[]  $openTags   List of open tags
  92      * @return void
  93      */
  94  	public static function filterTag(Tag $tag, Parser $parser, array $tagsConfig, array $openTags)
  95      {
  96          $tagName   = $tag->getName();
  97          $tagConfig = $tagsConfig[$tagName];
  98  
  99          // Record the tag being processed into the logger it can be added to the context of
 100          // messages logged during the execution
 101          $logger = $parser->getLogger();
 102          $logger->setTag($tag);
 103  
 104          // Prepare the variables that are accessible to filters
 105          $text = $parser->getText();
 106          $vars = [
 107              'innerText'      => '',
 108              'logger'         => $logger,
 109              'openTags'       => $openTags,
 110              'outerText'      => substr($text, $tag->getPos(), $tag->getLen()),
 111              'parser'         => $parser,
 112              'registeredVars' => $parser->registeredVars,
 113              'tag'            => $tag,
 114              'tagConfig'      => $tagConfig,
 115              'tagText'        => substr($text, $tag->getPos(), $tag->getLen()),
 116              'text'           => $text
 117          ];
 118          $endTag = $tag->getEndTag();
 119          if ($endTag)
 120          {
 121              $vars['innerText'] = substr($text, $tag->getPos() + $tag->getLen(), $endTag->getPos() - $tag->getPos() - $tag->getLen());
 122              $vars['outerText'] = substr($text, $tag->getPos(), $endTag->getPos() + $endTag->getLen() - $tag->getPos());
 123          }
 124          foreach ($tagConfig['filterChain'] as $filter)
 125          {
 126              if ($tag->isInvalid())
 127              {
 128                  break;
 129              }
 130              self::executeFilter($filter, $vars);
 131          }
 132  
 133          // Remove the tag from the logger
 134          $logger->unsetTag();
 135      }
 136  
 137      /**
 138      * Execute an attribute's filterChain
 139      *
 140      * @param  array $filterChain Attribute's filterChain
 141      * @param  array $vars        Callback vars
 142      * @return mixed              Filtered value
 143      */
 144  	protected static function executeAttributeFilterChain(array $filterChain, array $vars)
 145      {
 146          $vars['logger']->setAttribute($vars['attrName']);
 147          foreach ($filterChain as $filter)
 148          {
 149              $vars['attrValue'] = self::executeFilter($filter, $vars);
 150              if ($vars['attrValue'] === false)
 151              {
 152                  break;
 153              }
 154          }
 155          $vars['logger']->unsetAttribute();
 156  
 157          return $vars['attrValue'];
 158      }
 159  
 160      /**
 161      * Execute an attribute preprocessor
 162      *
 163      * @param  Tag      $tag
 164      * @param  string   $attrName
 165      * @param  string   $regexp
 166      * @param  string[] $map
 167      * @return void
 168      */
 169  	protected static function executeAttributePreprocessor(Tag $tag, $attrName, $regexp, $map)
 170      {
 171          $attrValue = $tag->getAttribute($attrName);
 172          $captures  = self::getNamedCaptures($attrValue, $regexp, $map);
 173          foreach ($captures as $k => $v)
 174          {
 175              // Attribute preprocessors cannot overwrite other attributes but they can
 176              // overwrite themselves
 177              if ($k === $attrName || !$tag->hasAttribute($k))
 178              {
 179                  $tag->setAttribute($k, $v);
 180              }
 181          }
 182      }
 183  
 184      /**
 185      * Execute a filter
 186      *
 187      * @see s9e\TextFormatter\Configurator\Items\ProgrammableCallback
 188      *
 189      * @param  array $filter Programmed callback
 190      * @param  array $vars   Variables to be used when executing the callback
 191      * @return mixed         Whatever the callback returns
 192      */
 193  	protected static function executeFilter(array $filter, array $vars)
 194      {
 195          // Add vars from the registeredVars array to the list of vars
 196          $vars += ['registeredVars' => []];
 197          $vars += $vars['registeredVars'];
 198  
 199          // Prepare the list of arguments
 200          $args = [];
 201          if (isset($filter['params']))
 202          {
 203              foreach ($filter['params'] as $k => $v)
 204              {
 205                  $args[] = (isset($vars[$k])) ? $vars[$k] : $v;
 206              }
 207          }
 208  
 209          return call_user_func_array($filter['callback'], $args);
 210      }
 211  
 212      /**
 213      * Execute a regexp and return the values of the mapped captures
 214      *
 215      * @param  string   $str
 216      * @param  string   $regexp
 217      * @param  string[] $map
 218      * @return array
 219      */
 220  	protected static function getNamedCaptures($str, $regexp, $map)
 221      {
 222          if (!preg_match($regexp, $str, $m))
 223          {
 224              return [];
 225          }
 226  
 227          $values = [];
 228          foreach ($map as $i => $k)
 229          {
 230              if (isset($m[$i]) && $m[$i] !== '')
 231              {
 232                  $values[$k] = $m[$i];
 233              }
 234          }
 235  
 236          return $values;
 237      }
 238  }


Generated: Wed Nov 11 20:28:18 2020 Cross-referenced by PHPXref 0.7.1