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 |
SingleByteStringManipulation.php
001 <?php
002
003 /**
004 * @package s9e\TextFormatter
005 * @copyright Copyright (c) 2010-2022 The s9e authors
006 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
007 */
008 namespace s9e\TextFormatter\Configurator\RendererGenerators\PHP\XPathConvertor\Convertors;
009
010 class SingleByteStringManipulation extends AbstractConvertor
011 {
012 /**
013 * {@inheritdoc}
014 */
015 public function getMatchers(): array
016 {
017 return [
018 'String:Concat' => 'concat \\( ((?&String)) ((?:, (?&String) )+)?\\)',
019 'String:NormalizeSpace' => 'normalize-space \\( ((?&String)) \\)',
020 'String:SubstringAfter' => 'substring-after \\( ((?&String)) , ((?&LiteralString)) \\)',
021 'String:SubstringBefore' => 'substring-before \\( ((?&String)) , ((?&String)) \\)',
022 'String:Translate' => 'translate \\( ((?&String)) , ((?&LiteralString)) , ((?&LiteralString)) \\)'
023 ];
024 }
025
026 /**
027 * Convert a call to concat()
028 *
029 * @param string $expr1 First argument
030 * @param string $expr2 All other comma-separated arguments, starting with a comma
031 * @return string
032 */
033 public function parseConcat($expr1, $expr2 = null)
034 {
035 $php = $this->recurse($expr1);
036 if (isset($expr2))
037 {
038 $php .= '.' . $this->recurse('concat(' . ltrim($expr2, ',') . ')');
039 }
040
041 return $php;
042 }
043
044 /**
045 * Convert a call to normalize-space()
046 *
047 * @param string $expr
048 * @return string
049 */
050 public function parseNormalizeSpace($expr)
051 {
052 return "preg_replace('(\\\\s+)',' ',trim(" . $this->recurse($expr) . '))';
053 }
054
055 /**
056 * Convert a call to substring-after() where the second argument is a literal string
057 *
058 * @param string $expr
059 * @param string $str
060 * @return string
061 */
062 public function parseSubstringAfter($expr, $str)
063 {
064 return 'substr(strstr(' . $this->recurse($expr) . ',' . $this->recurse($str) . '),' . (strlen($str) - 2) . ')';
065 }
066
067 /**
068 * Convert a call to substring-before()
069 *
070 * @param string $expr1
071 * @param string $expr2
072 * @return string
073 */
074 public function parseSubstringBefore($expr1, $expr2)
075 {
076 return 'strstr(' . $this->recurse($expr1) . ',' . $this->recurse($expr2) . ',true)';
077 }
078
079 /**
080 * Convert a call to translate() where the second and third arguments are literal strings
081 *
082 * @param string $expr
083 * @param string $from
084 * @param string $to
085 * @return string
086 */
087 public function parseTranslate($expr, $from, $to)
088 {
089 $from = $this->splitStringChars($from);
090 $to = $this->splitStringChars($to);
091
092 // Add missing elements to $to then remove duplicates from $from and keep matching elements
093 $to = array_pad($to, count($from), '');
094 $from = array_unique($from);
095 $to = array_intersect_key($to, $from);
096
097 // Build the arguments list for the strtr() call
098 $args = [$this->recurse($expr)];
099 if ($this->isAsciiChars($from) && $this->isAsciiChars($to))
100 {
101 $args[] = $this->serializeAsciiChars($from);
102 $args[] = $this->serializeAsciiChars($to);
103 }
104 else
105 {
106 $args[] = $this->serializeMap($from, $to);
107 }
108
109 return 'strtr(' . implode(',', $args) . ')';
110 }
111
112 /**
113 * Test whether given list of strings contains only single ASCII characters
114 *
115 * @param string[] $chars
116 * @return bool
117 */
118 protected function isAsciiChars(array $chars)
119 {
120 return ([1] === array_unique(array_map('strlen', $chars)));
121 }
122
123 /**
124 * Serialize a list of ASCII chars into a single PHP string
125 *
126 * @param string[] $chars
127 * @return string
128 */
129 protected function serializeAsciiChars(array $chars)
130 {
131 return var_export(implode('', $chars), true);
132 }
133
134 /**
135 * Serialize the lists of characters to replace with strtr()
136 *
137 * @param string[] $from
138 * @param string[] $to
139 * @return string
140 */
141 protected function serializeMap(array $from, array $to)
142 {
143 $elements = [];
144 foreach ($from as $k => $str)
145 {
146 $elements[] = var_export($str, true) . '=>' . var_export($to[$k], true);
147 }
148
149 return '[' . implode(',', $elements) . ']';
150 }
151
152 /**
153 * Split individual characters from given literal string
154 *
155 * @param string $string Original string, including quotes
156 * @return string[]
157 */
158 protected function splitStringChars($string)
159 {
160 preg_match_all('(.)su', substr($string, 1, -1), $matches);
161
162 return $matches[0];
163 }
164 }