[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

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

   1  <?php declare(strict_types=1);
   2  
   3  /**
   4  * @package   s9e\RegexpBuilder
   5  * @copyright Copyright (c) 2016-2022 The s9e authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\RegexpBuilder\Passes;
   9  
  10  use const false, true;
  11  use function array_shift, array_unshift, count, end, is_array;
  12  
  13  abstract class AbstractPass implements PassInterface
  14  {
  15      /**
  16      * @var bool Whether the current set of strings is optional
  17      */
  18      protected $isOptional;
  19  
  20      /**
  21      * {@inheritdoc}
  22      */
  23  	public function run(array $strings): array
  24      {
  25          $strings = $this->beforeRun($strings);
  26          if ($this->canRun($strings))
  27          {
  28              $strings = $this->runPass($strings);
  29          }
  30          $strings = $this->afterRun($strings);
  31  
  32          return $strings;
  33      }
  34  
  35      /**
  36      * Process the list of strings after the pass is run
  37      *
  38      * @param  array[] $strings
  39      * @return array[]
  40      */
  41  	protected function afterRun(array $strings): array
  42      {
  43          if ($this->isOptional && $strings[0] !== [])
  44          {
  45              array_unshift($strings, []);
  46          }
  47  
  48          return $strings;
  49      }
  50  
  51      /**
  52      * Prepare the list of strings before the pass is run
  53      *
  54      * @param  array[] $strings
  55      * @return array[]
  56      */
  57  	protected function beforeRun(array $strings): array
  58      {
  59          $this->isOptional = (isset($strings[0]) && $strings[0] === []);
  60          if ($this->isOptional)
  61          {
  62              array_shift($strings);
  63          }
  64  
  65          return $strings;
  66      }
  67  
  68      /**
  69      * Test whether this pass can be run on a given list of strings
  70      *
  71      * @param  array[] $strings
  72      * @return bool
  73      */
  74  	protected function canRun(array $strings): bool
  75      {
  76          return true;
  77      }
  78  
  79      /**
  80      * Run this pass on a list of strings
  81      *
  82      * @param  array[] $strings
  83      * @return array[]
  84      */
  85      abstract protected function runPass(array $strings): array;
  86  
  87      /**
  88      * Test whether given string has an optional suffix
  89      *
  90      * @param  array $string
  91      * @return bool
  92      */
  93  	protected function hasOptionalSuffix(array $string): bool
  94      {
  95          $suffix = end($string);
  96  
  97          return (is_array($suffix) && $suffix[0] === []);
  98      }
  99  
 100      /**
 101      * Test whether given string contains a single alternation made of single values
 102      *
 103      * @param  array $string
 104      * @return bool
 105      */
 106  	protected function isCharacterClassString(array $string): bool
 107      {
 108          return ($this->isSingleAlternationString($string) && $this->isSingleCharStringList($string[0]));
 109      }
 110  
 111      /**
 112      * Test whether given string contains one single element that is an alternation
 113      *
 114      * @param  array $string
 115      * @return bool
 116      */
 117  	protected function isSingleAlternationString(array $string): bool
 118      {
 119          return (count($string) === 1 && is_array($string[0]));
 120      }
 121  
 122      /**
 123      * Test whether given string contains a single character value
 124      *
 125      * @param  array $string
 126      * @return bool
 127      */
 128  	protected function isSingleCharString(array $string): bool
 129      {
 130          return (count($string) === 1 && !is_array($string[0]));
 131      }
 132  
 133      /**
 134      * Test whether given list of strings contains nothing but single-char strings
 135      *
 136      * @param  array[] $strings
 137      * @return bool
 138      */
 139  	protected function isSingleCharStringList(array $strings): bool
 140      {
 141          foreach ($strings as $string)
 142          {
 143              if (!$this->isSingleCharString($string))
 144              {
 145                  return false;
 146              }
 147          }
 148  
 149          return true;
 150      }
 151  }


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