Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

PcreCompiler.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\Persistence\TransformationProcessor;
8
9 use eZ\Publish\Core\Persistence\Utf8Converter;
10 use eZ\Publish\Core\Persistence\TransformationProcessor;
11 use RuntimeException;
12
17 class PcreCompiler
18 {
24 protected $converter;
25
31 public function __construct(Utf8Converter $converter)
32 {
33 $this->converter = $converter;
34 }
35
47 public function compile(array $ast)
48 {
49 $transformations = [];
50
51 foreach ($ast as $section => $rules) {
52 foreach ($rules as $rule) {
53 $transformations[$section][] = $this->compileRule($rule);
54 }
55 }
56
57 return $transformations;
58 }
59
67 protected function compileRule(array $rule)
68 {
69 switch ($rule['type']) {
70 case TransformationProcessor::T_MAP:
71 return $this->compileMap($rule);
72
73 case TransformationProcessor::T_REPLACE:
74 return $this->compileReplace($rule);
75
76 case TransformationProcessor::T_TRANSPOSE:
77 return $this->compileTranspose($rule);
78
79 case TransformationProcessor::T_TRANSPOSE_MODULO:
80 return $this->compileTransposeModulo($rule);
81
82 default:
83 throw new RuntimeException('Unknown rule type: ' . $rule['type']);
84 }
85 }
86
94 protected function compileMap(array $rule)
95 {
96 return [
97 'regexp' => '(' . preg_quote($this->compileCharacter($rule['data']['src'])) . ')us',
98 'callback' => $this->compileTargetCharacter($rule['data']['dest']),
99 ];
100 }
101
109 protected function compileReplace(array $rule)
110 {
111 return [
112 'regexp' => '([' .
113 preg_quote($this->compileCharacter($rule['data']['srcStart'])) . '-' .
114 preg_quote($this->compileCharacter($rule['data']['srcEnd'])) .
115 '])us',
116 'callback' => $this->compileTargetCharacter($rule['data']['dest']),
117 ];
118 }
119
127 protected function compileTranspose(array $rule)
128 {
129 return [
130 'regexp' => '([' .
131 preg_quote($this->compileCharacter($rule['data']['srcStart'])) . '-' .
132 preg_quote($this->compileCharacter($rule['data']['srcEnd'])) .
133 '])us',
134 'callback' => $this->getTransposeClosure($rule['data']['op'], $rule['data']['dest']),
135 ];
136 }
137
145 protected function compileTransposeModulo(array $rule)
146 {
147 return [
148 'regexp' => '([' .
149 preg_quote(
150 $this->getModuloCharRange(
151 $this->compileCharacter($rule['data']['srcStart']),
152 $this->compileCharacter($rule['data']['srcEnd']),
153 $rule['data']['modulo']
154 )
155 ) .
156 '])us',
157 'callback' => $this->getTransposeClosure($rule['data']['op'], $rule['data']['dest']),
158 ];
159 }
160
174 protected function getModuloCharRange($start, $end, $modulo)
175 {
176 $start = $this->converter->toUnicodeCodepoint($start);
177 $end = $this->converter->toUnicodeCodepoint($end);
178 $modulo = hexdec($modulo);
179
180 $chars = '';
181 for ($start; $start <= $end; $start += $modulo) {
182 $chars .= $this->converter->toUTF8Character($start);
183 }
184
185 return $chars;
186 }
187
197 protected function getTransposeClosure($operator, $value)
198 {
199 $value = $this->hexdec($value) * ($operator === '-' ? -1 : 1);
200 $converter = $this->converter;
201
202 return function ($matches) use ($value, $converter) {
203 return $converter->toUTF8Character(
204 $converter->toUnicodeCodepoint($matches[0]) + $value
205 );
206 };
207 }
208
217 protected function compileTargetCharacter($char)
218 {
219 switch (true) {
220 case $char === 'remove':
221 return function ($matches) {
222 return '';
223 };
224
225 case $char === 'keep':
226 return function ($matches) {
227 return $matches[0];
228 };
229
230 case preg_match('("(?:[^\\\\"]+|\\\\\\\\|\\\\\'|\\\\")*?")', $char):
231 $string = str_replace(
232 ['\\\\', '\\"', "\\'"],
233 ['\\', '"', "'"],
234 substr($char, 1, -1)
235 );
236
237 return function ($matches) use ($string) {
238 return $string;
239 };
240
241 default:
242 $char = $this->compileCharacter($char);
243
244 return function ($matches) use ($char) {
245 return $char;
246 };
247 }
248 }
249
261 protected function compileCharacter($char)
262 {
263 switch (true) {
264 case preg_match('(^U\\+[0-9a-fA-F]{4}$)', $char):
265 return $this->converter->toUTF8Character(hexdec(substr($char, 2)));
266
267 case preg_match('(^[0-9a-fA-F]{2}$)', $char):
268 return chr(hexdec($char));
269
270 default:
271 throw new RuntimeException("Invalid character definition: $char");
272 }
273 }
274
280 private function hexdec(?string $value): int
281 {
282 if ($value === null) {
283 return 0;
284 }
285
286 return hexdec(preg_replace('/[^[:xdigit:]]/', '', (string)$value));
287 }
288 }
eZ\Publish\Core\Persistence\TransformationProcessor\PcreCompiler
Compiles the AST of parsed transformation rules into a set of PCRE replace regular expressions.
Definition: PcreCompiler.php:18
eZ\Publish\Core\Persistence\TransformationProcessor\PcreCompiler\compileTargetCharacter
compileTargetCharacter($char)
Compile target into a closure, which can be used by preg_replace_callback.
Definition: PcreCompiler.php:217
eZ\Publish\Core\Persistence\TransformationProcessor\PcreCompiler\hexdec
hexdec(?string $value)
Converts a hexadecimal string to a decimal number.
Definition: PcreCompiler.php:280
eZ\Publish\Core\Persistence\TransformationProcessor\PcreCompiler\compileTransposeModulo
compileTransposeModulo(array $rule)
Compile transpose modulo rule.
Definition: PcreCompiler.php:145
eZ\Publish\Core\Persistence\TransformationProcessor\PcreCompiler\compile
compile(array $ast)
Compile AST into a set of regular expressions.
Definition: PcreCompiler.php:47
eZ\Publish\Core\Persistence\TransformationProcessor\PcreCompiler\compileTranspose
compileTranspose(array $rule)
Compile transpose rule.
Definition: PcreCompiler.php:127
eZ\Publish\Core\Persistence\TransformationProcessor\PcreCompiler\compileRule
compileRule(array $rule)
Compiles a single rule.
Definition: PcreCompiler.php:67
eZ\Publish\Core\Persistence\TransformationProcessor\PcreCompiler\compileCharacter
compileCharacter($char)
Compile a single source character definition into a plain UTF-8 character.
Definition: PcreCompiler.php:261
eZ\Publish\Core\Persistence\TransformationProcessor\PcreCompiler\__construct
__construct(Utf8Converter $converter)
Construct from UTF8Converter.
Definition: PcreCompiler.php:31
eZ\Publish\Core\Persistence\TransformationProcessor\PcreCompiler\compileReplace
compileReplace(array $rule)
Compile replace rule.
Definition: PcreCompiler.php:109
eZ\Publish\Core\Persistence\TransformationProcessor\PcreCompiler\$converter
$converter
Definition: PcreCompiler.php:24
eZ\Publish\Core\Persistence\TransformationProcessor\PcreCompiler\getTransposeClosure
getTransposeClosure($operator, $value)
Returns a closure which modifies the provided character by the given value.
Definition: PcreCompiler.php:197
eZ\Publish\Core\Persistence\TransformationProcessor\PcreCompiler\getModuloCharRange
getModuloCharRange($start, $end, $modulo)
Get string with all characters defined by parameters.
Definition: PcreCompiler.php:174
eZ\Publish\Core\Persistence\TransformationProcessor\PcreCompiler\compileMap
compileMap(array $rule)
Compile map rule.
Definition: PcreCompiler.php:94
eZ\Publish\Core\Persistence\TransformationProcessor
Interface for processing a set of transformations on a string.
Definition: TransformationProcessor.php:15
eZ\Publish\Core\Persistence\TransformationProcessor\T_MAP
const T_MAP
Definition: TransformationProcessor.php:19
eZ\Publish\Core\Persistence\TransformationProcessor\T_REPLACE
const T_REPLACE
Definition: TransformationProcessor.php:20
eZ\Publish\Core\Persistence\TransformationProcessor\T_TRANSPOSE
const T_TRANSPOSE
Definition: TransformationProcessor.php:21
eZ\Publish\Core\Persistence\TransformationProcessor\T_TRANSPOSE_MODULO
const T_TRANSPOSE_MODULO
Definition: TransformationProcessor.php:22
eZ\Publish\Core\Persistence\Utf8Converter
Class for converting UTF-8 characters to their decimal code points and vice versa.
Definition: Utf8Converter.php:18
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.