Skip to content

Commit

Permalink
Added Helpers::escape()
Browse files Browse the repository at this point in the history
This reverts commit df4eef2.
  • Loading branch information
dg committed Aug 5, 2020
1 parent 206328f commit 5ddfaf1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/DI/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
23 changes: 23 additions & 0 deletions tests/DI/Helpers.escape().phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* Test: Nette\DI\Helpers::escape()
*/

declare(strict_types=1);

use Nette\DI\Helpers;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


Assert::same(123, Helpers::escape(123));
Assert::same('%%a%%', Helpers::escape('%a%'));
Assert::same('@@', Helpers::escape('@'));
Assert::same('x@', Helpers::escape('x@'));
Assert::same(
['key1' => '%%', 'key2' => '@@', '%%a%%' => 123, '@' => 123],
Helpers::escape(['key1' => '%', 'key2' => '@', '%a%' => 123, '@' => 123])
);

0 comments on commit 5ddfaf1

Please sign in to comment.