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. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
AbstractPass.php
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\Passes;
009
010 use const false, true;
011 use function array_shift, array_unshift, count, end, is_array;
012
013 abstract class AbstractPass implements PassInterface
014 {
015 /**
016 * @var bool Whether the current set of strings is optional
017 */
018 protected $isOptional;
019
020 /**
021 * {@inheritdoc}
022 */
023 public function run(array $strings): array
024 {
025 $strings = $this->beforeRun($strings);
026 if ($this->canRun($strings))
027 {
028 $strings = $this->runPass($strings);
029 }
030 $strings = $this->afterRun($strings);
031
032 return $strings;
033 }
034
035 /**
036 * Process the list of strings after the pass is run
037 *
038 * @param array[] $strings
039 * @return array[]
040 */
041 protected function afterRun(array $strings): array
042 {
043 if ($this->isOptional && $strings[0] !== [])
044 {
045 array_unshift($strings, []);
046 }
047
048 return $strings;
049 }
050
051 /**
052 * Prepare the list of strings before the pass is run
053 *
054 * @param array[] $strings
055 * @return array[]
056 */
057 protected function beforeRun(array $strings): array
058 {
059 $this->isOptional = (isset($strings[0]) && $strings[0] === []);
060 if ($this->isOptional)
061 {
062 array_shift($strings);
063 }
064
065 return $strings;
066 }
067
068 /**
069 * Test whether this pass can be run on a given list of strings
070 *
071 * @param array[] $strings
072 * @return bool
073 */
074 protected function canRun(array $strings): bool
075 {
076 return true;
077 }
078
079 /**
080 * Run this pass on a list of strings
081 *
082 * @param array[] $strings
083 * @return array[]
084 */
085 abstract protected function runPass(array $strings): array;
086
087 /**
088 * Test whether given string has an optional suffix
089 *
090 * @param array $string
091 * @return bool
092 */
093 protected function hasOptionalSuffix(array $string): bool
094 {
095 $suffix = end($string);
096
097 return (is_array($suffix) && $suffix[0] === []);
098 }
099
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 }