[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/s9e/regexp-builder/src/ -> Builder.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;
   9  
  10  use const SORT_STRING;
  11  use function array_map, array_unique, sort;
  12  use s9e\RegexpBuilder\Input\InputInterface;
  13  use s9e\RegexpBuilder\Output\OutputInterface;
  14  use s9e\RegexpBuilder\Passes\CoalesceOptionalStrings;
  15  use s9e\RegexpBuilder\Passes\CoalesceSingleCharacterPrefix;
  16  use s9e\RegexpBuilder\Passes\GroupSingleCharacters;
  17  use s9e\RegexpBuilder\Passes\MergePrefix;
  18  use s9e\RegexpBuilder\Passes\MergeSuffix;
  19  use s9e\RegexpBuilder\Passes\PromoteSingleStrings;
  20  use s9e\RegexpBuilder\Passes\Recurse;
  21  
  22  class Builder
  23  {
  24      /**
  25      * @var InputInterface
  26      */
  27      protected $input;
  28  
  29      /**
  30      * @var MetaCharacters
  31      */
  32      protected $meta;
  33  
  34      /**
  35      * @var Runner
  36      */
  37      protected $runner;
  38  
  39      /**
  40      * @var Serializer
  41      */
  42      protected $serializer;
  43  
  44      /**
  45      * @param array $config
  46      */
  47  	public function __construct(array $config = [])
  48      {
  49          $config += [
  50              'delimiter'     => '/',
  51              'input'         => 'Bytes',
  52              'inputOptions'  => [],
  53              'meta'          => [],
  54              'output'        => 'Bytes',
  55              'outputOptions' => []
  56          ];
  57  
  58          $this->setInput($config['input'], $config['inputOptions']);
  59          $this->setMeta($config['meta']);
  60          $this->setSerializer($config['output'], $config['outputOptions'], $config['delimiter']);
  61          $this->setRunner();
  62      }
  63  
  64      /**
  65      * Build and return a regular expression that matches all of the given strings
  66      *
  67      * @param  string[] $strings Literal strings to be matched
  68      * @return string            Regular expression (without delimiters)
  69      */
  70  	public function build(array $strings): string
  71      {
  72          $strings = array_unique($strings);
  73          sort($strings, SORT_STRING);
  74          if ($this->isEmpty($strings))
  75          {
  76              return '';
  77          }
  78  
  79          $strings = $this->splitStrings($strings);
  80          $strings = $this->meta->replaceMeta($strings);
  81          $strings = $this->runner->run($strings);
  82  
  83          return $this->serializer->serializeStrings($strings);
  84      }
  85  
  86      /**
  87      * Test whether the list of strings is empty
  88      *
  89      * @param  string[] $strings
  90      * @return bool
  91      */
  92  	protected function isEmpty(array $strings): bool
  93      {
  94          return (empty($strings) || $strings === ['']);
  95      }
  96  
  97      /**
  98      * Set the InputInterface instance in $this->input
  99      *
 100      * @param  string $inputType
 101      * @param  array  $inputOptions
 102      * @return void
 103      */
 104  	protected function setInput(string $inputType, array $inputOptions): void
 105      {
 106          $className   = __NAMESPACE__ . '\\Input\\' . $inputType;
 107          $this->input = new $className($inputOptions);
 108      }
 109  
 110      /**
 111      * Set the MetaCharacters instance in $this->meta
 112      *
 113      * @param  array $map
 114      * @return void
 115      */
 116  	protected function setMeta(array $map): void
 117      {
 118          $this->meta = new MetaCharacters($this->input);
 119          foreach ($map as $char => $expr)
 120          {
 121              $this->meta->add($char, $expr);
 122          }
 123      }
 124  
 125      /**
 126      * Set the Runner instance $in this->runner
 127      *
 128      * @return void
 129      */
 130  	protected function setRunner(): void
 131      {
 132          $this->runner = new Runner;
 133          $this->runner->addPass(new MergePrefix);
 134          $this->runner->addPass(new GroupSingleCharacters);
 135          $this->runner->addPass(new Recurse($this->runner));
 136          $this->runner->addPass(new PromoteSingleStrings);
 137          $this->runner->addPass(new CoalesceOptionalStrings);
 138          $this->runner->addPass(new MergeSuffix);
 139          $this->runner->addPass(new CoalesceSingleCharacterPrefix);
 140      }
 141  
 142      /**
 143      * Set the Serializer instance in $this->serializer
 144      *
 145      * @param  string $outputType
 146      * @param  array  $outputOptions
 147      * @param  string $delimiter
 148      * @return void
 149      */
 150  	protected function setSerializer(string $outputType, array $outputOptions, string $delimiter): void
 151      {
 152          $className = __NAMESPACE__ . '\\Output\\' . $outputType;
 153          $output    = new $className($outputOptions);
 154          $escaper   = new Escaper($delimiter);
 155  
 156          $this->serializer = new Serializer($output, $this->meta, $escaper);
 157      }
 158  
 159      /**
 160      * Split all given strings by character
 161      *
 162      * @param  string[] $strings List of strings
 163      * @return array[]           List of arrays
 164      */
 165  	protected function splitStrings(array $strings): array
 166      {
 167          return array_map([$this->input, 'split'], $strings);
 168      }
 169  }


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