[ Index ]

PHP Cross Reference of phpBB-3.3.3-deutsch

title

Body

[close]

/vendor/s9e/regexp-builder/src/Passes/ -> CoalesceSingleCharacterPrefix.php (source)

   1  <?php
   2  
   3  /**
   4  * @package   s9e\RegexpBuilder
   5  * @copyright Copyright (c) 2016-2020 The s9e authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\RegexpBuilder\Passes;
   9  
  10  /**
  11  * Replaces (?:ab|bb|c) with (?:[ab]b|c)
  12  */
  13  class CoalesceSingleCharacterPrefix extends AbstractPass
  14  {
  15      /**
  16      * {@inheritdoc}
  17      */
  18  	protected function runPass(array $strings)
  19      {
  20          $newStrings = [];
  21          foreach ($this->getEligibleKeys($strings) as $keys)
  22          {
  23              // Create a new string to hold the merged strings and replace the first element with
  24              // an empty character class
  25              $newString    = $strings[$keys[0]];
  26              $newString[0] = [];
  27  
  28              // Fill the character class with the prefix of each string in this group before removing
  29              // the original string
  30              foreach ($keys as $key)
  31              {
  32                  $newString[0][] = [$strings[$key][0]];
  33                  unset($strings[$key]);
  34              }
  35              $newStrings[] = $newString;
  36          }
  37  
  38          return array_merge($newStrings, $strings);
  39      }
  40  
  41      /**
  42      * Filter the list of eligible keys and keep those that have at least two matches
  43      *
  44      * @param  array[] $eligibleKeys List of lists of keys
  45      * @return array[]
  46      */
  47  	protected function filterEligibleKeys(array $eligibleKeys)
  48      {
  49          $filteredKeys = [];
  50          foreach ($eligibleKeys as $k => $keys)
  51          {
  52              if (count($keys) > 1)
  53              {
  54                  $filteredKeys[] = $keys;
  55              }
  56          }
  57  
  58          return $filteredKeys;
  59      }
  60  
  61      /**
  62      * Get a list of keys of strings eligible to be merged together, grouped by suffix
  63      *
  64      * @param  array[] $strings
  65      * @return array[]
  66      */
  67  	protected function getEligibleKeys(array $strings)
  68      {
  69          $eligibleKeys = [];
  70          foreach ($strings as $k => $string)
  71          {
  72              if (!is_array($string[0]) && isset($string[1]))
  73              {
  74                  $suffix = serialize(array_slice($string, 1));
  75                  $eligibleKeys[$suffix][] = $k;
  76              }
  77          }
  78  
  79          return $this->filterEligibleKeys($eligibleKeys);
  80      }
  81  }


Generated: Sun Feb 14 20:08:31 2021 Cross-referenced by PHPXref 0.7.1