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]) +);