add escapeValue to ParameterBagInterface
This commit is contained in:
parent
49e213ce7c
commit
1227cc2a8a
@ -253,6 +253,27 @@ class ParameterBag implements ParameterBagInterface
|
||||
return $this->resolved;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function escapeValue($value)
|
||||
{
|
||||
if (is_string($value)) {
|
||||
return str_replace('%', '%%', $value);
|
||||
}
|
||||
|
||||
if (is_array($value)) {
|
||||
$result = array();
|
||||
foreach ($value as $k => $v) {
|
||||
$result[$k] = $this->escapeValue($v);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
private function unescapeValue($value)
|
||||
{
|
||||
if (is_string($value)) {
|
||||
|
@ -94,4 +94,13 @@ interface ParameterBagInterface
|
||||
* @throws ParameterNotFoundException if a placeholder references a parameter that does not exist
|
||||
*/
|
||||
function resolveValue($value);
|
||||
|
||||
/**
|
||||
* Escape parameter placeholders %
|
||||
*
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
function escapeValue($value);
|
||||
}
|
||||
|
@ -201,6 +201,22 @@ class ParameterBagTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals(array('bar' => array('ding' => 'I\'m a bar %foo %bar')), $bag->get('foo'), '->resolveValue() supports % escaping by doubling it');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::escapeValue
|
||||
*/
|
||||
public function testEscapeValue()
|
||||
{
|
||||
$bag = new ParameterBag();
|
||||
|
||||
$bag->add(array(
|
||||
'foo' => $bag->escapeValue(array('bar' => array('ding' => 'I\'m a bar %foo %bar', 'zero' => null))),
|
||||
'bar' => $bag->escapeValue('I\'m a %foo%'),
|
||||
));
|
||||
|
||||
$this->assertEquals('I\'m a %%foo%%', $bag->get('bar'), '->escapeValue() escapes % by doubling it');
|
||||
$this->assertEquals(array('bar' => array('ding' => 'I\'m a bar %%foo %%bar', 'zero' => null)), $bag->get('foo'), '->escapeValue() escapes % by doubling it');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::resolve
|
||||
* @dataProvider stringsWithSpacesProvider
|
||||
|
Reference in New Issue
Block a user