|
35 | 35 | use function preg_split; |
36 | 36 | use function random_bytes; |
37 | 37 | use function random_int; |
| 38 | +use function sprintf; |
38 | 39 | use function str_pad; |
39 | 40 | use function str_repeat; |
40 | 41 | use function str_replace; |
@@ -275,14 +276,33 @@ public static function genNOV2($prefix = '', array $randomRange = []): string |
275 | 276 | } |
276 | 277 |
|
277 | 278 | /** |
278 | | - * @param string $template |
| 279 | + * @param string $tplCode |
279 | 280 | * @param array $vars |
280 | 281 | * |
281 | 282 | * @return string |
282 | 283 | */ |
283 | | - public static function replaces(string $template, array $vars): string |
| 284 | + public static function replaces(string $tplCode, array $vars): string |
284 | 285 | { |
285 | | - return strtr($template, $vars); |
| 286 | + return strtr($tplCode, $vars); |
| 287 | + } |
| 288 | + |
| 289 | + /** |
| 290 | + * @param string $tplCode |
| 291 | + * @param array $vars |
| 292 | + * @param string $format |
| 293 | + * |
| 294 | + * @return string |
| 295 | + */ |
| 296 | + public static function renderTemplate(string $tplCode, array $vars, string $format = '{{%s}}'): string |
| 297 | + { |
| 298 | + $fmtVars = []; |
| 299 | + foreach ($vars as $name => $var) { |
| 300 | + $name = sprintf($format, (string)$name); |
| 301 | + // add |
| 302 | + $fmtVars[$name] = $var; |
| 303 | + } |
| 304 | + |
| 305 | + return strtr($tplCode, $fmtVars); |
286 | 306 | } |
287 | 307 |
|
288 | 308 | //////////////////////////////////////////////////////////// |
@@ -335,6 +355,30 @@ public static function removeQuotes(string $str): string |
335 | 355 | return $str; |
336 | 356 | } |
337 | 357 |
|
| 358 | + /** |
| 359 | + * @param string $str |
| 360 | + * @param bool $quoteAll |
| 361 | + * |
| 362 | + * @return string |
| 363 | + */ |
| 364 | + public static function paramQuotes(string $str, bool $quoteAll = false): string |
| 365 | + { |
| 366 | + if ($str === '') { |
| 367 | + return "''"; |
| 368 | + } |
| 369 | + |
| 370 | + if ( |
| 371 | + !$quoteAll && |
| 372 | + (preg_match("/^\".*\"$/", $str) || preg_match("/^'.*'$/", $str)) |
| 373 | + ) { |
| 374 | + return $str; |
| 375 | + } |
| 376 | + |
| 377 | + $quote = str_contains($str, "'") ? '"' : "'"; |
| 378 | + |
| 379 | + return $quote . $str . $quote; |
| 380 | + } |
| 381 | + |
338 | 382 | /** |
339 | 383 | * @param array $list |
340 | 384 | * @param string $wrapChar |
@@ -377,7 +421,7 @@ public static function shellQuote(string $arg): string |
377 | 421 | $quote = ''; |
378 | 422 | if (str_contains($arg, '"')) { |
379 | 423 | $quote = "'"; |
380 | | - } elseif ($arg === '' || strpos($arg, "'") !== false || strpos($arg, ' ') !== false) { |
| 424 | + } elseif ($arg === '' || str_contains($arg, "'") || str_contains($arg, ' ')) { |
381 | 425 | $quote = '"'; |
382 | 426 | } |
383 | 427 |
|
|
0 commit comments