1 | <?php |
||
17 | class PcreCompiler |
||
18 | { |
||
19 | /** |
||
20 | * Class for converting UTF-8 characters. |
||
21 | * |
||
22 | * @var \eZ\Publish\Core\Persistence\Utf8Converter |
||
23 | */ |
||
24 | protected $converter; |
||
25 | |||
26 | /** |
||
27 | * Construct from UTF8Converter. |
||
28 | * |
||
29 | * @param \eZ\Publish\Core\Persistence\Utf8Converter $converter |
||
30 | */ |
||
31 | public function __construct(Utf8Converter $converter) |
||
35 | |||
36 | /** |
||
37 | * Compile AST into a set of regular expressions. |
||
38 | * |
||
39 | * The returned array contains a set of regular expressions and their |
||
40 | * replacement callbacks. The regular expressions can then be applied to |
||
41 | * strings to executed the transformations. |
||
42 | * |
||
43 | * @param array $ast |
||
44 | * |
||
45 | * @return array |
||
46 | */ |
||
47 | public function compile(array $ast) |
||
59 | |||
60 | /** |
||
61 | * Compiles a single rule. |
||
62 | * |
||
63 | * @param array $rule |
||
64 | * |
||
65 | * @return array |
||
66 | */ |
||
67 | protected function compileRule(array $rule) |
||
86 | |||
87 | /** |
||
88 | * Compile map rule. |
||
89 | * |
||
90 | * @param array $rule |
||
91 | * |
||
92 | * @return array |
||
93 | */ |
||
94 | protected function compileMap(array $rule) |
||
101 | |||
102 | /** |
||
103 | * Compile replace rule. |
||
104 | * |
||
105 | * @param array $rule |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
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 | |||
120 | /** |
||
121 | * Compile transpose rule. |
||
122 | * |
||
123 | * @param array $rule |
||
124 | * |
||
125 | * @return array |
||
126 | */ |
||
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 | |||
138 | /** |
||
139 | * Compile transpose modulo rule. |
||
140 | * |
||
141 | * @param array $rule |
||
142 | * |
||
143 | * @return array |
||
144 | */ |
||
145 | protected function compileTransposeModulo(array $rule) |
||
160 | |||
161 | /** |
||
162 | * Get string with all characters defined by parameters. |
||
163 | * |
||
164 | * Returns a string containing all UTF-8 characters starting with the |
||
165 | * specified $start character up to the $end character with the step size |
||
166 | * defined in $modulo. |
||
167 | * |
||
168 | * @param string $start |
||
169 | * @param string $end |
||
170 | * @param string $modulo |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | protected function getModuloCharRange($start, $end, $modulo) |
||
187 | |||
188 | /** |
||
189 | * Returns a closure which modifies the provided character by the given |
||
190 | * value. |
||
191 | * |
||
192 | * @param string $operator |
||
193 | * @param string $value |
||
194 | * |
||
195 | * @return callback |
||
196 | */ |
||
197 | protected function getTransposeClosure($operator, $value) |
||
208 | |||
209 | /** |
||
210 | * Compile target into a closure, which can be used by |
||
211 | * preg_replace_callback. |
||
212 | * |
||
213 | * @param string $char |
||
214 | * |
||
215 | * @return callback |
||
216 | */ |
||
217 | protected function compileTargetCharacter($char) |
||
249 | |||
250 | /** |
||
251 | * Compile a single source character definition into a plain UTF-8 character. |
||
252 | * |
||
253 | * Handles the two formats from the possible character definitions: |
||
254 | * - U+xxxx : Unicode value in hexadecimal |
||
255 | * - xx: Ascii value in hexadecimal |
||
256 | * |
||
257 | * @param string $char |
||
258 | * |
||
259 | * @return string |
||
260 | */ |
||
261 | protected function compileCharacter($char) |
||
274 | |||
275 | /** |
||
276 | * Converts a hexadecimal string to a decimal number. |
||
277 | * |
||
278 | * In comparison to standard hexdec function it will ignore any non-hexadecimal characters |
||
279 | */ |
||
280 | private function hexdec(?string $value): int |
||
288 | } |
||
289 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.