[ Index ]

PHP Cross Reference of phpBB-3.3.7-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Configurator/Items/AttributeFilters/ -> HashmapFilter.php (source)

   1  <?php
   2  
   3  /**
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2021 The s9e authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Configurator\Items\AttributeFilters;
   9  
  10  use InvalidArgumentException;
  11  use RuntimeException;
  12  use s9e\TextFormatter\Configurator\JavaScript\Dictionary;
  13  
  14  class HashmapFilter extends AbstractMapFilter
  15  {
  16      /**
  17      * Constructor
  18      *
  19      * @param  array $map    Associative array in the form [key => value]
  20      * @param  bool  $strict Whether this map is strict (values with no match are invalid)
  21      */
  22  	public function __construct(array $map = null, $strict = false)
  23      {
  24          parent::__construct('s9e\\TextFormatter\\Parser\\AttributeFilters\\HashmapFilter::filter');
  25  
  26          $this->resetParameters();
  27          $this->addParameterByName('attrValue');
  28          $this->addParameterByName('map');
  29          $this->addParameterByName('strict');
  30          $this->setJS('HashmapFilter.filter');
  31  
  32          if (isset($map))
  33          {
  34              $this->setMap($map, $strict);
  35          }
  36      }
  37  
  38      /**
  39      * Set the content of this map
  40      *
  41      * @param  array $map    Associative array in the form [word => replacement]
  42      * @param  bool  $strict Whether this map is strict (values with no match are invalid)
  43      * @return void
  44      */
  45  	public function setMap(array $map, $strict = false)
  46      {
  47          if (!is_bool($strict))
  48          {
  49              throw new InvalidArgumentException('Argument 2 passed to ' . __METHOD__ . ' must be a boolean');
  50          }
  51  
  52          // If the map is not strict, we can optimize away the values that are identical to their key
  53          if (!$strict)
  54          {
  55              $map = $this->optimizeLooseMap($map);
  56          }
  57  
  58          // Sort the map so it looks tidy
  59          ksort($map);
  60  
  61          // Record this filter's variables
  62          $this->vars['map']    = new Dictionary($map);
  63          $this->vars['strict'] = $strict;
  64  
  65          // Evaluate safeness
  66          $this->resetSafeness();
  67          if (!empty($this->vars['strict']))
  68          {
  69              $this->assessSafeness($map);
  70          }
  71      }
  72  
  73      /**
  74      * Optimize a non-strict map by removing values that are identical to their key
  75      *
  76      * @param  array $map Original map
  77      * @return array      Optimized map
  78      */
  79  	protected function optimizeLooseMap(array $map)
  80      {
  81          foreach ($map as $k => $v)
  82          {
  83              if ($k === $v)
  84              {
  85                  unset($map[$k]);
  86              }
  87          }
  88  
  89          return $map;
  90      }
  91  }


Generated: Thu Mar 24 21:31:15 2022 Cross-referenced by PHPXref 0.7.1