1 | <?php |
||
19 | class PcreCompiler |
||
20 | { |
||
21 | /** |
||
22 | * Class for converting UTF-8 characters. |
||
23 | * |
||
24 | * @var \eZ\Publish\Core\Persistence\Utf8Converter |
||
25 | */ |
||
26 | protected $converter; |
||
27 | |||
28 | /** |
||
29 | * Construct from UTF8Converter. |
||
30 | * |
||
31 | * @param \eZ\Publish\Core\Persistence\Utf8Converter $converter |
||
32 | */ |
||
33 | public function __construct(Utf8Converter $converter) |
||
37 | |||
38 | /** |
||
39 | * Compile AST into a set of regular expressions. |
||
40 | * |
||
41 | * The returned array contains a set of regular expressions and their |
||
42 | * replacement callbacks. The regular expressions can then be applied to |
||
43 | * strings to executed the transformations. |
||
44 | * |
||
45 | * @param array $ast |
||
46 | * |
||
47 | * @return array |
||
48 | */ |
||
49 | public function compile(array $ast) |
||
61 | |||
62 | /** |
||
63 | * Compiles a single rule. |
||
64 | * |
||
65 | * @param array $rule |
||
66 | * |
||
67 | * @return array |
||
68 | */ |
||
69 | protected function compileRule(array $rule) |
||
88 | |||
89 | /** |
||
90 | * Compile map rule. |
||
91 | * |
||
92 | * @param array $rule |
||
93 | * |
||
94 | * @return array |
||
95 | */ |
||
96 | protected function compileMap(array $rule) |
||
103 | |||
104 | /** |
||
105 | * Compile replace rule. |
||
106 | * |
||
107 | * @param array $rule |
||
108 | * |
||
109 | * @return array |
||
110 | */ |
||
111 | protected function compileReplace(array $rule) |
||
112 | { |
||
113 | return [ |
||
114 | 'regexp' => '([' . |
||
115 | preg_quote($this->compileCharacter($rule['data']['srcStart'])) . '-' . |
||
116 | preg_quote($this->compileCharacter($rule['data']['srcEnd'])) . |
||
117 | '])us', |
||
118 | 'callback' => $this->compileTargetCharacter($rule['data']['dest']), |
||
119 | ]; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Compile transpose rule. |
||
124 | * |
||
125 | * @param array $rule |
||
126 | * |
||
127 | * @return array |
||
128 | */ |
||
129 | protected function compileTranspose(array $rule) |
||
130 | { |
||
131 | return [ |
||
132 | 'regexp' => '([' . |
||
133 | preg_quote($this->compileCharacter($rule['data']['srcStart'])) . '-' . |
||
134 | preg_quote($this->compileCharacter($rule['data']['srcEnd'])) . |
||
135 | '])us', |
||
136 | 'callback' => $this->getTransposeClosure($rule['data']['op'], $rule['data']['dest']), |
||
137 | ]; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Compile transpose modulo rule. |
||
142 | * |
||
143 | * @param array $rule |
||
144 | * |
||
145 | * @return array |
||
146 | */ |
||
147 | protected function compileTransposeModulo(array $rule) |
||
162 | |||
163 | /** |
||
164 | * Get string with all characters defined by parameters. |
||
165 | * |
||
166 | * Returns a string containing all UTF-8 characters starting with the |
||
167 | * specified $start character up to the $end character with the step size |
||
168 | * defined in $modulo. |
||
169 | * |
||
170 | * @param string $start |
||
171 | * @param string $end |
||
172 | * @param string $modulo |
||
173 | * |
||
174 | * @return string |
||
175 | */ |
||
176 | protected function getModuloCharRange($start, $end, $modulo) |
||
189 | |||
190 | /** |
||
191 | * Returns a closure which modifies the provided character by the given |
||
192 | * value. |
||
193 | * |
||
194 | * @param string $operator |
||
195 | * @param string $value |
||
196 | * |
||
197 | * @return callback |
||
198 | */ |
||
199 | protected function getTransposeClosure($operator, $value) |
||
210 | |||
211 | /** |
||
212 | * Compile target into a closure, which can be used by |
||
213 | * preg_replace_callback. |
||
214 | * |
||
215 | * @param string $char |
||
216 | * |
||
217 | * @return callback |
||
218 | */ |
||
219 | protected function compileTargetCharacter($char) |
||
251 | |||
252 | /** |
||
253 | * Compile a single source character definition into a plain UTF-8 character. |
||
254 | * |
||
255 | * Handles the two formats from the possible character definitions: |
||
256 | * - U+xxxx : Unicode value in hexadecimal |
||
257 | * - xx: Ascii value in hexadecimal |
||
258 | * |
||
259 | * @param string $char |
||
260 | * |
||
261 | * @return string |
||
262 | */ |
||
263 | protected function compileCharacter($char) |
||
276 | |||
277 | /** |
||
278 | * Converts a hexadecimal string to a decimal number. |
||
279 | * |
||
280 | * In compare to standard hexdec function it will ignore any non-hexadecimal characters |
||
281 | */ |
||
282 | private function hexdec(?string $value): int |
||
290 | } |
||
291 |
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.