From 5ddfaf1cbe26e3451838e3b2861842ac8d8e9aa5 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 25 May 2020 22:28:04 +0200 Subject: [PATCH] Added Helpers::escape() This reverts commit df4eef206d37a5665d1b4e575bf01fd30c3d63c0. --- src/DI/Helpers.php | 21 +++++++++++++++++++++ tests/DI/Helpers.escape().phpt | 23 +++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 tests/DI/Helpers.escape().phpt diff --git a/src/DI/Helpers.php b/src/DI/Helpers.php index e4bf5f4f7..b24309a52 100644 --- a/src/DI/Helpers.php +++ b/src/DI/Helpers.php @@ -98,6 +98,27 @@ public static function expand($var, array $params, $recursive = false) } + /** + * Escapes '%' and '@' + * @param mixed + * @return mixed + */ + public static function escape($value) + { + if (is_array($value)) { + $res = []; + foreach ($value as $key => $val) { + $key = is_string($key) ? str_replace('%', '%%', $key) : $key; + $res[$key] = self::escape($val); + } + return $res; + } elseif (is_string($value)) { + return preg_replace('#^@|%#', '$0$0', $value); + } + return $value; + } + + /** * Performs internal functions like not(), bool() ... recursively. */ diff --git a/tests/DI/Helpers.escape().phpt b/tests/DI/Helpers.escape().phpt new file mode 100644 index 000000000..522946581 --- /dev/null +++ b/tests/DI/Helpers.escape().phpt @@ -0,0 +1,23 @@ + '%%', 'key2' => '@@', '%%a%%' => 123, '@' => 123], + Helpers::escape(['key1' => '%', 'key2' => '@', '%a%' => 123, '@' => 123]) +);