[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Configurator/Helpers/ -> ConfigHelper.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 RuntimeException;
  11  use s9e\TextFormatter\Configurator\ConfigProvider;
  12  use s9e\TextFormatter\Configurator\FilterableConfigValue;
  13  use s9e\TextFormatter\Configurator\JavaScript\Dictionary;
  14  use Traversable;
  15  
  16  abstract class ConfigHelper
  17  {
  18      /**
  19      * Recursively filter a config array to replace variants with the desired value
  20      *
  21      * @param  array  $config Config array
  22      * @param  string $target Target parser
  23      * @return array          Filtered config
  24      */
  25  	public static function filterConfig(array $config, $target = 'PHP')
  26      {
  27          $filteredConfig = [];
  28          foreach ($config as $name => $value)
  29          {
  30              if ($value instanceof FilterableConfigValue)
  31              {
  32                  $value = $value->filterConfig($target);
  33                  if (!isset($value))
  34                  {
  35                      continue;
  36                  }
  37              }
  38              if (is_array($value))
  39              {
  40                  $value = self::filterConfig($value, $target);
  41              }
  42              $filteredConfig[$name] = $value;
  43          }
  44  
  45          return $filteredConfig;
  46      }
  47  
  48      /**
  49      * Generate a quickMatch string from a list of strings
  50      *
  51      * This is basically a LCS implementation, tuned for small strings and fast failure
  52      *
  53      * @param  array $strings Array of strings
  54      * @return mixed          quickMatch string, or FALSE if none could be generated
  55      */
  56  	public static function generateQuickMatchFromList(array $strings)
  57      {
  58          foreach ($strings as $string)
  59          {
  60              $stringLen  = strlen($string);
  61              $substrings = [];
  62  
  63              for ($len = $stringLen; $len; --$len)
  64              {
  65                  $pos = $stringLen - $len;
  66  
  67                  do
  68                  {
  69                      $substrings[substr($string, $pos, $len)] = 1;
  70                  }
  71                  while (--$pos >= 0);
  72              }
  73  
  74              if (isset($goodStrings))
  75              {
  76                  $goodStrings = array_intersect_key($goodStrings, $substrings);
  77  
  78                  if (empty($goodStrings))
  79                  {
  80                      break;
  81                  }
  82              }
  83              else
  84              {
  85                  $goodStrings = $substrings;
  86              }
  87          }
  88  
  89          if (empty($goodStrings))
  90          {
  91              return false;
  92          }
  93  
  94          // The strings are stored by length descending, so we return the first in the list
  95          return strval(key($goodStrings));
  96      }
  97  
  98      /**
  99      * Optimize the size of a deep array by deduplicating identical structures
 100      *
 101      * This method is meant to be used on a config array which is only read and never modified
 102      *
 103      * @param  array &$config
 104      * @param  array &$cache
 105      * @return array
 106      */
 107  	public static function optimizeArray(array &$config, array &$cache = [])
 108      {
 109          foreach ($config as $k => &$v)
 110          {
 111              if (!is_array($v))
 112              {
 113                  continue;
 114              }
 115  
 116              // Dig deeper into this array
 117              self::optimizeArray($v, $cache);
 118  
 119              // Look for a matching structure
 120              $cacheKey = serialize($v);
 121              if (!isset($cache[$cacheKey]))
 122              {
 123                  // Record this value in the cache
 124                  $cache[$cacheKey] = $v;
 125              }
 126  
 127              // Replace the entry in $config with a reference to the cached value
 128              $config[$k] =& $cache[$cacheKey];
 129          }
 130          unset($v);
 131      }
 132  
 133      /**
 134      * Convert a structure to a (possibly multidimensional) array
 135      *
 136      * @param  mixed $value
 137      * @param  bool  $keepEmpty Whether to keep empty arrays instead of removing them
 138      * @param  bool  $keepNull  Whether to keep NULL values instead of removing them
 139      * @return array
 140      */
 141  	public static function toArray($value, $keepEmpty = false, $keepNull = false)
 142      {
 143          $array = [];
 144  
 145          foreach ($value as $k => $v)
 146          {
 147              $isDictionary = $v instanceof Dictionary;
 148              if ($v instanceof ConfigProvider)
 149              {
 150                  $v = $v->asConfig();
 151              }
 152              elseif ($v instanceof Traversable || is_array($v))
 153              {
 154                  $v = self::toArray($v, $keepEmpty, $keepNull);
 155              }
 156              elseif (is_scalar($v) || is_null($v))
 157              {
 158                  // Do nothing
 159              }
 160              else
 161              {
 162                  $type = (is_object($v))
 163                        ? 'an instance of ' . get_class($v)
 164                        : 'a ' . gettype($v);
 165  
 166                  throw new RuntimeException('Cannot convert ' . $type . ' to array');
 167              }
 168  
 169              if (!isset($v) && !$keepNull)
 170              {
 171                  // We don't record NULL values
 172                  continue;
 173              }
 174  
 175              if (!$keepEmpty && $v === [])
 176              {
 177                  // We don't record empty structures
 178                  continue;
 179              }
 180  
 181              $array[$k] = ($isDictionary) ? new Dictionary($v) : $v;
 182          }
 183  
 184          return $array;
 185      }
 186  }


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