Verzeichnisstruktur phpBB-3.3.15


Veröffentlicht
28.08.2024

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

Builder.php

Zuletzt modifiziert: 02.04.2025, 15:03 - Dateigröße: 4.01 KiB


001  <?php declare(strict_types=1);
002   
003  /**
004  * @package   s9e\RegexpBuilder
005  * @copyright Copyright (c) 2016-2022 The s9e authors
006  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
007  */
008  namespace s9e\RegexpBuilder;
009   
010  use const SORT_STRING;
011  use function array_map, array_unique, sort;
012  use s9e\RegexpBuilder\Input\InputInterface;
013  use s9e\RegexpBuilder\Output\OutputInterface;
014  use s9e\RegexpBuilder\Passes\CoalesceOptionalStrings;
015  use s9e\RegexpBuilder\Passes\CoalesceSingleCharacterPrefix;
016  use s9e\RegexpBuilder\Passes\GroupSingleCharacters;
017  use s9e\RegexpBuilder\Passes\MergePrefix;
018  use s9e\RegexpBuilder\Passes\MergeSuffix;
019  use s9e\RegexpBuilder\Passes\PromoteSingleStrings;
020  use s9e\RegexpBuilder\Passes\Recurse;
021   
022  class Builder
023  {
024      /**
025      * @var InputInterface
026      */
027      protected $input;
028   
029      /**
030      * @var MetaCharacters
031      */
032      protected $meta;
033   
034      /**
035      * @var Runner
036      */
037      protected $runner;
038   
039      /**
040      * @var Serializer
041      */
042      protected $serializer;
043   
044      /**
045      * @param array $config
046      */
047      public function __construct(array $config = [])
048      {
049          $config += [
050              'delimiter'     => '/',
051              'input'         => 'Bytes',
052              'inputOptions'  => [],
053              'meta'          => [],
054              'output'        => 'Bytes',
055              'outputOptions' => []
056          ];
057   
058          $this->setInput($config['input'], $config['inputOptions']);
059          $this->setMeta($config['meta']);
060          $this->setSerializer($config['output'], $config['outputOptions'], $config['delimiter']);
061          $this->setRunner();
062      }
063   
064      /**
065      * Build and return a regular expression that matches all of the given strings
066      *
067      * @param  string[] $strings Literal strings to be matched
068      * @return string            Regular expression (without delimiters)
069      */
070      public function build(array $strings): string
071      {
072          $strings = array_unique($strings);
073          sort($strings, SORT_STRING);
074          if ($this->isEmpty($strings))
075          {
076              return '';
077          }
078   
079          $strings = $this->splitStrings($strings);
080          $strings = $this->meta->replaceMeta($strings);
081          $strings = $this->runner->run($strings);
082   
083          return $this->serializer->serializeStrings($strings);
084      }
085   
086      /**
087      * Test whether the list of strings is empty
088      *
089      * @param  string[] $strings
090      * @return bool
091      */
092      protected function isEmpty(array $strings): bool
093      {
094          return (empty($strings) || $strings === ['']);
095      }
096   
097      /**
098      * Set the InputInterface instance in $this->input
099      *
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  }